Interface EventManager
An event manager allows users to publish events to previously registered listeners. The modular style allows plugins or modules to hook and unhook into/from the system dynamically. It is not designed for inter-process communication.
To receive an event a listener must:
- Define a method taking a single argument, the
Eventit wants to subscribe to. - Mark that method with a
EventListenerannotation. - Pass the listener class instance to
registerListener(Object).
Note: event execution is always a blocking operation, NEVER should an event listener receive event notifications simultaneously. By default, no event listener will be called when any event publish is ongoing. Other implementations are free to change this behaviour as long as there are no calls to the same event listener simultaneously.
- Since:
- 4.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<T extends Event>
TCalls the given event to the given channel, only triggering the event listeners which are specifically listening to the given channel unless the channel is *.default <T extends Event>
TcallEvent(T event) Calls the given event to the * channel, triggering all event listeners which are listening to it.registerListener(@NonNull Class<?> listenerClass) Registers all methods in the given listener class which are annotated withEventListenerand are taking only one argument with a subtype ofEvent.registerListener(@NonNull Object listener) Registers all methods in the given listener class which are annotated withEventListenerand are taking only one argument with a subtype ofEvent.default @NonNull EventManagerregisterListeners(Object @NonNull ... listeners) Registers all listeners which are in the given listener classes individually to this event manager.unregisterListener(Object @NonNull ... listeners) Unregisters all listeners which were registered in each of the given listener classes.unregisterListeners(@NonNull ClassLoader classLoader) Unregisters all listeners in classes which were loaded by the given class loader.
-
Method Details
-
unregisterListeners
Unregisters all listeners in classes which were loaded by the given class loader.- Parameters:
classLoader- the loader based onm which to unregister listeners.- Returns:
- the same event manager as used to call the method, for chaining.
- Throws:
NullPointerException- if class loader is null.
-
unregisterListener
Unregisters all listeners which were registered in each of the given listener classes.- Parameters:
listeners- the classes to unregister all listeners of.- Returns:
- the same event manager as used to call the method, for chaining.
- Throws:
NullPointerException- if any listener instance is null.
-
callEvent
Calls the given event to the * channel, triggering all event listeners which are listening to it.This method call is equivalent to
callEvent("*", event).- Type Parameters:
T- the type of the event.- Parameters:
event- the event to call.- Returns:
- the same event as used to call the method, after processing.
- Throws:
NullPointerException- if the given event is null.EventListenerException- if any listener threw an exception while processing the event.
-
callEvent
Calls the given event to the given channel, only triggering the event listeners which are specifically listening to the given channel unless the channel is *.- Type Parameters:
T- the type of the event.- Parameters:
channel- the specific channel to call the listeners on.event- the event to call.- Returns:
- the same event as used to call the method, after processing.
- Throws:
NullPointerException- if the given channel or event is null.EventListenerException- if any listener threw an exception while processing the event.
-
registerListener
Registers all methods in the given listener class which are annotated withEventListenerand are taking only one argument with a subtype ofEvent. The instance the constructed event listeners are bound to are created by requesting it from the default external injection layer.This method accepts public, protected, default (package) access, and private methods but will not include inherited methods at all.
All methods which are not annotated with
EventListener, are static and are not taking one or more arguments are silently ignored.- Parameters:
listenerClass- the class to create an instance of and register all listeners in.- Returns:
- the same event manager as used to call the method, for chaining.
- Throws:
NullPointerException- if the given listener class is null.IllegalArgumentException- if an event listener target doesn't take an event as it's first argument.
-
registerListener
Registers all methods in the given listener class which are annotated withEventListenerand are taking only one argument with a subtype ofEvent.This method accepts public, protected, default (package) access, and private methods but will not include inherited methods at all.
All methods which are not annotated with
EventListener, are static and are not taking one or more arguments are silently ignored.- Parameters:
listener- the instance of the listener to register the methods in.- Returns:
- the same event manager as used to call the method, for chaining.
- Throws:
NullPointerException- if the given listener is null.IllegalArgumentException- if an event listener target doesn't take an event as it's first argument.
-
registerListeners
Registers all listeners which are in the given listener classes individually to this event manager.- Parameters:
listeners- the listeners to register.- Returns:
- the same event manager as used to call the method, for chaining.
- Throws:
NullPointerException- if one of the given listeners is null.IllegalArgumentException- if an event listener target doesn't take an event as it's only argument.- See Also:
-