Package org.forgerock.util.promise
An implementation of the
Promise API in Java. Promises provide a simple
API for chaining together asynchronous tasks and result transformation.
Example using CREST:
final ConnectionFactory server = getConnectionFactory();
final AtomicReference<Connection> connectionHolder = new AtomicReference<Connection>();
final Promise<Resource, ResourceException> promise = server.getConnectionAsync()
.thenAsync(new AsyncFunction<Connection, Resource, ResourceException>() {
// Read resource.
public Promise<Resource, ResourceException> apply(final Connection connection)
throws ResourceException {
connectionHolder.set(connection); // Save connection for later.
return connection.readAsync(ctx(), Requests.newReadRequest("users/1"));
}
}).thenAsync(new AsyncFunction<Resource, Resource, ResourceException>() {
// Update resource.
public Promise<Resource, ResourceException> apply(final Resource user) throws ResourceException {
return connectionHolder.get().updateAsync(ctx(),
Requests.newUpdateRequest("users/1", userAliceWithIdAndRev(1, 1)));
}
}).then(new SuccessHandler<Resource>() {
// Check updated resource.
public void handleResult(final Resource user) {
// Update successful!
}
}).thenAlways(new Runnable() {
// Close the connection.
public void run() {
final Connection connection = connectionHolder.get();
if (connection != null) {
connection.close();
}
}
});
- See Also:
- Promises/A+
-
Interface Summary Interface Description ExceptionHandler<E> A completion handler for consuming exceptions which occur during the execution of asynchronous tasks.Promise<V,E extends Exception> APromiserepresents the result of an asynchronous task.ResultHandler<V> A completion handler for consuming the results of asynchronous tasks.RuntimeExceptionHandler A completion handler for consuming runtime exceptions which occur during the execution of asynchronous tasks. -
Class Summary Class Description PromiseImpl<V,E extends Exception> An implementation ofPromisewhich can be used as is, or as the basis for more complex asynchronous behavior.Promises Utility methods for creating and composingPromises.Promises.Results Ordered list of joined asynchronous results. -
Exception Summary Exception Description NeverThrowsException TheNeverThrowsExceptionclass is an uninstantiable placeholder exception which should be used for indicating that aFunctionorAsyncFunctionnever throws an exception (i.e.