-
Notifications
You must be signed in to change notification settings - Fork 0
Listeners
Pieter edited this page Jun 28, 2024
·
6 revisions
To create a listener, you create a class and implement the Listener
interface.
- Create a new class.
- Implement the
Listener
interface. - Create a method that accepts an event as a parameter.
- Add the
@EventListener
annotation to the method. - Done.
public class MyListener implements Listener {
@EventListener
public void onMyEvent(MyEvent event) {
System.out.println(event.getMessage());
}
}
To subscribe or unsubscribe a listener, you use an instance of Sees
and use the subscribe
or unsubscribe
method.
- Get the default instance of
Sees
(or create a new instance). - Subscribe or unsubscribe a listener.
- Done.
public class Main {
public static void main(String[] args) {
// Get the default instance of `Sees`.
Sees sees = Sees.getInstance();
MyListener listener = new MyListener();
// Subscribe a listener.
sees.subscribe(listener);
// Unsubscribe a listener.
sees.unsubscribe(listener);
}
}