A pipeline of ClosingFutures is a tree of steps. Each step represents either an
asynchronously-computed intermediate value, or else an exception that indicates the failure or
cancellation of the operation so far. The only way to extract the value or exception from a step
is by declaring that step to be the last step of the pipeline. Nevertheless, we refer to the
"value" of a successful step or the "result" (value or exception) of any step.
ListenableFuture.
ClosingFuture pipeline from a
callable block that may capture objects for later closing. To start a pipeline from a ListenableFuture that doesn't create resources that should be closed later, you can use from(ListenableFuture) instead.
ClosingFuture step can be derived from one or more input ClosingFuture steps in
ways similar to FluentFutures:
A step can be the input to at most one derived step. Once you transform its value, catch its exception, or combine it with others, you cannot do anything else with it, including declare it to be the last step of the pipeline.
transform(ClosingFunction, Executor) or transformAsync(AsyncClosingFunction,
Executor) on the input step.
catching(Class, ClosingFunction,
Executor) or catchingAsync(Class, AsyncClosingFunction, Executor) on the input step.
ClosingFuture from two or more input steps, pass the input steps to whenAllComplete(Iterable) or whenAllSucceed(Iterable) or its overloads.
Future. In addition, a
successfully cancelled step will immediately start closing all objects captured for later closing
by it and by its input steps.
ClosingFuture pipeline must be ended. To end a pipeline, decide whether you want to
close the captured objects automatically or manually.
Future that represents the result of the last step in the pipeline by
calling finishToFuture(). All objects the pipeline has captured for closing will begin
to be closed asynchronously after the returned Future is done: the future
completes before closing starts, rather than once it has finished.
FluentFuture<UserName> userName =
ClosingFuture.submit(
closer -> closer.eventuallyClose(database.newTransaction(), closingExecutor),
executor)
.transformAsync((closer, transaction) -> transaction.queryClosingFuture("..."), executor)
.transform((closer, result) -> result.get("userName"), directExecutor())
.catching(DBException.class, e -> "no user", directExecutor())
.finishToFuture();
In this example, when the userName Future is done, the transaction and the query
result cursor will both be closed, even if the operation is cancelled or fails.
finishToValueAndCloser(ValueAndCloserConsumer, Executor) to get an object that holds the
final result. You then call ValueAndCloser#closeAsync() to close the captured objects.
ClosingFuture.submit(
closer -> closer.eventuallyClose(database.newTransaction(), closingExecutor),
executor)
.transformAsync((closer, transaction) -> transaction.queryClosingFuture("..."), executor)
.transform((closer, result) -> result.get("userName"), directExecutor())
.catching(DBException.class, e -> "no user", directExecutor())
.finishToValueAndCloser(
valueAndCloser -> this.userNameValueAndCloser = valueAndCloser, executor);
// later
try { // get() will throw if the operation failed or was cancelled.
UserName userName = userNameValueAndCloser.get();
// do something with userName
} finally {
userNameValueAndCloser.closeAsync();
}
In this example, when userNameValueAndCloser.closeAsync() is called, the transaction and
the query result cursor will both be closed, even if the operation is cancelled or fails.
Note that if you don't call closeAsync(), the captured objects will not be closed. The
automatic-closing approach described above is safer.
| Modifier and Type | Class and Description |
|---|---|
| public static interface | ClosingFuture.AsyncClosingCallable<
the type of the result V extends @Nullable Object>An operation that computes a |
| public static interface | ClosingFuture.AsyncClosingFunction<
the type of the input to the function T extends @Nullable Object, the type of the result of the function U extends @Nullable Object>A function from an input to a |
| private static class | |
| public static interface | ClosingFuture.ClosingCallable<
the type of the result V extends @Nullable Object>An operation that computes a result. |
| public static interface | ClosingFuture.ClosingFunction<
the type of the input to the function T extends @Nullable Object, the type of the result of the function U extends @Nullable Object>A function from an input to a result. |
| public static class | ClosingFuture.Combiner
A builder of a |
| public static class | ClosingFuture.Combiner2<
the type returned by the first future V1 extends @Nullable Object, the type returned by the second future V2 extends @Nullable Object>A generic |
| public static class | ClosingFuture.Combiner3<
the type returned by the first future V1 extends @Nullable Object, the type returned by the second future V2 extends @Nullable Object, the type returned by the third future V3 extends @Nullable Object>A generic |
| public static class | ClosingFuture.Combiner4<
the type returned by the first future V1 extends @Nullable Object, the type returned by the second future V2 extends @Nullable Object, the type returned by the third future V3 extends @Nullable Object, the type returned by the fourth future V4 extends @Nullable Object>A generic |
| public static class | ClosingFuture.Combiner5<
the type returned by the first future V1 extends @Nullable Object, the type returned by the second future V2 extends @Nullable Object, the type returned by the third future V3 extends @Nullable Object, the type returned by the fourth future V4 extends @Nullable Object, the type returned by the fifth future V5 extends @Nullable Object>A generic |
| public static class | ClosingFuture.DeferredCloser
An object that can capture objects to be closed later, when a |
| public static class | ClosingFuture.Peeker
An object that can return the value of the |
| pack-priv static enum | ClosingFuture.State
The state of a |
| public static class | ClosingFuture.ValueAndCloser<
the type of the value of a successful operation V extends @Nullable Object>An object that holds the final result of an asynchronous |
| public static interface | ClosingFuture.ValueAndCloserConsumer<
the type of the final value of a successful pipeline V extends @Nullable Object>Represents an operation that accepts a |
| Modifier and Type | Field and Description |
|---|---|
| private final ClosingFuture. | |
| private final FluentFuture | |
| private static final Logger | |
| private final AtomicReference |
| Access | Constructor and Description |
|---|---|
| private | |
| private | |
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public boolean | Returns: false if the step could not be cancelled, typically because it has already
completed normally; true otherwisetrue if the thread executing this task should be
interrupted; otherwise, in-progress tasks are allowed to complete, but the step will be
cancelled regardlessAttempts to cancel execution of this step. |
| public <X extends Throwable> ClosingFuture | catching(Class<X>
the exception type that triggers use of exceptionType, ClosingFuture.fallback. The exception
type is matched against this step's exception. "This step's exception" means the cause of
the ExecutionException thrown by Future#get() on the Future
underlying this step or, if get() throws a different kind of exception, that
exception itself. To avoid hiding bugs and other unrecoverable errors, callers should
prefer more specific types, avoiding Throwable.class in particular.the function to be called if this step fails with the expected exception type.
The function's argument is this step's exception. "This step's exception" means the cause
of the fallback,ExecutionException thrown by Future#get() on the Future
underlying this step or, if get() throws a different kind of exception, that
exception itself.the executor that runs executor)fallback if the input failsReturns a new |
| public <X extends Throwable> ClosingFuture | catchingAsync(Class<X>
the exception type that triggers use of exceptionType, ClosingFuture.fallback. The exception
type is matched against this step's exception. "This step's exception" means the cause of
the ExecutionException thrown by Future#get() on the Future
underlying this step or, if get() throws a different kind of exception, that
exception itself. To avoid hiding bugs and other unrecoverable errors, callers should
prefer more specific types, avoiding Throwable.class in particular.the function to be called if this step fails with the expected exception type.
The function's argument is this step's exception. "This step's exception" means the cause
of the fallback,ExecutionException thrown by Future#get() on the Future
underlying this step or, if get() throws a different kind of exception, that
exception itself.the executor that runs executor)fallback if the input failsReturns a new |
| private <X extends Throwable, W extends V> ClosingFuture | catchingAsyncMoreGeneric(Class<X> exceptionType, final ClosingFuture.
|
| private <X extends Throwable, W extends V> ClosingFuture | catchingMoreGeneric(Class<X> exceptionType, final ClosingFuture.
|
| private void | |
| private void | |
| private static void | |
| private boolean | |
| private <U extends @Nullable Object> ClosingFuture | |
| public static <C extends @Nullable Object & @Nullable AutoCloseable> ClosingFuture | eventuallyClosing(ListenableFuture<C>
the future to create the future, final Executor ClosingFuture from. For discussion of the
future's result type C, see DeferredCloser#eventuallyClose(Object,
Executor).the future's result will be closed on this executor closingExecutor)
Deprecated
Creating
Starts a Futures of closeable types is dangerous in general because the
underlying value may never be closed if the Future is canceled after its operation
begins.
ClosingFuture pipeline with a ListenableFuture.
|
| protected void | finalize()
Overrides java. |
| public FluentFuture | Returns: aFuture that represents the final value or exception of the pipelineMarks this step as the last step in the |
| public void | finishToValueAndCloser(final ClosingFuture.
a callback whose method will be called (using consumer,executor) when this
operation is doneMarks this step as the last step in the |
| public static <V extends @Nullable Object> ClosingFuture | |
| private static <C extends @Nullable Object, V extends C> void | provideValueAndCloser(ClosingFuture.
|
| public ListenableFuture | |
| public static <V extends @Nullable Object> ClosingFuture | submit(ClosingFuture.
Starts a |
| public static <V extends @Nullable Object> ClosingFuture | submitAsync(ClosingFuture.
Starts a |
| public String | |
| public <U extends @Nullable Object> ClosingFuture | Returns: the derived steptransforms the value of this step to the value of the derived step function,executor to run the function in executor)Returns a new |
| public <U extends @Nullable Object> ClosingFuture | Returns: the derived steptransforms the value of this step to a function,ClosingFuture with the value of
the derived stepexecutor to run the function in executor)Returns a new |
| public static ClosingFuture. | whenAllComplete(Iterable<? extends ClosingFuture<?>> futures)
Starts specifying how to combine |
| public static ClosingFuture. | whenAllComplete(ClosingFuture<?> future1, ClosingFuture<?>... moreFutures)
Starts specifying how to combine |
| public static ClosingFuture. | whenAllSucceed(Iterable<? extends ClosingFuture<?>> futures)
Starts specifying how to combine |
| public static <V1 extends @Nullable Object, V2 extends @Nullable Object> ClosingFuture. | whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2)
Starts specifying how to combine two |
| public static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object> ClosingFuture. | whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3)
Starts specifying how to combine three |
| public static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object> ClosingFuture. | whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3, ClosingFuture<V4> future4)
Starts specifying how to combine four |
| public static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object, V5 extends @Nullable Object> ClosingFuture. | whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3, ClosingFuture<V4> future4, ClosingFuture<V5> future5)
Starts specifying how to combine five |
| public static ClosingFuture. | whenAllSucceed(ClosingFuture<?> future1, ClosingFuture<?> future2, ClosingFuture<?> future3, ClosingFuture<?> future4, ClosingFuture<?> future5, ClosingFuture<?> future6, ClosingFuture<?>... moreFutures)
Starts specifying how to combine |
| pack-priv CountDownLatch | whenClosedCountDown()
Returns an object that can be used to wait until this objects' deferred closeables have all had
|
| public static <V extends @Nullable Object, U extends @Nullable Object> ClosingFuture. | withoutCloser(final AsyncFunction<V, U>
transforms the value of a function)ClosingFuture step to a ListenableFuture with the value of a derived stepReturns an |