Guava’s Event Bus

Post on 22-Dec-2014

2,558 views 1 download

Tags:

description

Introduction slides about Google's Guava Event Bus framework

transcript

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 interfaces– Callbacks for asynch. communication– Listener management– Propagation through all layers

EventBus Communication

Fragment Fragment

Activity

Service / Helper

Thread

Activity

EventBus

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

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

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

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>

EventBus : Listener

• Define an event handler@ComponentPublic class EventHandler{}

Event Handler

Event

Bus

EventBus: Listener Registration

• Register handler@PostConstructpublic void registerHandler(){

asyncEventBus.register(this);}

Event Handler

Event

Bus

register

EventBus: Listener Subscription

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

Event Handler

Event

Bus

subscribe

register

EventBus: Event Post

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

Event Handler

Event

Bus

subscribe

registerEventpost

Guava Event Bus

Event

Bus

Event

Event

Event

Event Handler

Event Handler

Event Handler

post

post

post

subscribe

subscribe

subscribe

register

register

register

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

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

Publish / Subscribe

Publisher EventBus

Publish / Subscribe

Publisher EventBus

Event

post(Object1)

Publish / Subscribe

Publisher EventBus

Subscriber

Event

post(Object1)

Event handleMethod1(Object1)

Subscriber

Event

handleMethod2(Object1)

Event Type is a Filter

Publisher EventBus

Subscriber

Event

post(user)

EventhandleUserXXX(User)

Subscriber

handleAddressXXX(Address)

It‘s time to see some

• CODE

EventBus Code: Sender (Spring)@Autowired

private EventBus eventBus;

eventBus.post(user);

OR

@Autowired

private AsyncEventBus asyncEventBus;

asyncEventBus.post(user);

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}

}