Class RecordingCommandBus

java.lang.Object
org.axonframework.test.util.RecordingCommandBus
All Implemented Interfaces:
DescribableComponent, CommandBus, CommandHandlerRegistry<CommandBus>

@Internal 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 CompletableFuture<CommandResultMessage> dispatch(@Nonnull CommandMessage command, @Nullable ProcessingContext processingContext)
      Description copied from interface: CommandBus
      Dispatch the given command to the command handler subscribed to the given command's name. The name is typically deferred from the Message.type(), which contains a MessageType.qualifiedName().
      Specified by:
      dispatch in interface CommandBus
      Parameters:
      command - The command to dispatch.
      processingContext - The processing context under which the command is being published (can be null).
      Returns:
      The CompletableFuture providing the result of the command, once finished.
    • subscribe

      public CommandBus subscribe(@Nonnull QualifiedName name, @Nonnull CommandHandler handler)
      Description copied from interface: CommandHandlerRegistry
      Subscribe the given handler for commands of the given name.

      If a subscription already exists for the 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 CommandHandlerRegistry<CommandBus>
      Parameters:
      name - The name the given commandHandler can handle.
      handler - The handler instance that handles commands for the given name.
      Returns:
      This registry for fluent interfacing.
    • 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(CommandHandler 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 boolean isSubscribed(QualifiedName commandName, CommandHandler commandHandler)
      Indicates whether the given commandHandler is subscribed to commands of the given commandType on this command bus.
      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<QualifiedName,CommandHandler> 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.
    • describeTo

      public void describeTo(@Nonnull ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.