Class FutureCallback<C,R>

java.lang.Object
java.util.concurrent.CompletableFuture<CommandResultMessage<? extends R>>
org.axonframework.commandhandling.callbacks.FutureCallback<C,R>
Type Parameters:
C - The type of payload of the dispatched command
R - the type of result of the command handling
All Implemented Interfaces:
CompletionStage<CommandResultMessage<? extends R>>, Future<CommandResultMessage<? extends R>>, CommandCallback<C,R>

public class FutureCallback<C,R> extends CompletableFuture<CommandResultMessage<? extends R>> implements CommandCallback<C,R>
Command Handler Callback that allows the dispatching thread to wait for the result of the callback, using the Future mechanism. This callback allows the caller to synchronize calls when an asynchronous command bus is being used.
Since:
0.6
Author:
Allard Buijze
  • Constructor Details

    • FutureCallback

      public FutureCallback()
  • Method Details

    • onResult

      public void onResult(@Nonnull CommandMessage<? extends C> commandMessage, @Nonnull CommandResultMessage<? extends R> commandResultMessage)
      Description copied from interface: CommandCallback
      Invoked when command handling execution is completed.
      Specified by:
      onResult in interface CommandCallback<C,R>
      Parameters:
      commandMessage - the CommandMessage that was dispatched
      commandResultMessage - the CommandResultMessage of the command handling execution
    • getResult

      public CommandResultMessage<? extends R> getResult()
      Waits if necessary for the command handling to complete, and then returns its result.

      Unlike CompletableFuture.get(long, java.util.concurrent.TimeUnit), this method will throw the original exception. Only checked exceptions are wrapped in a CommandExecutionException.

      If the thread is interrupted while waiting, the interrupt flag is set back on the thread, and null is returned. To distinguish between an interrupt and a null result, use the CompletableFuture.isDone() method.

      Returns:
      the result of the command handler execution.
      See Also:
    • getResult

      public CommandResultMessage<? extends R> getResult(long timeout, TimeUnit unit)
      Waits if necessary for at most the given time for the command handling to complete, and then retrieves its result, if available.

      Unlike CompletableFuture.get(long, java.util.concurrent.TimeUnit), this method will report the original exception from within a CommandResultMessage, rather than throwing an ExecutionException.

      If the timeout expired or the thread is interrupted before completion, the returned CommandResultMessage will contain an InterruptedException or TimeoutException. In case of an interrupt, the interrupt flag will have been set back on the thread.

      Parameters:
      timeout - the maximum time to wait
      unit - the time unit of the timeout argument
      Returns:
      the result of the command handler execution.
    • awaitCompletion

      public boolean awaitCompletion(long timeout, TimeUnit unit)
      Wait for completion of the command, or for the timeout to expire.
      Parameters:
      timeout - The amount of time to wait for command processing to complete
      unit - The unit in which the timeout is expressed
      Returns:
      true if command processing completed before the timeout expired, otherwise false.