-
Notifications
You must be signed in to change notification settings - Fork 5
How to use the GumTree Event Bus_124649505
nxi edited this page Apr 9, 2015
·
1 revision
Created by Tony Lam, last modified on Jul 09, 2009
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.
- 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));
- 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
- Event Bus is an example of Pub-sub Event broadcasting mechanism in Java
Document generated by Confluence on Apr 01, 2015 00:11
Home | Developer Guide | Copyright © 2013 ANSTO