Skip to content

Event Bus_138773132

nxi edited this page Apr 9, 2015 · 1 revision
Created by Tony Lam, last modified on Feb 07, 2010

Introduction

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.

How to use the Event Bus

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);
    You may also use the following API if you are only interest in a particular producer:
    GTPlatform.getPlatformEventBus().subscribe(publisher, eventHandler);
  • Step 4:
    Publish event in an asynchronised way
    GTPlatform.getPlatformEventBus().postEvent(new StateChangedEvent(publisher, newState));
    To send event in a synchronised way, use sendEvent().

Event Filtering

Many event bus implementations provide filtering mechanism to avoid unrelated events passing to the subscribers. For example, OSGi uses the topic string matching as the event filter. In GumTree, you can do this by subclassing the event handler with org.gumtree.core.eventbus.IFilteredEventHandler. For example,
    public boolean isDispatchable(StateChangedEvent event) {
        // Only notify me if there is a new event
        return event.equals(StateChangedEvent.NEW);
    }
}
Of course, if the filtering is not in use, the event bus will remain as a broadcast publisher.

Limitation:

  • 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.

References:

Document generated by Confluence on Apr 01, 2015 00:11
Clone this wiki locally