Class DistributedCommandBus
java.lang.Object
org.axonframework.commandhandling.distributed.DistributedCommandBus
- All Implemented Interfaces:
CommandBus,Lifecycle,Distributed<CommandBus>,MessageDispatchInterceptorSupport<CommandMessage<?>>,MessageHandlerInterceptorSupport<CommandMessage<?>>
public class DistributedCommandBus
extends Object
implements CommandBus, Distributed<CommandBus>, Lifecycle
Implementation of a
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.- Since:
- 2.0
- Author:
- Allard Buijze
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aDistributedCommandBus.Nested classes/interfaces inherited from interface org.axonframework.lifecycle.Lifecycle
Lifecycle.LifecycleHandler, Lifecycle.LifecycleRegistry -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe initial load factor of this node when it is registered with theCommandRouter. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aDistributedCommandBusbased on the fields contained in theDistributedCommandBus.Builder. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()Instantiate a Builder to be able to create aDistributedCommandBus.voidDisconnect the command bus for receiving new commands, by unsubscribing all registered command handlers.<C> voiddispatch(CommandMessage<C> command) Dispatch the givencommandto the CommandHandler subscribed to the givencommand's name.<C,R> void dispatch(CommandMessage<C> command, CommandCallback<? super C, ? super R> callback) Dispatch the givencommandto the CommandHandler subscribed to the givencommand's name.intReturns the current load factor of this node.Return the message bus of typeMessageBuswhich is regarded as the local segment for this implementation.registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor) Registers the given list of dispatch interceptors to the command bus.registerHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor) Register the givenhandlerInterceptor.voidRegisters the activities to be executed in the various phases of an application's lifecycle.Shutdown the command bus asynchronously for dispatching commands to other instances.subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler) Subscribe the givenhandlerto commands with the givencommandName.voidupdateLoadFactor(int loadFactor) Updates the load factor of this node compared to other nodes registered with theCommandRouter.
-
Field Details
-
INITIAL_LOAD_FACTOR
public static final int INITIAL_LOAD_FACTORThe initial load factor of this node when it is registered with theCommandRouter.- See Also:
-
-
Constructor Details
-
DistributedCommandBus
Instantiate aDistributedCommandBusbased on the fields contained in theDistributedCommandBus.Builder.Will assert that the
CommandRouter,CommandBusConnectorandMessageMonitorare notnull, and will throw anAxonConfigurationExceptionif any of them isnull.- Parameters:
builder- theDistributedCommandBus.Builderused to instantiate aDistributedCommandBusinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aDistributedCommandBus.The
CommandCallbackis defaulted to aLoggingCallback. TheMessageMonitoris defaulted to aNoOpMessageMonitor. TheCommandBusSpanFactoryis defaulted to aDefaultCommandBusSpanFactorybacked by aNoOpSpanFactory. TheCommandRouterandCommandBusConnectorare hard requirements and as such should be provided.- Returns:
- a Builder to be able to create a
DistributedCommandBus
-
disconnect
public void disconnect()Disconnect the command bus for receiving new commands, by unsubscribing all registered command handlers. This shutdown operation is performed in thePhase.INBOUND_COMMAND_CONNECTORphase. -
shutdownDispatching
Shutdown the command bus asynchronously for dispatching commands to other instances. This process will wait for dispatched commands which have not received a response yet. This shutdown operation is performed in thePhase.OUTBOUND_COMMAND_CONNECTORSphase.- Returns:
- a completable future which is resolved once all command dispatching activities are completed
-
registerLifecycleHandlers
Description copied from interface:LifecycleRegisters the activities to be executed in the various phases of an application's lifecycle. This could either be at startup, shutdown, or both.- Specified by:
registerLifecycleHandlersin interfaceLifecycle- Parameters:
handle- the lifecycle instance to register the handlers with- See Also:
-
dispatch
Description copied from interface:CommandBusDispatch the givencommandto the CommandHandler subscribed to the givencommand'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.- Specified by:
dispatchin interfaceCommandBus- Type Parameters:
C- The payload type of the command to dispatch- Parameters:
command- The Command to dispatch- See Also:
-
dispatch
public <C,R> void dispatch(@Nonnull CommandMessage<C> command, @Nonnull CommandCallback<? super C, ? super R> callback) Dispatch the givencommandto the CommandHandler subscribed to the givencommand'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.- Specified by:
dispatchin interfaceCommandBus- Type Parameters:
C- The payload type of the command to dispatchR- The type of the expected result- Parameters:
command- The Command to dispatchcallback- The callback to invoke when command processing is complete- Throws:
CommandDispatchException- when an error occurs while dispatching the command to a segment- See Also:
-
subscribe
public Registration subscribe(@Nonnull String commandName, @Nonnull MessageHandler<? super CommandMessage<?>> handler) Subscribe the givenhandlerto commands with the givencommandName. 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 newhandlergets the subscription. In the DistributedCommandBus, the handler is subscribed to the local segment only.- Specified by:
subscribein interfaceCommandBus- Parameters:
commandName- The name of the command to subscribe the handler tohandler- The handler instance that handles the given type of command- Returns:
- a handle to unsubscribe the
handler. When unsubscribed it will no longer receive commands.
-
localSegment
Return the message bus of typeMessageBuswhich 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 anOptional.empty(), this method defaults to returningthisas last resort.- Specified by:
localSegmentin interfaceDistributed<CommandBus>- Returns:
- a
MessageBuswhich is the local segment for this distributed message bus implementation
-
getLoadFactor
public int getLoadFactor()Returns the current load factor of this node.- Returns:
- the current load factor
-
updateLoadFactor
public void updateLoadFactor(int loadFactor) Updates the load factor of this node compared to other nodes registered with theCommandRouter.- Parameters:
loadFactor- the new load factor of this node
-
registerDispatchInterceptor
public Registration registerDispatchInterceptor(@Nonnull MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor) Registers the given list of dispatch interceptors to the command bus. All incoming commands will pass through the interceptors at the given order before the command is dispatched toward the command handler.- Specified by:
registerDispatchInterceptorin interfaceMessageDispatchInterceptorSupport<CommandMessage<?>>- Parameters:
dispatchInterceptor- The interceptors to invoke when commands are dispatched- Returns:
- handle to deregister the interceptor
-
registerHandlerInterceptor
public Registration registerHandlerInterceptor(@Nonnull MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor) Description copied from interface:MessageHandlerInterceptorSupportRegister the givenhandlerInterceptor. 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.- Specified by:
registerHandlerInterceptorin interfaceMessageHandlerInterceptorSupport<CommandMessage<?>>- Parameters:
handlerInterceptor- The interceptor to register- Returns:
- A Registration, which may be used to deregister the interceptor.
-