Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Guava’s Event Bus
1
Traditional Events
Listener Listener
Activity
Service / Helper
Thread
Activity
Communication Issues?!
• Tight coupling of components
 Inflexible, changes are expensive
• Boiler plate code
– Define int...
EventBus Communication
Fragment Fragment
Activity
Service / Helper
Thread
Activity
Event
Bus
Guava’s EventBus
• A message dispatching system
– to allow publish-subscribe style of
communication between components
– N...
Guava’s EventBus
• The Event Bus comes in two flavours:
– Synchronous (backed-up by the EventBus class), and
– Asynchronou...
Building an Event Bus
• The Guava’s Event (Message) Bus itself
• The event (message), which can be any Java
object: a Date...
EventBus Configuration (Spring way)
• EventBus instances will create when
application is deploying
<bean id="eventBus" cla...
EventBus : Listener
• Define an event handler
@Component
Public class EventHandler
{
}
Event
Handler
E
v
e
n
t
B
u
s
EventBus: Listener Registration
• Register handler
@PostConstruct
public void registerHandler()
{
asyncEventBus.register(t...
EventBus: Listener Subscription
• Subscribe for events a.k.a objects
@Subscribe
@AllowConcurrentEvents
public void handleE...
EventBus: Event Post
• Post an event
// Post information back to event bus
asyncEventBus.post(objA);
Event
Handler
E
v
e
n...
Guava Event Bus
E
v
e
n
t
B
u
s
Event
Event
Event
Event
Handler
Event
Handler
Event
Handler
post
post
post
subscribe
subsc...
Event Handler Method
• EventBus scans subscribers
– During (first) registration of subscriber and
registers the event list...
Type-based Event Routing
• Event type: Java class of the event
• To receive an event, its type must match
• E.g.
post(new ...
Publish / Subscribe
Publisher
Event
Bus
Publish / Subscribe
Publisher
Event
Bus
Event
post(Object1)
Publish / Subscribe
Publisher
Event
Bus
Subscriber
Event
post(Object1)
Event
handleMethod1(Object1)
Subscriber
Event
handl...
Event Type is a Filter
Publisher
Event
Bus
Subscriber
Event
post
(user)
Event
handleUserXXX(User)
Subscriber
handleAddress...
It‘s time to see some
• CODE
EventBus Code: Sender (Spring)
@Autowired
private EventBus eventBus;
…
eventBus.post(user);
…
OR
@Autowired
private AsyncE...
EventBus Code: Receiver (Spring)
@Component
public class UserListener
{
@Autowired
private AsyncEventBus asyncEventBus;
@P...
Upcoming SlideShare
Loading in …5
×

Guava’s Event Bus

14,778 views

Published on

Introduction slides about Google's Guava Event Bus framework

Published in: Technology, Business

Guava’s Event Bus

  1. 1. Guava’s Event Bus 1
  2. 2. Traditional Events Listener Listener Activity Service / Helper Thread Activity
  3. 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
  4. 4. EventBus Communication Fragment Fragment Activity Service / Helper Thread Activity Event Bus
  5. 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. 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. 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. 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. 9. EventBus : Listener • Define an event handler @Component Public class EventHandler { } Event Handler E v e n t B u s
  10. 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. 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. 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
  13. 13. Guava Event Bus E v e n t B u s Event Event Event Event Handler Event Handler Event Handler post post post subscribe subscribe subscribe register register register
  14. 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. 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) {…}
  16. 16. Publish / Subscribe Publisher Event Bus
  17. 17. Publish / Subscribe Publisher Event Bus Event post(Object1)
  18. 18. Publish / Subscribe Publisher Event Bus Subscriber Event post(Object1) Event handleMethod1(Object1) Subscriber Event handleMethod2(Object1)
  19. 19. Event Type is a Filter Publisher Event Bus Subscriber Event post (user) Event handleUserXXX(User) Subscriber handleAddressXXX(Address)
  20. 20. It‘s time to see some • CODE
  21. 21. EventBus Code: Sender (Spring) @Autowired private EventBus eventBus; … eventBus.post(user); … OR @Autowired private AsyncEventBus asyncEventBus; … asyncEventBus.post(user); …
  22. 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 } }

×