3. Communication Issues?!
• Tight coupling of components
Inflexible, changes are expensive
• Boiler plate code
– Define interfaces
– Callbacks for asynch. communication
– Listener management
– Propagation through all layers
5. Guava’s EventBus
• A message dispatching system
– to allow publish-subscribe style of
communication between components
– No-nonsense
– Lightweight
– and very practical
• Everything happens within the run-time
boundaries of the same Java application.
https://code.google.com/p/guava-
libraries/wiki/EventBusExplained
6. Guava’s EventBus
• The Event Bus comes in two flavours:
– Synchronous (backed-up by the EventBus class), and
– Asynchronous (backed-up by the AsyncEventBus class which
extends EventBus)
• com.google.common.eventbus package
• Exposed API
– void register(Object) - registers and caches
subscribers for subsequent event handling
– void unregister(Object) - undoes the register action
– void post(Object) - posts an event (event) to all
registered subscribers
7. Building an Event Bus
• The Guava’s Event (Message) Bus itself
• The event (message), which can be any Java
object: a Date, a String, your POJO etc…
• The event subscriber (listener) – any
complexity Java class that must have a specially
annotated method for handling events (messages);
8. EventBus Configuration (Spring way)
• EventBus instances will create when
application is deploying
<bean id="eventBus" class="com.google.common.even
tbus.EventBus" />
<bean id="asyncEventBus"
class="com.google.common.eventbus.AsyncEventBus">
<constructor-arg name="executor" ref="executorService" />
</bean>
9. EventBus : Listener
• Define an event handler
@Component
Public class EventHandler
{
}
Event
Handler
E
v
e
n
t
B
u
s
10. EventBus: Listener Registration
• Register handler
@PostConstruct
public void registerHandler()
{
asyncEventBus.register(this);
}
Event
Handler
E
v
e
n
t
B
u
s
register
11. EventBus: Listener Subscription
• Subscribe for events a.k.a objects
@Subscribe
@AllowConcurrentEvents
public void handleEventMethod(ObjectA objA)
{
}
Event
Handler
E
v
e
n
t
B
u
s
subscribe
register
12. EventBus: Event Post
• Post an event
// Post information back to event bus
asyncEventBus.post(objA);
Event
Handler
E
v
e
n
t
B
u
s
subscribe
registerEvent
post
14. Event Handler Method
• EventBus scans subscribers
– During (first) registration of subscriber and
registers the event listener methods based on
the method’s parameter type
• Event handler methods
– public visibility
– No return value (void)
– Single parameter for the event to receive
14
15. Type-based Event Routing
• Event type: Java class of the event
• To receive an event, its type must match
• E.g.
post(new User());
handleUserXXX(User user) {…}
Post(new Address());
handleAddressXXX(Address address) {…}
22. EventBus Code: Receiver (Spring)
@Component
public class UserListener
{
@Autowired
private AsyncEventBus asyncEventBus;
@PostConstruct
public void registerHandler()
{
asyncEventBus.register(this);
}
@PreDestroy
public void unRegisterHandler()
{
asyncEventBus.unregister(this);
}
@Subscribe
@AllowConcurrentEvents
public void handleUserXXX(User user)
{
// your logic
}
}
Notes de l'éditeur
TraditionalAs stated above — the traditional method requires an interface declaration, an explicit subscription, and knowledge of the object that is posting the particular event. Additionally, it forces the object that is posting the event to invent its own method of publishing the event.
The Guava’s Event (Message) Bus itself – represented by the EventBus class that provides methods for subscribers (event listeners) to register and unregister themselves with the Bus as well as a method for dispatching events (messages) to the target subscribers The event (message), which can be any Java object: a Date, a String, your POJO, anything that can be routed by the Bus to your subscriber The event subscriber (listener) – an arbitrary complexity Java class that must have a specially annotated method for handling events (messages); this method is a call-back function that must return void and take one parameter of the same type as the type of the corresponding event (a Date, a String, your POJO, etc.)