public interface HippoEventBus
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);
}
});
Modifier and Type | Method and Description |
---|---|
void |
post(Object event)
Publish an event to registered listeners.
|
void post(Object event)
Subscribe
annotated methods (with suitable parameter
types), or implementations their HippoEventListener.onEvent(HippoEvent)
method
will be invoked.event
- event to be publishedCopyright © 2012–2019 Hippo B.V. (http://www.onehippo.com). All rights reserved.