Class SimpleMessageBus

java.lang.Object
it.tidalwave.messagebus.spi.SimpleMessageBus
All Implemented Interfaces:
MessageBus

@ThreadSafe public class SimpleMessageBus extends Object implements MessageBus
A partial implementation of MessageBus.
Author:
Fabrizio Giudici
  • Constructor Details

    • SimpleMessageBus

      public SimpleMessageBus()
      Creates a new instance with a SimpleAsyncMessageDelivery strategy for delivery. It will use its own thread pool.
    • SimpleMessageBus

      public SimpleMessageBus(@Nonnull Executor executor)
      Creates a new instance given an executor and a SimpleAsyncMessageDelivery strategy for delivery.
      Parameters:
      executor - the Executor
    • SimpleMessageBus

      public SimpleMessageBus(@Nonnull Executor executor, @Nonnull MessageDelivery messageDelivery)
      Creates a new instance given an executor and a strategy for delivery.
      Parameters:
      executor - the Executor
      messageDelivery - the strategy for delivery
  • Method Details

    • publish

      public <T> void publish(@Nonnull T message)
      Publishes the given event. The topic is the class of the event.
      Specified by:
      publish in interface MessageBus
      Type Parameters:
      T - the static type of the topic
      Parameters:
      message - the event
    • publish

      public <T> void publish(@Nonnull Class<T> topic, @Nonnull T message)
      Publishes the given message and topic. Passing an explicit topic can be useful when dealing with a hierarchy of events (so, perhaps a subclass is passed but the topic is the root of the hierarchy).
      Specified by:
      publish in interface MessageBus
      Type Parameters:
      T - the static type of the topic
      Parameters:
      topic - the topic
      message - the message
    • subscribe

      public <T> void subscribe(@Nonnull Class<T> topic, @Nonnull MessageBus.Listener<T> listener)
      Subscribes a MessageBus.Listener to a topic.
      Specified by:
      subscribe in interface MessageBus
      Type Parameters:
      T - the static type of the topic
      Parameters:
      topic - the topic
      listener - the listener
    • unsubscribe

      public void unsubscribe(@Nonnull MessageBus.Listener<?> listener)
      Unsubscribes a MessageBus.Listener.
      Specified by:
      unsubscribe in interface MessageBus
      Parameters:
      listener - the listener
    • dispatchMessage

      protected <T> void dispatchMessage(@Nonnull Class<T> topic, @Nonnull T message)
      Dispatches a message.
      Type Parameters:
      T - the static type of the topic
      Parameters:
      topic - the dynamic type of the topic
      message - the message