Package org.axonframework.test.utils
Class RecordingCommandBus
java.lang.Object
org.axonframework.test.utils.RecordingCommandBus
- All Implemented Interfaces:
CommandBus,MessageDispatchInterceptorSupport<CommandMessage<?>>,MessageHandlerInterceptorSupport<CommandMessage<?>>
CommandBus implementation that does not perform any actions on subscriptions or dispatched commands, but records
them instead. This implementation is not a stand-in replacement for a mock, but might prove useful in many simple
cases.
- Since:
- 1.1
- Author:
- Allard Buijze
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all the commands recorded by this Command Bus.voidClears all subscribed handlers on this command bus.<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.List<CommandMessage<?>> Returns a list with all commands that have been dispatched by this command bus.Map<String, MessageHandler<? super CommandMessage<?>>> Returns a Map will all Command Names and their Command Handler that have been subscribed to this command bus.<C> booleanisSubscribed(String commandName, MessageHandler<? super CommandMessage<?>> commandHandler) Indicates whether the givencommandHandleris subscribed to commands of the givencommandTypeon this command bus.booleanisSubscribed(MessageHandler<? super CommandMessage<?>> commandHandler) Indicates whether the givencommandHandleris subscribed to this command bus.registerDispatchInterceptor(MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor) Register the given DispatchInterceptor.registerHandlerInterceptor(MessageHandlerInterceptor<? super CommandMessage<?>> handlerInterceptor) Register the givenhandlerInterceptor.voidsetCallbackBehavior(CallbackBehavior callbackBehavior) Sets the instance that defines the behavior of the Command Bus when a command is dispatched with a callback.subscribe(String commandName, MessageHandler<? super CommandMessage<?>> handler) Subscribe the givenhandlerto commands with the givencommandName.
-
Constructor Details
-
RecordingCommandBus
public RecordingCommandBus()
-
-
Method Details
-
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:
-
subscribe
public Registration subscribe(@Nonnull String commandName, @Nonnull MessageHandler<? super CommandMessage<?>> handler) 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 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.
-
clearCommands
public void clearCommands()Clears all the commands recorded by this Command Bus. -
clearSubscriptions
public void clearSubscriptions()Clears all subscribed handlers on this command bus. -
isSubscribed
Indicates whether the givencommandHandleris subscribed to this command bus.- Parameters:
commandHandler- The command handler to verify the subscription for- Returns:
trueif the handler is subscribed, otherwisefalse.
-
isSubscribed
public <C> boolean isSubscribed(String commandName, MessageHandler<? super CommandMessage<?>> commandHandler) Indicates whether the givencommandHandleris subscribed to commands of the givencommandTypeon this command bus.- Type Parameters:
C- The type of command to verify the subscription for- Parameters:
commandName- The name of the command to verify the subscription forcommandHandler- The command handler to verify the subscription for- Returns:
trueif the handler is subscribed, otherwisefalse.
-
getSubscriptions
Returns a Map will all Command Names and their Command Handler that have been subscribed to this command bus.- Returns:
- a Map will all Command Names and their Command Handler
-
getDispatchedCommands
Returns a list with all commands that have been dispatched by this command bus.- Returns:
- a list with all commands that have been dispatched
-
setCallbackBehavior
Sets the instance that defines the behavior of the Command Bus when a command is dispatched with a callback.- Parameters:
callbackBehavior- The instance deciding to how the callback should be invoked.
-
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.
-
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.
-