org.axonframework.commandhandling.callbacks
Class FutureCallback<R>

java.lang.Object
  extended by org.axonframework.commandhandling.callbacks.FutureCallback<R>
Type Parameters:
R - the type of result of the command handling
All Implemented Interfaces:
Future<R>, CommandCallback<R>

public class FutureCallback<R>
extends Object
implements CommandCallback<R>, Future<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 Summary
FutureCallback()
           
 
Method Summary
 boolean awaitCompletion(long timeout, TimeUnit unit)
          Wait for completion of the command, or for the timeout to expire.
 boolean cancel(boolean mayInterruptIfRunning)
          Always returns false, since command execution cannot be cancelled.
 R get()
          Waits if necessary for the command handling to complete, and then returns its result.
 R get(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.
 R getResult()
          Waits if necessary for the command handling to complete, and then returns its result.
 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.
 boolean isCancelled()
          Always returns false, since command execution cannot be cancelled.
 boolean isDone()
          Indicates whether command handler execution has finished.
 void onFailure(Throwable cause)
          Invoked when command handling execution resulted in an error.
 void onSuccess(R executionResult)
          Invoked when command handling execution was successful.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FutureCallback

public FutureCallback()
Method Detail

onSuccess

public void onSuccess(R executionResult)
Description copied from interface: CommandCallback
Invoked when command handling execution was successful.

Specified by:
onSuccess in interface CommandCallback<R>
Parameters:
executionResult - The result of the command handling execution, if any.

onFailure

public void onFailure(Throwable cause)
Description copied from interface: CommandCallback
Invoked when command handling execution resulted in an error.

Specified by:
onFailure in interface CommandCallback<R>
Parameters:
cause - The exception raised during command handling

get

public R get()
      throws InterruptedException,
             ExecutionException
Waits if necessary for the command handling to complete, and then returns its result.

Specified by:
get in interface Future<R>
Returns:
the result of the command handler execution.
Throws:
InterruptedException - if the current thread is interrupted while waiting
ExecutionException - if the command handler threw an exception

get

public R get(long timeout,
             TimeUnit unit)
      throws TimeoutException,
             InterruptedException,
             ExecutionException
Waits if necessary for at most the given time for the command handling to complete, and then retrieves its result, if available.

Specified by:
get in interface Future<R>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the result of the command handler execution.
Throws:
InterruptedException - if the current thread is interrupted while waiting
TimeoutException - if the wait timed out
ExecutionException - if the command handler threw an exception
See Also:
getResult(long, java.util.concurrent.TimeUnit)

getResult

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

Unlike 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 isDone() method.

Returns:
the result of the command handler execution.
See Also:
get()

getResult

public 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 get(long, java.util.concurrent.TimeUnit), this method will throw the original exception. Only checked exceptions are wrapped in a CommandExecutionException.

If the timeout expired or the thread is interrupted before completion, null is returned. In case of an interrupt, the interrupt flag will have been set back on the thread. To distinguish between an interrupt and a null result, use the isDone()

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.

cancel

public boolean cancel(boolean mayInterruptIfRunning)
Always returns false, since command execution cannot be cancelled.

Specified by:
cancel in interface Future<R>
Parameters:
mayInterruptIfRunning - true if the thread executing the command should be interrupted; otherwise, in-progress tasks are allowed to complete
Returns:
false

isCancelled

public boolean isCancelled()
Always returns false, since command execution cannot be cancelled.

Specified by:
isCancelled in interface Future<R>
Returns:
false

isDone

public boolean isDone()
Indicates whether command handler execution has finished.

Specified by:
isDone in interface Future<R>
Returns:
true if command handler execution has finished, otherwise false.


Copyright © 2010-2016. All Rights Reserved.