Class InterceptingCommandBus

java.lang.Object
org.axonframework.messaging.commandhandling.interception.InterceptingCommandBus
All Implemented Interfaces:
DescribableComponent, CommandBus, CommandHandlerRegistry<CommandBus>

public class InterceptingCommandBus extends Object implements CommandBus
A CommandBus wrapper that supports both MessageHandlerInterceptors and MessageDispatchInterceptors. Actual dispatching and handling of commands is done by a delegate.

This InterceptingCommandBus is typically registered as a decorator and automatically kicks in whenever CommandMessage specific MessageHandlerInterceptors or any MessageDispatchInterceptors are present.

Since:
5.0.0
Author:
Allad Buijze, Simon Zambrovski, Steven van Beelen
  • Field Details

    • DECORATION_ORDER

      public static final int DECORATION_ORDER
      The order in which the InterceptingCommandBus is applied as a decorator to the CommandBus.

      As such, any decorator with a lower value will be applied to the delegate, and any higher value will be applied to the InterceptingCommandBus itself. Using the same value can either lead to application of the decorator to the delegate or the InterceptingCommandBus, depending on the order of registration.

      The order of the InterceptingCommandBus is set to Integer.MIN_VALUE + 100 to ensure it is applied very early in the configuration process, but not the earliest to allow for other decorators to be applied.

      See Also:
  • Constructor Details

    • InterceptingCommandBus

      public InterceptingCommandBus(@Nonnull CommandBus delegate, @Nonnull List<MessageHandlerInterceptor<? super CommandMessage>> handlerInterceptors, @Nonnull List<MessageDispatchInterceptor<? super CommandMessage>> dispatchInterceptors)
      Constructs a InterceptingCommandBus, delegating dispatching and handling logic to the given delegate. The given handlerInterceptors are wrapped around the command handlers when subscribing. The given dispatchInterceptors are invoked before dispatching is provided to the given delegate.
      Parameters:
      delegate - The delegate CommandBus that will handle all dispatching and handling logic.
      handlerInterceptors - The interceptors to invoke before handling a command and if present on the command result.
      dispatchInterceptors - The interceptors to invoke before dispatching a command and on the command result.
  • Method Details

    • subscribe

      public InterceptingCommandBus subscribe(@Nonnull QualifiedName name, @Nonnull CommandHandler commandHandler)
      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.
      commandHandler - The handler instance that handles commands for the given name.
      Returns:
      This registry for fluent interfacing.
    • 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.
    • 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.