Class DefaultCommandGateway

java.lang.Object
org.axonframework.commandhandling.gateway.AbstractCommandGateway
org.axonframework.commandhandling.gateway.DefaultCommandGateway
All Implemented Interfaces:
CommandGateway, MessageDispatchInterceptorSupport<CommandMessage<?>>

public class DefaultCommandGateway extends AbstractCommandGateway implements CommandGateway
Default implementation of the CommandGateway interface. It allow configuration of a RetryScheduler and CommandDispatchInterceptors. The Retry Scheduler allows for Command to be automatically retried when a non-transient exception occurs. The Command Dispatch Interceptors can intercept and alter command dispatched on this specific gateway. Typically, this would be used to add gateway specific meta data to the Command.
Since:
2.0
Author:
Allard Buijze
  • Constructor Details

  • Method Details

    • builder

      public static DefaultCommandGateway.Builder builder()
      Instantiate a Builder to be able to create a DefaultCommandGateway.

      The dispatchInterceptors are defaulted to an empty list. The CommandBus is a hard requirement and as such should be provided.

      Returns:
      a Builder to be able to create a DefaultCommandGateway
    • send

      public <C, R> void send(@Nonnull C command, @Nonnull CommandCallback<? super C,? super R> callback)
      Description copied from class: AbstractCommandGateway
      Sends the given command, and invokes the callback when the command is processed.
      Specified by:
      send in interface CommandGateway
      Overrides:
      send in class AbstractCommandGateway
      Type Parameters:
      R - The type of response expected from the command
      Parameters:
      command - The command to dispatch
      callback - The callback to notify with the processing result
    • sendAndWait

      public <R> R sendAndWait(@Nonnull Object command)
      Sends the given command and waits for its execution to complete, or until the waiting thread is interrupted.
      Specified by:
      sendAndWait in interface CommandGateway
      Type Parameters:
      R - The expected type of return value
      Parameters:
      command - The command to send
      Returns:
      The result of the command handler execution
      Throws:
      CommandExecutionException - when command execution threw a checked exception
    • sendAndWait

      public <R> R sendAndWait(@Nonnull Object command, long timeout, @Nonnull TimeUnit unit)
      Sends the given command and waits for its execution to complete, or until the given timeout has expired, or the waiting thread is interrupted.

      When the timeout occurs, or the thread is interrupted, this method returns null.

      Specified by:
      sendAndWait in interface CommandGateway
      Type Parameters:
      R - The expected type of return value
      Parameters:
      command - The command to send
      timeout - The maximum time to wait
      unit - The time unit of the timeout argument
      Returns:
      The result of the command handler execution
      Throws:
      CommandExecutionException - when command execution threw a checked exception
    • send

      public <R> CompletableFuture<R> send(@Nonnull Object command)
      Description copied from interface: CommandGateway
      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.

      Specified by:
      send in interface CommandGateway
      Parameters:
      command - The command to dispatch.
      Returns:
      A CompletableFuture which will be resolved successfully or exceptionally based on the eventual command execution result.
    • registerDispatchInterceptor

      @Nonnull public Registration registerDispatchInterceptor(@Nonnull MessageDispatchInterceptor<? super CommandMessage<?>> dispatchInterceptor)
      Description copied from class: AbstractCommandGateway
      Registers a command dispatch interceptor within a CommandGateway.
      Specified by:
      registerDispatchInterceptor in interface MessageDispatchInterceptorSupport<CommandMessage<?>>
      Overrides:
      registerDispatchInterceptor in class AbstractCommandGateway
      Parameters:
      dispatchInterceptor - To intercept command messages
      Returns:
      a registration which can be used to cancel the registration of given interceptor