public class DistributedCommandBus extends Object implements CommandBus
CommandBus
that is aware of multiple instances of a CommandBus working together to
spread load. Each "physical" CommandBus instance is considered a "segment" of a conceptual distributed CommandBus.
The DistributedCommandBus relies on a CommandBusConnector
to dispatch commands and replies to different
segments of the CommandBus. Depending on the implementation used, each segment may run in a different JVM.Modifier and Type | Field and Description |
---|---|
static int |
INITIAL_LOAD_FACTOR
The initial load factor of this node when it is registered with the
CommandRouter . |
Constructor and Description |
---|
DistributedCommandBus(CommandRouter commandRouter,
CommandBusConnector connector)
Initializes the command bus with the given
commandRouter and connector . |
DistributedCommandBus(CommandRouter commandRouter,
CommandBusConnector connector,
MessageMonitor<? super CommandMessage<?>> messageMonitor)
Initializes the command bus with the given
commandRouter , connector and messageMonitor . |
Modifier and Type | Method and Description |
---|---|
<C> void |
dispatch(CommandMessage<C> command)
Dispatch the given
command to the CommandHandler subscribed to the given command 's name. |
<C,R> void |
dispatch(CommandMessage<C> command,
CommandCallback<? super C,R> callback)
Dispatch the given
command to the CommandHandler subscribed to the given command 's name. |
int |
getLoadFactor()
Returns the current load factor of this node.
|
Registration |
registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor)
Registers the given list of dispatch interceptors to the command bus.
|
Registration |
subscribe(String commandName,
MessageHandler<? super CommandMessage<?>> handler)
Subscribe the given
handler to commands with the given commandName . |
void |
updateLoadFactor(int loadFactor)
Updates the load factor of this node compared to other nodes registered with the
CommandRouter . |
public static final int INITIAL_LOAD_FACTOR
CommandRouter
.public DistributedCommandBus(CommandRouter commandRouter, CommandBusConnector connector)
commandRouter
and connector
. The
commandRouter
is used to determine the target node for each dispatched command. The connector
performs the actual transport of the message to the destination node.commandRouter
- the service registry that discovers the network of worker nodesconnector
- the connector that connects the different command bus segmentspublic DistributedCommandBus(CommandRouter commandRouter, CommandBusConnector connector, MessageMonitor<? super CommandMessage<?>> messageMonitor)
commandRouter
, connector
and messageMonitor
.
The commandRouter
is used to determine the target node for each dispatched command.
The connector
performs the actual transport of the message to the destination node.
The messageMonitor
is used to monitor incoming messages and their execution result.commandRouter
- the service registry that discovers the network of worker nodesconnector
- the connector that connects the different command bus segmentsmessageMonitor
- the message monitor to notify of incoming messages and their execution resultpublic <C> void dispatch(CommandMessage<C> command)
CommandBus
command
to the CommandHandler subscribed to the given command
's name.
No feedback is given about the status of the dispatching process. Implementations may return immediately after
asserting a valid handler is registered for the given command.dispatch
in interface CommandBus
C
- The payload type of the command to dispatchcommand
- The Command to dispatchGenericCommandMessage.asCommandMessage(Object)
public <C,R> void dispatch(CommandMessage<C> command, CommandCallback<? super C,R> callback)
command
to the CommandHandler subscribed to the given command
's name.
When the command is processed, one of the callback's methods is called, depending on the result of the processing.
When the method returns, the only guarantee provided by the CommandBus implementation is that the command has
been successfully received. Implementations are highly recommended to perform basic validation of the command
before returning from this method call.
Implementations must start a UnitOfWork when before dispatching the command, and either commit or rollback after
a successful or failed execution, respectively.dispatch
in interface CommandBus
C
- The payload type of the command to dispatchR
- The type of the expected resultcommand
- The Command to dispatchcallback
- The callback to invoke when command processing is completeCommandDispatchException
- when an error occurs while dispatching the command to a segmentGenericCommandMessage.asCommandMessage(Object)
public Registration subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler)
handler
to commands with the given commandName
.
If a subscription already exists for the given name, the behavior is undefined. Implementations may throw an
Exception to refuse duplicate subscription or alternatively decide whether the existing or new
handler
gets the subscription.
In the DistributedCommandBus, the handler is subscribed to the local segment only.subscribe
in interface CommandBus
commandName
- The name of the command to subscribe the handler tohandler
- The handler instance that handles the given type of commandhandler
. When unsubscribed it will no longer receive commands.public int getLoadFactor()
public void updateLoadFactor(int loadFactor)
CommandRouter
.loadFactor
- the new load factor of this nodepublic Registration registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor)
dispatchInterceptor
- The interceptors to invoke when commands are dispatchedCopyright © 2010–2017. All rights reserved.