22
Guava’s Event Bus 1

Guava’s Event Bus

Embed Size (px)

DESCRIPTION

Introduction slides about Google's Guava Event Bus framework

Citation preview

Page 1: Guava’s Event Bus

Guava’s Event Bus

1

Page 2: Guava’s Event Bus

Traditional Events

Listener Listener

Activity

Service / Helper

Thread

Activity

Page 3: Guava’s Event Bus

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

Page 4: Guava’s Event Bus

EventBus Communication

Fragment Fragment

Activity

Service / Helper

Thread

Activity

EventBus

Page 5: Guava’s Event Bus

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

Page 6: Guava’s Event Bus

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

Page 7: Guava’s Event Bus

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);

Page 8: Guava’s Event Bus

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>

Page 9: Guava’s Event Bus

EventBus : Listener

• Define an event handler@ComponentPublic class EventHandler{}

Event Handler

Event

Bus

Page 10: Guava’s Event Bus

EventBus: Listener Registration

• Register handler@PostConstructpublic void registerHandler(){

asyncEventBus.register(this);}

Event Handler

Event

Bus

register

Page 11: Guava’s Event Bus

EventBus: Listener Subscription

• Subscribe for events a.k.a objects@Subscribe@AllowConcurrentEventspublic void handleEventMethod(ObjectA objA){}

Event Handler

Event

Bus

subscribe

register

Page 12: Guava’s Event Bus

EventBus: Event Post

• Post an event// Post information back to event busasyncEventBus.post(objA);

Event Handler

Event

Bus

subscribe

registerEventpost

Page 13: Guava’s Event Bus

Guava Event Bus

Event

Bus

Event

Event

Event

Event Handler

Event Handler

Event Handler

post

post

post

subscribe

subscribe

subscribe

register

register

register

Page 14: Guava’s Event Bus

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

Page 15: Guava’s Event Bus

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) {…}

Page 16: Guava’s Event Bus

Publish / Subscribe

Publisher EventBus

Page 17: Guava’s Event Bus

Publish / Subscribe

Publisher EventBus

Event

post(Object1)

Page 18: Guava’s Event Bus

Publish / Subscribe

Publisher EventBus

Subscriber

Event

post(Object1)

Event handleMethod1(Object1)

Subscriber

Event

handleMethod2(Object1)

Page 19: Guava’s Event Bus

Event Type is a Filter

Publisher EventBus

Subscriber

Event

post(user)

EventhandleUserXXX(User)

Subscriber

handleAddressXXX(Address)

Page 20: Guava’s Event Bus

It‘s time to see some

• CODE

Page 21: Guava’s Event Bus

EventBus Code: Sender (Spring)@Autowired

private EventBus eventBus;

eventBus.post(user);

OR

@Autowired

private AsyncEventBus asyncEventBus;

asyncEventBus.post(user);

Page 22: Guava’s Event Bus

EventBus Code: Receiver (Spring)@Componentpublic class UserListener{

@Autowiredprivate AsyncEventBus asyncEventBus;

@PostConstructpublic void registerHandler(){

asyncEventBus.register(this);}

@PreDestroypublic void unRegisterHandler(){

asyncEventBus.unregister(this);}

@Subscribe@AllowConcurrentEventspublic void handleUserXXX(User user){

// your logic}

}