Interface CommandDispatcher

All Superinterfaces:
DescribableComponent
All Known Implementing Classes:
ContextAwareCommandDispatcher

public interface CommandDispatcher extends DescribableComponent
Component that dispatches commands to a CommandGateway in a predefined context. This makes the CommandDispatcher the preferred way to send commands from within another message handling method.

The commands will be dispatched in the context this dispatcher was created for. You can construct one through the forContext(ProcessingContext).

When using annotation-based @MessageHandler-methods and you have declared an argument of type CommandDispatcher, the dispatcher will automatically be injected by the CommandDispatcherParameterResolverFactory.

As this component is ProcessingContext-scoped, it is not retrievable from the Configuration.

Since:
5.0.0
Author:
Steven van Beelen
  • Field Details

  • Method Details

    • forContext

      static CommandDispatcher forContext(@Nonnull ProcessingContext context)
      Creates a dispatcher for the given ProcessingContext.

      You can use this dispatcher only for the context it was created for. There is no harm in using this method more than once with the same context, as the same dispatcher will be returned.

      Parameters:
      context - The ProcessingContext to create the dispatcher for.
      Returns:
      The command dispatcher specific for the given context.
    • send

      default <R> CompletableFuture<R> send(@Nonnull Object command, @Nonnull Class<R> expectedType)
      Sends the given command and returns a CompletableFuture immediately, without waiting for the command to execute.

      The caller will therefore not receive any immediate feedback on the command's execution. Instead, hooks can be added to the returned CompletableFuture to react on success or failure of command execution.

      Note that this operation expects the CommandBus to use new threads for command execution.

      The given command is wrapped as the payload of the CommandMessage that is eventually posted on the CommandBus, unless the command already implements Message. In that case, a CommandMessage is constructed from that message's payload and Metadata.

      Type Parameters:
      R - The generic type of the expected response.
      Parameters:
      command - The command payload or CommandMessage to send.
      expectedType - The expected result type.
      Returns:
      A CompletableFuture that will be resolved successfully or exceptionally based on the eventual command execution result.
    • send

      CommandResult send(@Nonnull Object command)
      Sends the given command and returns a CommandResult immediately, without waiting for the command to execute.

      The caller will therefore not receive any immediate feedback on the command's execution. Instead, hooks can be added to the returned CommandResult to react on success or failure of command execution. A shorthand to retrieve a CompletableFuture is available through the CommandResult.getResultMessage() operation.

      Note that this operation expects the CommandBus to use new threads for command execution.

      The given command is wrapped as the payload of the CommandMessage that is eventually posted on the CommandBus, unless the command already implements Message. In that case, a CommandMessage is constructed from that message's payload and Metadata.

      Parameters:
      command - The command payload or CommandMessage to send.
      Returns:
      A command result success and failure hooks can be registered. The CommandResult.getResultMessage() serves as a shorthand to retrieve the response.
    • send

      CommandResult send(@Nonnull Object command, @Nonnull Metadata metadata)
      Sends the given command with the given metadata and returns a CommandResult immediately, without waiting for the command to execute.

      The caller will therefore not receive any immediate feedback on the command's execution. Instead, hooks can be added to the returned CommandResult to react on success or failure of command execution. A shorthand to retrieve a CompletableFuture is available through the CommandResult.getResultMessage() operation.

      Note that this operation expects the CommandBus to use new threads for command execution.

      The given command and metadata are wrapped as the payload of the CommandMessage that is eventually posted on the CommandBus, unless the command already implements Message. In that case, a CommandMessage is constructed from that message's payload and Metadata. The provided metadata is attached afterward in this case.

      Parameters:
      command - The command payload or CommandMessage to send.
      metadata - Metadata that must be registered with the command.
      Returns:
      A command result success and failure hooks can be registered. The CommandResult.getResultMessage() serves as a shorthand to retrieve the response.