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:
  • Class
    Description
    A 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 a Function or AsyncFunction never throws an exception (i.e.
    Promise<V, E extends Exception>
    A Promise represents the result of an asynchronous task.
    PromiseImpl<V, E extends Exception>
    An implementation of Promise which can be used as is, or as the basis for more complex asynchronous behavior.
    Utility methods for creating and composing Promises.
    IterateOverBuilder provides a mechanism for iterating over a stream or iterable of promise suppliers in search of the first to match the supplied predicate.
    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.