Class SimpleCommandBus
java.lang.Object
org.axonframework.commandhandling.SimpleCommandBus
- All Implemented Interfaces:
CommandBus,MessageDispatchInterceptorSupport<CommandMessage<?>>,MessageHandlerInterceptorSupport<CommandMessage<?>>
- Direct Known Subclasses:
AsynchronousCommandBus
Implementation of the CommandBus that dispatches commands to the handlers subscribed to that specific command's name.
Interceptors may be configured to add processing to commands regardless of their type or name, for example logging,
security (authorization), sla monitoring, etc.
- Since:
- 0.5
- Author:
- Allard Buijze, Martin Tilma
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSimpleCommandBus(SimpleCommandBus.Builder builder) Instantiate aSimpleCommandBusbased on the fields contained in theSimpleCommandBus.Builder. -
Method Summary
Modifier and TypeMethodDescriptionstatic SimpleCommandBus.Builderbuilder()Instantiate a Builder to be able to create aSimpleCommandBus.<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.protected <C,R> void doDispatch(CommandMessage<C> command, CommandCallback<? super C, ? super R> callback) Performs the actual dispatching logic.protected <C,R> void handle(CommandMessage<C> command, MessageHandler<? super CommandMessage<?>> handler, CommandCallback<? super C, ? super R> callback) Performs the actual handling logic.protected <C> CommandMessage<C> intercept(CommandMessage<C> command) Invokes all the dispatch interceptors.registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor) Registers the given list of dispatch interceptors to the command bus.registerHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor) Registers the given interceptor to the command bus.voidsetRollbackConfiguration(RollbackConfiguration rollbackConfiguration) Sets theRollbackConfigurationthat allows you to change when theUnitOfWorkis rolled back.subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler) Subscribe the givenhandlerto commands with givencommandName.
-
Constructor Details
-
SimpleCommandBus
Instantiate aSimpleCommandBusbased on the fields contained in theSimpleCommandBus.Builder.Will assert that the
TransactionManager,MessageMonitorandRollbackConfigurationare notnull, and will throw anAxonConfigurationExceptionif any of them isnull.- Parameters:
builder- theSimpleCommandBus.Builderused to instantiate aSimpleCommandBusinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aSimpleCommandBus.The
TransactionManageris defaulted to aNoTransactionManager, theMessageMonitoris defaulted to aNoOpMessageMonitor, theRollbackConfigurationdefaults to aRollbackConfigurationType.UNCHECKED_EXCEPTIONS, theDuplicateCommandHandlerResolverdefaults toDuplicateCommandHandlerResolution.logAndOverride()and theCommandBusSpanFactorydefaults to aDefaultCommandBusSpanFactorywith aNoOpSpanFactory. TheTransactionManager,MessageMonitorandRollbackConfigurationare hard requirements. Thus setting them tonullwill result in anAxonConfigurationException.- Returns:
- a Builder to be able to create a
SimpleCommandBus
-
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) 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:
command- The Command to dispatchcallback- The callback to invoke when command processing is complete- See Also:
-
intercept
Invokes all the dispatch interceptors.- Type Parameters:
C- The type of payload contained in the CommandMessage- Parameters:
command- The original command being dispatched- Returns:
- The command to actually dispatch
-
doDispatch
protected <C,R> void doDispatch(CommandMessage<C> command, CommandCallback<? super C, ? super R> callback) Performs the actual dispatching logic. The dispatch interceptors must have been invoked at this point.- Type Parameters:
C- The type of payload of the commandR- The type of result expected from the command handler- Parameters:
command- The actual command to dispatch to the handlercallback- The callback to notify of the result
-
handle
protected <C,R> void handle(CommandMessage<C> command, MessageHandler<? super CommandMessage<?>> handler, CommandCallback<? super C, ? super R> callback) Performs the actual handling logic.- Type Parameters:
C- The type of payload of the commandR- The type of result expected from the command handler- Parameters:
command- The actual command to handlehandler- The handler that must be invoked for this commandcallback- The callback to notify of the result
-
subscribe
public Registration subscribe(@Nonnull String commandName, @Nonnull MessageHandler<? super CommandMessage<?>> handler) Subscribe the givenhandlerto commands with givencommandName. If a subscription already exists for the given name, the configuredDuplicateCommandHandlerResolverwill resolve the command handler which should be subscribed.- 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.
-
registerHandlerInterceptor
public Registration registerHandlerInterceptor(@Nonnull MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor) Registers the given interceptor to the command bus. All incoming commands will pass through the registered interceptors at the given order before the command is passed to the handler for processing.- Specified by:
registerHandlerInterceptorin interfaceMessageHandlerInterceptorSupport<CommandMessage<?>>- Parameters:
handlerInterceptor- The interceptor to invoke when commands are handled- Returns:
- handle to deregister the interceptor
-
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
-
setRollbackConfiguration
Sets theRollbackConfigurationthat allows you to change when theUnitOfWorkis rolled back. If not set the,RollbackConfigurationType.UNCHECKED_EXCEPTIONSwill be used, which triggers a rollback on all unchecked exceptions.- Parameters:
rollbackConfiguration- aRollbackConfigurationspecifying when aUnitOfWorkshould be rolled back
-