-
Notifications
You must be signed in to change notification settings - Fork 5
Event Bus_138773132
nxi edited this page Apr 9, 2015
·
1 revision
Created by Tony Lam, last modified on Feb 07, 2010
GumTree event bus is a powerful API for simplifying the use of listener pattern in your code. Listener pattern is sometimes considered to be difficult to maintain because programmers need to include many addXXListener() / removeXXXListener() in the API. Event bus is introduce to solve this coupling problem, together with providing a more centralised way of managing all events in the system. GumTree event bus is based on the well known publish-subscribe pattern.
Let's say we have a publisher which can produce an event upon state change. For each state change, it will pass an event called StateChangedEvent to all of its subscribers.
Of course, if the filtering is not in use, the event bus will remain as a broadcast publisher.
- Step 1:
StateChangedEvent implementation (must implement from org.gumtree.core.eventbus.IEvent):private State newState; public StateChangedEvent(Object publisher, State newState) { super(publisher); } public State getNewState() { return newState; } }
- Step 3:
Now, we implement a subscriber to catch this event:class EventHandler implements IEventHandler<StateChangedEvent> { public void handleEvent(StateChangedEvent event) { System.out.println("New state: " + event. getNewState()); } }
- Step 3:
We subscribe this handler to the event bus:GTPlatform.getPlatformEventBus().subscribe(eventHandler);
GTPlatform.getPlatformEventBus().subscribe(publisher, eventHandler);
- Step 4:
Publish event in an asynchronised wayGTPlatform.getPlatformEventBus().postEvent(new StateChangedEvent(publisher, newState));
public boolean isDispatchable(StateChangedEvent event) { // Only notify me if there is a new event return event.equals(StateChangedEvent.NEW); } }
- Dose not support removal of subscriber on bundle removal from OSGi. This can be solved using the whiteboard pattern or event admin API from OSGi.
- Each event bus has only one thread in its threading pool. If there is a long running task inside a handleEvent() method, all event dispatching will be delayed. For any mission critical event handling, consider creating your own private event bus rather than using the global one from the GumTree platform.
- Event-driven programming from wikipedia
- Message Bus a.k.a. Event Bus
- Event Bus is an example of Pub-sub Event broadcasting mechanism in Java
- E4/Event Processing discusses the event bus proposal for Eclipse 4.0
Document generated by Confluence on Apr 01, 2015 00:11
Home | Developer Guide | Copyright © 2013 ANSTO