Package it.tidalwave.messagebus.spi
Class SimpleMessageBus
- java.lang.Object
-
- it.tidalwave.messagebus.spi.SimpleMessageBus
-
- All Implemented Interfaces:
MessageBus
@ThreadSafe public class SimpleMessageBus extends java.lang.Object implements MessageBus
A partial implementation ofMessageBus
.- Author:
- Fabrizio Giudici
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface it.tidalwave.messagebus.MessageBus
MessageBus.Listener<TOPIC>
-
-
Constructor Summary
Constructors Constructor Description SimpleMessageBus()
Creates a new instance with aSimpleAsyncMessageDelivery
strategy for delivery.SimpleMessageBus(java.util.concurrent.Executor executor)
Creates a new instance given an executor and aSimpleAsyncMessageDelivery
strategy for delivery.SimpleMessageBus(java.util.concurrent.Executor executor, MessageDelivery messageDelivery)
Creates a new instance given an executor and a strategy for delivery.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <TOPIC> void
dispatchMessage(java.lang.Class<TOPIC> topic, TOPIC message)
Dispatches a message.<Topic> void
publish(java.lang.Class<Topic> topic, Topic message)
Publishes the given message and topic.<Topic> void
publish(Topic message)
Publishes the given event.<Topic> void
subscribe(java.lang.Class<Topic> topic, MessageBus.Listener<Topic> listener)
Subscribes aMessageBus.Listener
to a topic.void
unsubscribe(MessageBus.Listener<?> listener)
Unsubscribes aMessageBus.Listener
.
-
-
-
Constructor Detail
-
SimpleMessageBus
public SimpleMessageBus()
Creates a new instance with aSimpleAsyncMessageDelivery
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 aSimpleAsyncMessageDelivery
strategy for delivery.- Parameters:
executor
- theExecutor
-
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
- theExecutor
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 interfaceMessageBus
- 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 interfaceMessageBus
- Type Parameters:
Topic
- the static type of the topic- Parameters:
topic
- the topicmessage
- the message
-
subscribe
public <Topic> void subscribe(@Nonnull java.lang.Class<Topic> topic, @Nonnull MessageBus.Listener<Topic> listener)
Subscribes aMessageBus.Listener
to a topic.- Specified by:
subscribe
in interfaceMessageBus
- Type Parameters:
Topic
- the static type of the topic- Parameters:
topic
- the topiclistener
- the listener
-
unsubscribe
public void unsubscribe(@Nonnull MessageBus.Listener<?> listener)
Unsubscribes aMessageBus.Listener
.- Specified by:
unsubscribe
in interfaceMessageBus
- Parameters:
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 topicmessage
- the message
-
-