Interface EventListener<E extends Event>

Type Parameters:
E - type of events to be handled. Sub-types must also be accepted.
All Superinterfaces:
AutoCloseable, Closeable

public interface EventListener<E extends Event> extends Closeable
A listener of 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 Type
    Method
    Description
    default void
    Close this event listener.
     
    default void
    handle(E event)
    Handle an Event.
    default void
    handle(E event, EventMetaData eventMetaData)
    Handle an Event.
    default boolean
    Returns true if this EventListener is synchronous; false otherwise.
  • Method Details

    • getEventType

      Class<E> getEventType()
      Returns:
      The type EventListener of events handled by this event listener.
    • isSynchronous

      default boolean isSynchronous()
      Returns true if this EventListener is synchronous; false otherwise.

      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

      default void handle(E event)
      Handle an Event.
      Parameters:
      event - to be handled
      See Also:
    • handle

      default void handle(E event, EventMetaData eventMetaData)
      Handle an Event.

      Notice that the event is guaranteed to be of type E or 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 handled
      eventMetaData - the EventMetaData carrying additional information about the event
    • close

      default void close() throws IOException
      Close this event listener.

      This method may be called from any Thread, so implementations must be Thread-safe.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an IO error occurs