Class SimpleMessageBus

  • All Implemented Interfaces:
    MessageBus

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

      • SimpleMessageBus

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

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

        public SimpleMessageBus​(@Nonnull
                                java.util.concurrent.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 Detail

      • 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
                                java.lang.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
                                  java.lang.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
      • dispatchMessage

        protected <T> void dispatchMessage​(@Nonnull
                                           java.lang.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