Interface HippoEventBus
public interface HippoEventBus
Generic Hippo event bus. Specific implementations can dispatch events based on metadata of the listener and event
objects. (see e.g. the guava implementation)
Listeners must be registered through the HippoEventListenerRegistry
which the HippoEventBus service
will (must) query, or track itself, for the available listeners to dispatch events to (Whiteboard pattern).
Methods in the listener class hierarchy annotated with the Subscribe
annotation will be invoked when
events of a suitable type are dispatched. (see the Subscribe
documentation for details). Example:
Example listener implementation using the Subscribe
annotation:
MyObject() {
... initialization
// register as a listener using the HippoEventListenerRegistry:
HippoEventListenerRegistry.get().register(this);
// implement a HippoEvent receiver method and annotate it with 'Subscribe'
// Note that the method with the annotation MUST be public
@Subscribe
public void processHippoEvent(HippoEvent<?> event) {
System.out.println(event);
}
}
// get the eventBus
HippoEventBus eventBus = HippoServiceRegistry.getService(HippoEventBus.class);
// post an event which subsequently will be printed by the @Subscribe annotated method defined above
eventBus.post(new HippoEvent("foo").message("bar"));
When you are only interested in HippoEvent
s, you also can implement the
HippoEventListener
instead. Then you also don't need to annotate the onEvent method. For example:
HippoEventListenerRegistry.get().register(new HippoEventListener() {
// implement the onEvent method
// Note that the method MUST be public
public void onEvent(HippoEvent<?> event) {
System.out.println(event);
}
});
-
Method Summary
-
Method Details
-
post
Publish an event to registered listeners. TheirSubscribe
annotated methods (with suitable parameter types), or implementations theirHippoEventListener.onEvent(HippoEvent)
method will be invoked.- Parameters:
event
- event to be published
-