Class EventBus

java.lang.Object
tv.phantombot.event.EventBus

public final class EventBus extends Object
Sends events to downstream subscribers

To subscribe to an event, a Java class must be non-static, implement Listener, have a public method for each desired event with the signature public void methodName(EventClass event) where EventClass is the class of the event to be captured, have the @net.engio.mbassy.listener.Handler annotation applied to each event handler method, and must call register(Listener) for each instance of the class that should receive events

The event bus will deliver each event to all subscribers of the matching event class, as well as subscribers of any event class in the parent chain going back up to the Event base class. For example: subscribing to JVMEvent will receive all events in the tv.phantombot.event.jvm package, as it is the base event for the package

  • Method Details

    • instance

      public static EventBus instance()
      Singleton method
      Returns:
      An instance of eventbus
    • register

      public void register(Listener listener)
      Registers an instance of a class implementing Listener to receive events
      Parameters:
      listener - An instance to register to receive events
    • unregister

      public void unregister(Listener listener)
      Deregisters an instance of a class implementing Listener to no longer receive events
      Parameters:
      listener - An instance to deregister to no longer receive events
    • post

      public void post(Event event)
      Performs a blocking publish of an event to the relevant subscribers

      This method should only be used in extremely rare circumstances. Most publishes should be done using postAsync(Event)

      Parameters:
      event - An event to publish
    • postAsync

      public void postAsync(Event event)
      Publishes an event to the relevant subscribers on a separate thread
      Parameters:
      event - An event to publish