Class AxonServerCommandBus
- All Implemented Interfaces:
CommandBus,Lifecycle,Distributed<CommandBus>,MessageDispatchInterceptorSupport<CommandMessage<?>>,MessageHandlerInterceptorSupport<CommandMessage<?>>
CommandBus implementation that connects to Axon Server to submit and receive commands and command
responses. Delegates incoming commands to the provided localSegment.- Since:
- 4.0
- Author:
- Marc Gathier
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate anAxonServerCommandBus.Nested classes/interfaces inherited from interface org.axonframework.lifecycle.Lifecycle
Lifecycle.LifecycleHandler, Lifecycle.LifecycleRegistry -
Constructor Summary
ConstructorsConstructorDescriptionInstantiate aAxonServerCommandBusbased on the fields contained in theAxonServerCommandBus.Builder. -
Method Summary
Modifier and TypeMethodDescriptionstatic AxonServerCommandBus.Builderbuilder()Instantiate a Builder to be able to create anAxonServerCommandBus.Disconnect the command bus for receiving commands from Axon Server, 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> commandMessage, CommandCallback<? super C, ? super R> commandCallback) Dispatch the givencommandto the CommandHandler subscribed to the givencommand's name.Return the message bus of typeMessageBuswhich is regarded as the local segment for this implementation.registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor) Register the given DispatchInterceptor.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 Axon Server.voidstart()Start the Axon ServerCommandBusimplementation.subscribe(String commandName, MessageHandler<? super CommandMessage<?>> messageHandler) Subscribe the givenhandlerto commands with the givencommandName.
-
Constructor Details
-
AxonServerCommandBus
Instantiate aAxonServerCommandBusbased on the fields contained in theAxonServerCommandBus.Builder.- Parameters:
builder- theAxonServerCommandBus.Builderused to instantiate aAxonServerCommandBusinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create anAxonServerCommandBus.The
CommandPriorityCalculatoris defaulted toCommandPriorityCalculator.defaultCommandPriorityCalculator(), theTargetContextResolverdefaults to a lambda returning theAxonServerConfiguration.getContext()as the context and theCommandBusSpanFactorydefaults to aDefaultCommandBusSpanFactorybacked by aNoOpSpanFactory. TheExecutorServiceBuilderdefaults toExecutorServiceBuilder.defaultCommandExecutorServiceBuilder(). TheAxonServerConnectionManager, theAxonServerConfiguration, the localCommandBus,Serializerand theRoutingStrategyare a hard requirements and as such should be provided.- Returns:
- a Builder to be able to create a
AxonServerCommandBus
-
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:
-
start
public void start()Start the Axon ServerCommandBusimplementation. -
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> commandMessage, @Nonnull CommandCallback<? super C, ? super R> commandCallback) Description copied from interface:CommandBusDispatch 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:
commandMessage- The Command to dispatchcommandCallback- The callback to invoke when command processing is complete- See Also:
-
subscribe
public Registration subscribe(@Nonnull String commandName, @Nonnull MessageHandler<? super CommandMessage<?>> messageHandler) Description copied from interface:CommandBusSubscribe 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.- Specified by:
subscribein interfaceCommandBus- Parameters:
commandName- The name of the command to subscribe the handler tomessageHandler- 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
Description copied from interface:DistributedReturn 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.- Specified by:
localSegmentin interfaceDistributed<CommandBus>- Returns:
- a
MessageBuswhich is the local segment for this distributed message bus implementation
-
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.
-
registerDispatchInterceptor
public Registration registerDispatchInterceptor(@Nonnull MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor) Description copied from interface:MessageDispatchInterceptorSupportRegister the given DispatchInterceptor. After registration, the interceptor will be invoked for each Message dispatched on the messaging component that it was registered to.- Specified by:
registerDispatchInterceptorin interfaceMessageDispatchInterceptorSupport<CommandMessage<?>>- Parameters:
dispatchInterceptor- The interceptor to register- Returns:
- A Registration, which may be used to deregister the interceptor.
-
disconnect
Disconnect the command bus for receiving commands from Axon Server, by unsubscribing all registered command handlers. This shutdown operation is performed in thePhase.INBOUND_COMMAND_CONNECTORphase.This method will wait for in-progress command handlers to complete, up to the configured
commandInProgressAwaitduration. If handlers do not complete within this time, they will be cancelled. -
shutdownDispatching
Shutdown the command bus asynchronously for dispatching commands to Axon Server. 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
-