Skip to content

Listeners

Definition

Extend the EventListener<T> class.

public class ExampleListener extends EventListener<MessageEvent> {

    public ExampleListener() {
        super(Target.fine(MessageEvent.class));
    }

    @Override
    public void invoke(MessageEvent event) {
        //...
    }
}

LambdaEventListener provides a concise way to create a listener.

new LambdaEventListener<>(
    Target.fine(MessageEvent.class),
    event -> {/*...*/}
)

Registration

Listeners can be registered to a subsciber.

EventBus eventBus = new EventBus();
Subscriber subscriber = new Subscriber();
eventBus.subscribe(subscriber);

// Assuming you've already defined a listener
subscriber.registerListener(new ExampleListener());

Or directly to an event bus.

EventBus eventBus = new EventBus();

// Assuming you've already defined a listener
eventBus.register(new ExampleListener());

Listeners can be registered to a subsciber.

EventBus eventBus = new EventBus();
Subscriber subscriber = new Subscriber();
eventBus.subscribe(subscriber);

subscriber.registerListener(
    new LambdaEventListener<>(
        Target.fine(MessageEvent.class),
        event -> {/*...*/}
    )
);

Or directly to an event bus.

EventBus eventBus = new EventBus();

eventBus.register(
    new LambdaEventListener<>(
        Target.fine(MessageEvent.class),
        event -> {/*...*/}
    )
);