java.lang.Object
it.tidalwave.messagebus.spi.SimpleMessageBus
- All Implemented Interfaces:
MessageBus
An implementation of
MessageBus. Messages are delivered by means of an Executor, according to a MessageDelivery strategy. A listener
subscribed to a topic receives the messages of that class and of its subclasses (or implementations, if the topic is an interface); if a listener throws an
exception, it is logged and the message is delivered to the other listeners anyway. This class is thread-safe.- Author:
- Fabrizio Giudici
-
Nested Class Summary
Nested classes/interfaces inherited from interface it.tidalwave.messagebus.MessageBus
MessageBus.Listener<T> -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance with aSimpleAsyncMessageDeliverystrategy for delivery.SimpleMessageBus(Executor executor) Creates a new instance given an executor and aSimpleAsyncMessageDeliverystrategy for delivery.SimpleMessageBus(Executor executor, MessageDelivery messageDelivery) Creates a new instance given an executor and a strategy for delivery. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> voiddispatchMessage(Class<T> topic, T message) Dispatches a message to all the listeners subscribed to its topic, or to a supertype of it.<T> voidPublishes the given message and topic.<T> voidpublish(T message) Publishes the given event.<T> voidsubscribe(Class<T> topic, MessageBus.Listener<T> listener) Subscribes aMessageBus.Listenerto a topic.voidunsubscribe(MessageBus.Listener<?> listener) Unsubscribes aMessageBus.Listenerfrom all the topics it was subscribed to.
-
Constructor Details
-
SimpleMessageBus
public SimpleMessageBus()Creates a new instance with aSimpleAsyncMessageDeliverystrategy for delivery. It will use its own pool of 10 threads. -
SimpleMessageBus
Creates a new instance given an executor and aSimpleAsyncMessageDeliverystrategy for delivery.- Parameters:
executor- theExecutor
-
SimpleMessageBus
Creates a new instance given an executor and a strategy for delivery.- Parameters:
executor- theExecutormessageDelivery- 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:
publishin interfaceMessageBus- Type Parameters:
T- the static type of the topic- Parameters:
message- the event
-
publish
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:
publishin interfaceMessageBus- Type Parameters:
T- the static type of the topic- Parameters:
topic- the topicmessage- the message
-
subscribe
Subscribes aMessageBus.Listenerto a topic.- Specified by:
subscribein interfaceMessageBus- Type Parameters:
T- the static type of the topic- Parameters:
topic- the topiclistener- the listener
-
unsubscribe
Unsubscribes aMessageBus.Listenerfrom all the topics it was subscribed to.- Specified by:
unsubscribein interfaceMessageBus- Parameters:
listener- the listener
-
dispatchMessage
Dispatches a message to all the listeners subscribed to its topic, or to a supertype of it. It is called by theMessageDeliverystrategy.- Type Parameters:
T- the static type of the topic- Parameters:
topic- the dynamic type of the topicmessage- the message
-