Interface EventListener<E extends Event>
- Type Parameters:
E- type of events to be handled. Sub-types must also be accepted.
- All Superinterfaces:
AutoCloseable,Closeable
Event or one of its sub-types.
EventListeners are created by a EventListenerCollection which can be specified by a
EventListenerPluginDescriptor.
An EventListener may choose to be be synchronous or asynchronous. Asynchronous listeners are
guaranteed to have the handle(Event) method called on the same Thread (which makes them easier
to implement) and to not block the event bus main Thread. For this reason, listeners are asynchronous by default.
However, in certain cases, it may be necessary to make sure an event is handled before the server continues
execution from the point where the event was published. In these cases, the event listener may override the
isSynchronous() method to return true. In this case, it is guaranteed that the listener's
handle(Event) method is called from the same Thread as the one used to signal the event, hence
execution on the publisher's side only proceeds once the event has been handled.
- Since:
- 2.2.0
-
Method Summary
Modifier and TypeMethodDescriptiondefault voidclose()Close this event listener.default voidHandle an Event.default voidhandle(E event, EventMetaData eventMetaData) Handle an Event.default booleanReturnstrueif this EventListener is synchronous;falseotherwise.
-
Method Details
-
getEventType
- Returns:
- The type
EventListenerof events handled by this event listener.
-
isSynchronous
default boolean isSynchronous()Returnstrueif this EventListener is synchronous;falseotherwise.Synchronous EventListeners'
handle(Event)method is called on the same Thread that signaled the event; and must therefore be Thread-safe. The same instance of an asynchronous EventListener is, however, guaranteed to never be executed simultaneously by multiple Threads.- Returns:
- whether this EventListener is synchronous
-
handle
Handle an Event.- Parameters:
event- to be handled- See Also:
-
handle
Handle an Event.Notice that the event is guaranteed to be of type
Eor a sub-type.If this
EventListener's instance is asynchronous, this method is guaranteed to always be called from the same Thread, otherwise, this method will be called from the same Thread used to signal the event.- Parameters:
event- to be handledeventMetaData- theEventMetaDatacarrying additional information about the event
-
close
Close this event listener.This method may be called from any Thread, so implementations must be Thread-safe.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an IO error occurs
-