Handling events

To handle fired event, we have to register listener in EventBus. When listener is registered, EventBus automatically subscribes for this type of event in all instances in cluster.

Depends on expected event type, we have to decide what type of listener we should register.

Handling class based events

This option is reserved for class based events only. It doesn’t matter if it is serialized class or not.

eventBus.addListener(SampleEvent.class, new EventListener<SampleEvent>() {

    @Override
    public void onEvent(SampleEvent event) {
    }
});

To make registering listeners more easy, you can use method registerAll() from EventBus. This method registers all methods given class, annotated by @HandleEvent as listeners for event declared as the method argument.

public class SomeConsumer {

    @HandleEvent
    public void event1(Event12 e) {
    }

    public void initialize() {
        eventBus.registerAll(this);
    }
}
Handling XML events

To handle XML events we have to register listener for specific event package and name. In our example, package is empty because event name has no package declared (see also the section called “Filtering events”).

eventBus.addListener("", "EventName", new EventListener<Element>() {
    @Override
    public void onEvent(Element event) {

    }
});
eventBus.addListener("tigase.eventbus.impl", "Event1", new EventListener<Element>() {
    @Override
    public void onEvent(Element event) {

    }
});

Because serialized class events, ale transformed to XML elements, we are able to listen for XML representation of class based event. To do that, we have to register listener for specific package and class name:

eventBus.addListener("net.tigase.sample", "SampleSerializedEvent", new EventListener<Element>() {
    @Override
    public void onEvent(Element event) {

    }
});

Important

XML events created on others cluster node, will have attribute remote set to true and attribute source set to event creator node name:

<EventName xmlns="tigase:demo" remote="true" source="node1.example">
  <sample_value>1</sample_value>
</EventName>
Filtering events

Sometimes you may want to receive many kinds of events with the same handler. EventBus has very simple mechanism to generalization:

eventBus.addListener("net.tigase.sample", null,  event -> {}); 1
eventBus.addListener(null, null,  event -> {}); 2

1

This listener will be called for each event with given package name (XML based, or serialized class based).

2

This listener will be called for ALL events (XML based, or serialized class based).

In case of class based events, EventBus is checking class inheritance.

class MainEvent { }
class SpecificEvent extends MainEvent {}

eventBus.addListener(SpecificEvent.class, event -> {}); 1
eventBus.addListener(MainEvent.class, event -> {}); 2

eventBus.fire(new SpecificEvent());
  1. Will be called, because this is listener stricte for SpecificEvent.
  2. Will be called, because SpecificEvent extends MainEvent.