Your SlideShare is downloading. ×
0
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Guava’s Event Bus
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Guava’s Event Bus

4,844

Published on

Introduction slides about Google's Guava Event Bus framework

Introduction slides about Google's Guava Event Bus framework

Published in: Technology, Business
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,844
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
54
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • 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.)
  • Transcript

    • 1. Guava’s Event Bus 1
    • 2. Traditional Events Listener Listener Activity Service / Helper Thread Activity
    • 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. EventBus Communication Fragment Fragment Activity Service / Helper Thread Activity Event Bus
    • 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
    • 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. 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) {…}
    • 16. Publish / Subscribe Publisher Event Bus
    • 17. Publish / Subscribe Publisher Event Bus Event post(Object1)
    • 18. Publish / Subscribe Publisher Event Bus Subscriber Event post(Object1) Event handleMethod1(Object1) Subscriber Event handleMethod2(Object1)
    • 19. Event Type is a Filter Publisher Event Bus Subscriber Event post (user) Event handleUserXXX(User) Subscriber handleAddressXXX(Address)
    • 20. It‘s time to see some • CODE
    • 21. EventBus Code: Sender (Spring) @Autowired private EventBus eventBus; … eventBus.post(user); … OR @Autowired private AsyncEventBus asyncEventBus; … asyncEventBus.post(user); …
    • 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 } }

    ×