-
Notifications
You must be signed in to change notification settings - Fork 1
EventSubscriber
A class that implements the EventSubscriber interface will receive events published to the framework. In order to receive published events, an EventSubscriber must be registered with an InfinitumContext with subscribeForEvents. EventSubscriber implementations can be detected by Infinitum if they are annotated with Bean or Component, in which they are automatically registered.
Also note that events must be enabled in infinitum.cfg.xml and the AOP module must be included:
<application>
<property name="events">true</property> <!-- [true | false] -->
</application>EventSubscriber has a single method which must be implemented called onEventPublished, which is invoked by the framework when an event is published. InfinitumActivity, InfinitumListActivity, InfinitumFragmentActivity, InfinitumFragment, and InfinitumListFragment all implement EventSubscriber, so extensions of these classes can override onEventPublished to consume events.
The examples below shows how EventSubscriber is implemented to receive events. The first example shows how an EventSubscriber can be detected and registered by the framework through component scanning, and the second example shows how an EventSubscriber is explicitly registered.
@Component
public class MyEventSubscriber implements EventSubscriber {
public void onEventPublished(AbstractEvent event) {
if ("userAdded".equals(event.getName()) {
User user = (User) event.getPayloadValue("user");
// Do something with user
}
}
}public class MyEventSubscriber implements EventSubscriber {
public void registerSubscriber() {
ContextFactory.getInstance().getContext().subscribeForEvents(this);
}
public void onEventPublished(AbstractEvent event) {
if ("userAdded".equals(event.getName()) {
User user = (User) event.getPayloadValue("user");
// Do something with user
}
}
}