barchart-util-concurrent
2.0.16 / 2012-12-19T12:07:36.441-0600

com.barchart.util.concurrent
Interface FutureCallback<V,T extends FutureCallback<V,T>>

Type Parameters:
E - The result type
All Superinterfaces:
Future<V>
All Known Implementing Classes:
CancellableFutureNotifier, FutureCallbackList, FutureCallbackTask, FutureNotifier, FutureNotifierBase

public interface FutureCallback<V,T extends FutureCallback<V,T>>
extends Future<V>

Adds listener notification methods to Future to allow immediate notification when a result is available.

Example:

 FutureListener<Employee> listener = new FutureListener<Employee> {
   public void resultAvailable(Future<Employee> result) {
     System.out.println("Got deferred result: " + result.get());
   }
 }
 
 FutureCallback<Employee> result = myDataStore.getEmployee(id);
 result.addResultListener(listener);
 

In this example, getEmployee() returns a FutureCallback object that it will later use to return a result asynchronously. When it has a result, it calls result.succeed(Employee e), which in turn notifies any listeners registered with result.addResultListener(). If the call fails, the owner may call result.fail(Exception ex) to report the error. If a FutureCallback has failed, any future calls to result.get() throw an ExecutionException with the original exception as its cause.

This class is thread-safe.

Author:
jeremy
See Also:
Future, FutureListener, FutureCallbackTask

Method Summary
 T addResultListener(FutureListener<V> listener)
          Register a listener for this deferred result.
 T fail(Throwable error)
          Notify listeners that an error occurred.
 V getUnchecked()
          Get the return value, or null if a checked exception was caught.
 T succeed(V result)
          Notify listeners that a result is available.
 
Methods inherited from interface java.util.concurrent.Future
cancel, get, get, isCancelled, isDone
 

Method Detail

addResultListener

T addResultListener(FutureListener<V> listener)
Register a listener for this deferred result. If the result is already available, the listener may be called synchronously, so you should generally avoid any blocking code in the callback.

Parameters:
listener - The listener object

succeed

T succeed(V result)
Notify listeners that a result is available. Returns the current FutureCallbackTask instance to allow simple synchronous returns when the result is already available:
return new FutureCallbackTask().succeed(result);

Parameters:
result_ - The deferred result
Returns:
This FutureCallbackTask object (for chaining calls)

fail

T fail(Throwable error)
Notify listeners that an error occurred. Returns the current FutureCallbackTask instance to allow simple synchronous returns when an error has already occurred:
return new FutureCallbackTask().fail(exception);

Parameters:
error_ - The deferred error

getUnchecked

V getUnchecked()
Get the return value, or null if a checked exception was caught.


barchart-util-concurrent
2.0.16 / 2012-12-19T12:07:36.441-0600

Copyright © 2011-2012 Barchart, Inc.. All Rights Reserved.