Product Selector

Fusion 5.9
    Fusion 5.9

    Subscribing to Events

    For communication between separate system components, Appkit uses an event bus architecture. The event bus handles local events, within the same virtual machine. The event model is quite flexible and allows for any POJO to be transmitted on the event bus. Our current implemention uses Google Guava as the event bus provider.

    Sending event notifications

    To post an event notification onto the event bus, you must obtain a copy of the global event bus and submit an instance of the class that describes the event. In this example, we assume that a Java class named MyEvent captures the event details.

    import com.google.common.eventbus.EventBus;
    import com.google.inject.Inject;
    
    @Inject
    EventBus eventBus;
    
    ...
    
    eventBus.post(new MyEvent());

    Subscribing to events

    To subscribe for an event of a particular type, you create a new subscriber class (or use an anonymous class) that has a single-argument method annotated with the @Subscribe annotation.

    import eventBus.register.Subscribe;
    
    public class MyEventSubscriber {
    
        @Subscribe
        public void newEvent(MyEvent event) {
            // implement your business logic here
        }
    }

    Next, you must register the subscriber on the event bus, so that events of the given type are routed correctly. This is typically done in your application module or controller.

    import com.google.common.eventbus.EventBus;
    import com.google.inject.Inject;
    
    @Inject
    EventBus eventBus;
    
    ...
    
    eventBus.register(new MyEventSubscriber());

    Standard event types

    This types of event are built into the AppKit core framework. You can register your own subscriber to receive notifications of each. See details in our JavaDocs on which attributes are available in each case.

    • twigkit.security.event.LoginEvent: User logs into the system.

    • twigkit.event.ResponseEvent: A search response was retrieved from a data provider.

    In addition, this events are available if activity tracking is enabled:

    • twigkit.event.QueryEvent: A search query was formulated and submitted to a data provider.

    • twigkit.event.ClickEvent: User clicked a link on a page, possibly linked to a search query.