public class DistributedCommandBus extends Object implements CommandBus, Distributed<CommandBus>, Lifecycle
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 | Class and Description |
---|---|
static class |
DistributedCommandBus.Builder
Builder class to instantiate a
DistributedCommandBus . |
Lifecycle.LifecycleHandler, Lifecycle.LifecycleRegistry
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 . |
Modifier | Constructor and Description |
---|---|
protected |
DistributedCommandBus(DistributedCommandBus.Builder builder)
Instantiate a
DistributedCommandBus based on the fields contained in the DistributedCommandBus.Builder . |
Modifier and Type | Method and Description |
---|---|
static DistributedCommandBus.Builder |
builder()
Instantiate a Builder to be able to create a
DistributedCommandBus . |
void |
disconnect()
Disconnect the command bus for receiving new commands, by unsubscribing all registered command handlers.
|
<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,? super 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.
|
CommandBus |
localSegment()
Return the message bus of type
MessageBus which is regarded as the local segment for this implementation. |
Registration |
registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor)
Registers the given list of dispatch interceptors to the command bus.
|
Registration |
registerHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor)
Register the given
handlerInterceptor . |
void |
registerLifecycleHandlers(Lifecycle.LifecycleRegistry handle)
Registers the activities to be executed in the various phases of an application's lifecycle.
|
CompletableFuture<Void> |
shutdownDispatching()
Shutdown the command bus asynchronously for dispatching commands to other instances.
|
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
.protected DistributedCommandBus(DistributedCommandBus.Builder builder)
DistributedCommandBus
based on the fields contained in the DistributedCommandBus.Builder
.
Will assert that the CommandRouter
, CommandBusConnector
and MessageMonitor
are not
null
, and will throw an AxonConfigurationException
if any of them is null
.
builder
- the DistributedCommandBus.Builder
used to instantiate a DistributedCommandBus
instancepublic static DistributedCommandBus.Builder builder()
DistributedCommandBus
.
The CommandCallback
is defaulted to a LoggingCallback
. The MessageMonitor
is defaulted to
a NoOpMessageMonitor
. The CommandBusSpanFactory
is defaulted to a DefaultCommandBusSpanFactory
backed by a NoOpSpanFactory
. The CommandRouter
and CommandBusConnector
are hard
requirements and as such should be provided.
DistributedCommandBus
public void disconnect()
Phase.INBOUND_COMMAND_CONNECTOR
phase.public CompletableFuture<Void> shutdownDispatching()
Phase.OUTBOUND_COMMAND_CONNECTORS
phase.public void registerLifecycleHandlers(@Nonnull Lifecycle.LifecycleRegistry handle)
Lifecycle
registerLifecycleHandlers
in interface Lifecycle
handle
- the lifecycle instance to register the handlers withLifecycle.LifecycleRegistry.onShutdown(int, Runnable)
,
LifecycleRegistry#onShutdown(int, LifecycleHandler)
,
Lifecycle.LifecycleRegistry.onStart(int, Runnable)
,
LifecycleRegistry#onStart(int, LifecycleHandler)
public <C> void dispatch(@Nonnull 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(@Nonnull CommandMessage<C> command, @Nonnull CommandCallback<? super C,? super 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.
There are no guarantees about the successful completion of command dispatching or handling after the method
returns. 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(@Nonnull String commandName, @Nonnull 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 CommandBus localSegment()
MessageBus
which is regarded as the local segment for this implementation.
Would return the message bus used to dispatch and handle messages in a local environment to bridge the gap in a
distributed set up.
Will call CommandBusConnector.localSegment()
. If this returns an Optional.empty()
, this method
defaults to returning this
as last resort.
localSegment
in interface Distributed<CommandBus>
MessageBus
which is the local segment for this distributed message bus implementationpublic int getLoadFactor()
public void updateLoadFactor(int loadFactor)
CommandRouter
.loadFactor
- the new load factor of this nodepublic Registration registerDispatchInterceptor(@Nonnull MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor)
registerDispatchInterceptor
in interface MessageDispatchInterceptorSupport<CommandMessage<?>>
dispatchInterceptor
- The interceptors to invoke when commands are dispatchedpublic Registration registerHandlerInterceptor(@Nonnull MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor)
MessageHandlerInterceptorSupport
handlerInterceptor
. After registration, the interceptor will be invoked for each
handled Message on the messaging component that it was registered to, prior to invoking the message's handler.registerHandlerInterceptor
in interface MessageHandlerInterceptorSupport<CommandMessage<?>>
handlerInterceptor
- The interceptor to registerCopyright © 2010–2024. All rights reserved.