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 <TOPIC> void publish​(@Nonnull
                                    TOPIC message)
        Publishes the given event. The topic is the class of the event.
        Specified by:
        publish in interface MessageBus
        Type Parameters:
        TOPIC - the static type of the topic
        Parameters:
        message - the event
      • publish

        public <TOPIC> void publish​(@Nonnull
                                    java.lang.Class<TOPIC> topic,
                                    @Nonnull
                                    TOPIC 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:
        TOPIC - the static type of the topic
        Parameters:
        topic - the topic
        message - the message
      • subscribe

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

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