Package org.forgerock.util.promise
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:
-
ClassDescriptionA completion handler for consuming exceptions which occur during the execution of asynchronous tasks.The
NeverThrowsException
class is an uninstantiable placeholder exception which should be used for indicating that aFunction
orAsyncFunction
never throws an exception (i.e.APromise
represents the result of an asynchronous task.PromiseImpl<V,E extends Exception> An implementation ofPromise
which can be used as is, or as the basis for more complex asynchronous behavior.Utility methods for creating and composingPromise
s.Ordered list of joined asynchronous results.A completion handler for consuming the results of asynchronous tasks.A completion handler for consuming runtime exceptions which occur during the execution of asynchronous tasks.