Class RecordingCommandBus

java.lang.Object
org.axonframework.test.utils.RecordingCommandBus
All Implemented Interfaces:
CommandBus, MessageDispatchInterceptorSupport<CommandMessage<?>>, MessageHandlerInterceptorSupport<CommandMessage<?>>

public class RecordingCommandBus extends Object implements CommandBus
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 Details

    • RecordingCommandBus

      public RecordingCommandBus()
  • Method Details

    • dispatch

      public <C> void dispatch(@Nonnull CommandMessage<C> command)
      Description copied from interface: CommandBus
      Dispatch the given 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.
      Specified by:
      dispatch in interface CommandBus
      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: CommandBus
      Dispatch the given 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.

      Specified by:
      dispatch in interface CommandBus
      Type Parameters:
      C - The payload type of the command to dispatch
      R - The type of the expected result
      Parameters:
      command - The Command to dispatch
      callback - 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: CommandBus
      Subscribe the given 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.

      Specified by:
      subscribe in interface CommandBus
      Parameters:
      commandName - The name of the command to subscribe the handler to
      handler - 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

      public boolean isSubscribed(MessageHandler<? super CommandMessage<?>> commandHandler)
      Indicates whether the given commandHandler is subscribed to this command bus.
      Parameters:
      commandHandler - The command handler to verify the subscription for
      Returns:
      true if the handler is subscribed, otherwise false.
    • isSubscribed

      public <C> boolean isSubscribed(String commandName, MessageHandler<? super CommandMessage<?>> commandHandler)
      Indicates whether the given commandHandler is subscribed to commands of the given commandType on 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 for
      commandHandler - The command handler to verify the subscription for
      Returns:
      true if the handler is subscribed, otherwise false.
    • getSubscriptions

      public Map<String,MessageHandler<? super CommandMessage<?>>> 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

      public List<CommandMessage<?>> 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

      public void setCallbackBehavior(CallbackBehavior callbackBehavior)
      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: MessageDispatchInterceptorSupport
      Register 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:
      registerDispatchInterceptor in interface MessageDispatchInterceptorSupport<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: MessageHandlerInterceptorSupport
      Register the given 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.
      Specified by:
      registerHandlerInterceptor in interface MessageHandlerInterceptorSupport<CommandMessage<?>>
      Parameters:
      handlerInterceptor - The interceptor to register
      Returns:
      A Registration, which may be used to deregister the interceptor.