- Type Parameters:
V- the type of the value of this step
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.
- A pipeline starts at its leaf step (or steps), which is created from either a callable
block or a
ListenableFuture. - Each other step is derived from one or more input steps. At each step, zero or more objects can be captured for later closing.
- There is one last step (the root of the tree), from which you can extract the final result of the computation. After that result is available (or the computation fails), all objects captured by any of the steps in the pipeline are closed.
Starting a pipeline
Start aClosingFuture 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.
Derived steps
AClosingFuture step can be derived from one or more input ClosingFuture steps in
ways similar to FluentFutures:
- by transforming the value from a successful input step,
- by catching the exception from a failed input step, or
- by combining the results of several input steps.
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.
Transforming
To derive the next step by asynchronously applying a function to an input step's value, calltransform(ClosingFunction, Executor) or transformAsync(AsyncClosingFunction, Executor) on the input step.
Catching
To derive the next step from a failed input step, callcatching(Class, ClosingFunction, Executor) or catchingAsync(Class, AsyncClosingFunction, Executor) on the input step.
Combining
To derive aClosingFuture from two or more input steps, pass the input steps to whenAllComplete(Iterable) or whenAllSucceed(Iterable) or its overloads.
Cancelling
Any step in a pipeline can be cancelled, even after another step has been derived, with the same semantics as cancelling aFuture. In addition, a
successfully cancelled step will immediately start closing all objects captured for later closing
by it and by its input steps.
Ending a pipeline
EachClosingFuture pipeline must be ended. To end a pipeline, decide whether you want to
close the captured objects automatically or manually.
Automatically closing
You can extract aFuture 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();
userName Future is done, the transaction and the query
result cursor will both be closed, even if the operation is cancelled or fails.
Manually closing
If you want to close the captured objects manually, after you've used the final result, callfinishToValueAndCloser(ValueAndCloserConsumer, Executor) to get an object that holds the
final result. You then call ClosingFuture.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();
}
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.
- Since:
- 30.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn operation that computes aClosingFutureof a result.static interfaceA function from an input to aClosingFutureof a result.static interfaceClosingFuture.ClosingCallable<V extends @Nullable Object>An operation that computes a result.static interfaceA function from an input to a result.static classA builder of aClosingFuturestep that is derived from more than one input step.static final classA genericClosingFuture.Combinerthat lets you use a lambda or method reference to combine twoClosingFutures.static final classClosingFuture.Combiner3<V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object>A genericClosingFuture.Combinerthat lets you use a lambda or method reference to combine threeClosingFutures.static final classClosingFuture.Combiner4<V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object>A genericClosingFuture.Combinerthat lets you use a lambda or method reference to combine fourClosingFutures.static final classClosingFuture.Combiner5<V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object, V5 extends @Nullable Object>A genericClosingFuture.Combinerthat lets you use a lambda or method reference to combine fiveClosingFutures.static final classAn object that can capture objects to be closed later, when aClosingFuturepipeline is done.static final classAn object that can return the value of theClosingFutures that are passed towhenAllComplete(Iterable)orwhenAllSucceed(Iterable).static final classClosingFuture.ValueAndCloser<V extends @Nullable Object>An object that holds the final result of an asynchronousClosingFutureoperation and allows the user to close all the closeable objects that were captured during it for later closing.static interfaceRepresents an operation that accepts aClosingFuture.ValueAndCloserfor the last step in aClosingFuturepipeline. -
Method Summary
Modifier and TypeMethodDescriptionbooleancancel(boolean mayInterruptIfRunning) Attempts to cancel execution of this step.<X extends Throwable>
ClosingFuture<V> catching(Class<X> exceptionType, ClosingFuture.ClosingFunction<? super X, ? extends V> fallback, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function to its exception if it is an instance of a given exception type.<X extends Throwable>
ClosingFuture<V> catchingAsync(Class<X> exceptionType, ClosingFuture.AsyncClosingFunction<? super X, ? extends V> fallback, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function that returns aClosingFutureto its exception if it is an instance of a given exception type.static <C extends @Nullable Object & @Nullable AutoCloseable>
ClosingFuture<C> eventuallyClosing(ListenableFuture<C> future, Executor closingExecutor) Deprecated.protected voidfinalize()Marks this step as the last step in theClosingFuturepipeline.voidfinishToValueAndCloser(ClosingFuture.ValueAndCloserConsumer<? super V> consumer, Executor executor) Marks this step as the last step in theClosingFuturepipeline.static <V extends @Nullable Object>
ClosingFuture<V> from(ListenableFuture<V> future) Starts aClosingFuturepipeline with aListenableFuture.Returns a future that finishes when this step does.static <V extends @Nullable Object>
ClosingFuture<V> submit(ClosingFuture.ClosingCallable<V> callable, Executor executor) Starts aClosingFuturepipeline by submitting a callable block to an executor.static <V extends @Nullable Object>
ClosingFuture<V> submitAsync(ClosingFuture.AsyncClosingCallable<V> callable, Executor executor) Starts aClosingFuturepipeline by submitting a callable block to an executor.toString()<U extends @Nullable Object>
ClosingFuture<U> transform(ClosingFuture.ClosingFunction<? super V, U> function, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function to its value.<U extends @Nullable Object>
ClosingFuture<U> transformAsync(ClosingFuture.AsyncClosingFunction<? super V, U> function, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function that returns aClosingFutureto its value.static ClosingFuture.CombinerwhenAllComplete(ClosingFuture<?> future1, ClosingFuture<?>... moreFutures) Starts specifying how to combineClosingFutures into a single pipeline.static ClosingFuture.CombinerwhenAllComplete(Iterable<? extends ClosingFuture<?>> futures) Starts specifying how to combineClosingFutures into a single pipeline.static ClosingFuture.CombinerwhenAllSucceed(ClosingFuture<?> future1, ClosingFuture<?> future2, ClosingFuture<?> future3, ClosingFuture<?> future4, ClosingFuture<?> future5, ClosingFuture<?> future6, ClosingFuture<?>... moreFutures) Starts specifying how to combineClosingFutures into a single pipeline, assuming they all succeed.static <V1 extends @Nullable Object, V2 extends @Nullable Object>
ClosingFuture.Combiner2<V1, V2> whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2) Starts specifying how to combine twoClosingFutures into a single pipeline, assuming they all succeed.static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object>
ClosingFuture.Combiner3<V1, V2, V3> whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3) Starts specifying how to combine threeClosingFutures into a single pipeline, assuming they all succeed.static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object>
ClosingFuture.Combiner4<V1, V2, V3, V4> whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3, ClosingFuture<V4> future4) Starts specifying how to combine fourClosingFutures into a single pipeline, assuming they all succeed.static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object, V5 extends @Nullable Object>
ClosingFuture.Combiner5<V1, V2, V3, V4, V5> whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3, ClosingFuture<V4> future4, ClosingFuture<V5> future5) Starts specifying how to combine fiveClosingFutures into a single pipeline, assuming they all succeed.static ClosingFuture.CombinerwhenAllSucceed(Iterable<? extends ClosingFuture<?>> futures) Starts specifying how to combineClosingFutures into a single pipeline, assuming they all succeed.static <V extends @Nullable Object, U extends @Nullable Object>
ClosingFuture.AsyncClosingFunction<V, U> withoutCloser(AsyncFunction<V, U> function) Returns anClosingFuture.AsyncClosingFunctionthat applies anAsyncFunctionto an input, ignoring the DeferredCloser and returning aClosingFuturederived from the returnedListenableFuture.
-
Method Details
-
submit
public static <V extends @Nullable Object> ClosingFuture<V> submit(ClosingFuture.ClosingCallable<V> callable, Executor executor) Starts aClosingFuturepipeline by submitting a callable block to an executor.- Throws:
RejectedExecutionException- if the task cannot be scheduled for execution
-
submitAsync
public static <V extends @Nullable Object> ClosingFuture<V> submitAsync(ClosingFuture.AsyncClosingCallable<V> callable, Executor executor) Starts aClosingFuturepipeline by submitting a callable block to an executor.- Throws:
RejectedExecutionException- if the task cannot be scheduled for execution- Since:
- 30.1
-
from
Starts aClosingFuturepipeline with aListenableFuture.future's value will not be closed when the pipeline is done even ifVimplementsAutoCloseable. In order to start a pipeline with a value that will be closed when the pipeline is done, usesubmit(ClosingCallable, Executor)instead. -
eventuallyClosing
@Deprecated public static <C extends @Nullable Object & @Nullable AutoCloseable> ClosingFuture<C> eventuallyClosing(ListenableFuture<C> future, Executor closingExecutor) Deprecated.CreatingFutures of closeable types is dangerous in general because the underlying value may never be closed if theFutureis canceled after its operation begins. Consider replacing code that createsListenableFutures of closeable types, including those that pass them to this method, withsubmit(ClosingCallable, Executor)in order to ensure that resources do not leak. Or, to start a pipeline with aListenableFuturethat doesn't create values that should be closed, usefrom(ListenableFuture).Starts aClosingFuturepipeline with aListenableFuture.If
futuresucceeds, its value will be closed (usingclosingExecutor)) when the pipeline is done, even if the pipeline is canceled or fails.Cancelling the pipeline will not cancel
future, so that the pipeline can access its value in order to close it.- Parameters:
future- the future to create theClosingFuturefrom. For discussion of the future's result typeC, seeClosingFuture.DeferredCloser.eventuallyClose(Object, Executor).closingExecutor- the future's result will be closed on this executor
-
whenAllComplete
Starts specifying how to combineClosingFutures into a single pipeline.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of thefutures, or if any has already been finished
-
whenAllComplete
public static ClosingFuture.Combiner whenAllComplete(ClosingFuture<?> future1, ClosingFuture<?>... moreFutures) Starts specifying how to combineClosingFutures into a single pipeline.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of the arguments, or if any has already been finished
-
whenAllSucceed
Starts specifying how to combineClosingFutures into a single pipeline, assuming they all succeed. If any fail, the resulting pipeline will fail.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of thefutures, or if any has already been finished
-
whenAllSucceed
public static <V1 extends @Nullable Object, V2 extends @Nullable Object> ClosingFuture.Combiner2<V1,V2> whenAllSucceed(ClosingFuture<V1> future1, ClosingFuture<V2> future2) Starts specifying how to combine twoClosingFutures into a single pipeline, assuming they all succeed. If any fail, the resulting pipeline will fail.Calling this method allows you to use lambdas or method references typed with the types of the input
ClosingFutures.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of the arguments, or if any has already been finished
-
whenAllSucceed
public static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object> ClosingFuture.Combiner3<V1,V2, whenAllSucceedV3> (ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3) Starts specifying how to combine threeClosingFutures into a single pipeline, assuming they all succeed. If any fail, the resulting pipeline will fail.Calling this method allows you to use lambdas or method references typed with the types of the input
ClosingFutures.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of the arguments, or if any has already been finished
-
whenAllSucceed
public static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object> ClosingFuture.Combiner4<V1,V2, whenAllSucceedV3, V4> (ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3, ClosingFuture<V4> future4) Starts specifying how to combine fourClosingFutures into a single pipeline, assuming they all succeed. If any fail, the resulting pipeline will fail.Calling this method allows you to use lambdas or method references typed with the types of the input
ClosingFutures.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of the arguments, or if any has already been finished
-
whenAllSucceed
public static <V1 extends @Nullable Object, V2 extends @Nullable Object, V3 extends @Nullable Object, V4 extends @Nullable Object, V5 extends @Nullable Object> ClosingFuture.Combiner5<V1,V2, whenAllSucceedV3, V4, V5> (ClosingFuture<V1> future1, ClosingFuture<V2> future2, ClosingFuture<V3> future3, ClosingFuture<V4> future4, ClosingFuture<V5> future5) Starts specifying how to combine fiveClosingFutures into a single pipeline, assuming they all succeed. If any fail, the resulting pipeline will fail.Calling this method allows you to use lambdas or method references typed with the types of the input
ClosingFutures.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of the arguments, or if any has already been finished
-
whenAllSucceed
public static ClosingFuture.Combiner whenAllSucceed(ClosingFuture<?> future1, ClosingFuture<?> future2, ClosingFuture<?> future3, ClosingFuture<?> future4, ClosingFuture<?> future5, ClosingFuture<?> future6, ClosingFuture<?>... moreFutures) Starts specifying how to combineClosingFutures into a single pipeline, assuming they all succeed. If any fail, the resulting pipeline will fail.- Throws:
IllegalStateException- if aClosingFuturehas already been derived from any of the arguments, or if any has already been finished
-
statusFuture
Returns a future that finishes when this step does. Callingget()on the returned future returnsnullif the step is successful or throws the same exception that would be thrown by callingfinishToFuture().get()if this were the last step. Callingcancel()on the returned future has no effect on theClosingFuturepipeline.statusFuturediffers from most methods onClosingFuture: You can make calls tostatusFuturein addition to the call you make tofinishToFuture()or a derivation method on the same instance. This is important because callingstatusFuturealone does not provide a way to close the pipeline. -
transform
public <U extends @Nullable Object> ClosingFuture<U> transform(ClosingFuture.ClosingFunction<? super V, U> function, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function to its value. The function can use aClosingFuture.DeferredCloserto capture objects to be closed when the pipeline is done.If this
ClosingFuturefails, the function will not be called, and the derivedClosingFuturewill be equivalent to this one.If the function throws an exception, that exception is used as the result of the derived
ClosingFuture.Example usage:
ClosingFuture<List<Row>> rowsFuture = queryFuture.transform((closer, result) -> result.getRows(), executor);When selecting an executor, note that
directExecutoris dangerous in some cases. See the discussion in theListenableFuture.addListener(Runnable, Executor)documentation. All its warnings about heavyweight listeners are also applicable to heavyweight functions passed to this method.After calling this method, you may not call
finishToFuture(),finishToValueAndCloser(ValueAndCloserConsumer, Executor), or any other derivation method on the originalClosingFutureinstance.- Parameters:
function- transforms the value of this step to the value of the derived stepexecutor- executor to run the function in- Returns:
- the derived step
- Throws:
IllegalStateException- if aClosingFuturehas already been derived from this one, or if thisClosingFuturehas already been finished
-
transformAsync
public <U extends @Nullable Object> ClosingFuture<U> transformAsync(ClosingFuture.AsyncClosingFunction<? super V, U> function, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function that returns aClosingFutureto its value. The function can use aClosingFuture.DeferredCloserto capture objects to be closed when the pipeline is done (other than those captured by the returnedClosingFuture).If this
ClosingFuturesucceeds, the derived one will be equivalent to the one returned by the function.If this
ClosingFuturefails, the function will not be called, and the derivedClosingFuturewill be equivalent to this one.If the function throws an exception, that exception is used as the result of the derived
ClosingFuture. But if the exception is thrown after the function creates aClosingFuture, then none of the closeable objects in thatClosingFuturewill be closed.Usage guidelines for this method:
- Use this method only when calling an API that returns a
ListenableFutureor aClosingFuture. If possible, prefer callingtransform(ClosingFunction, Executor)instead, with a function that returns the next value directly. - Call
closer.eventuallyClose()for every closeable object this step creates in order to capture it for later closing. - Return a
ClosingFuture. To turn aListenableFutureinto aClosingFuturecallfrom(ListenableFuture). - In case this step doesn't create new closeables, you can adapt an API that returns a
ListenableFutureto return aClosingFutureby wrapping it with a call towithoutCloser(AsyncFunction)
Example usage:
// Result.getRowsClosingFuture() returns a ClosingFuture. ClosingFuture<List<Row>> rowsFuture = queryFuture.transformAsync((closer, result) -> result.getRowsClosingFuture(), executor); // Result.writeRowsToOutputStreamFuture() returns a ListenableFuture that resolves to the // number of written rows. openOutputFile() returns a FileOutputStream (which implements // AutoCloseable). ClosingFuture<Integer> rowsFuture2 = queryFuture.transformAsync( (closer, result) -> { FileOutputStream fos = closer.eventuallyClose(openOutputFile(), closingExecutor); return ClosingFuture.from(result.writeRowsToOutputStreamFuture(fos)); }, executor); // Result.getRowsFuture() returns a ListenableFuture (no new closeables are created). ClosingFuture<List<Row>> rowsFuture3 = queryFuture.transformAsync(withoutCloser(Result::getRowsFuture), executor);When selecting an executor, note that
directExecutoris dangerous in some cases. See the discussion in theListenableFuture.addListener(Runnable, Executor)documentation. All its warnings about heavyweight listeners are also applicable to heavyweight functions passed to this method. (Specifically,directExecutorfunctions should avoid heavyweight operations insideAsyncClosingFunction.apply. Any heavyweight operations should occur in other threads responsible for completing the returnedClosingFuture.)After calling this method, you may not call
finishToFuture(),finishToValueAndCloser(ValueAndCloserConsumer, Executor), or any other derivation method on the originalClosingFutureinstance.- Parameters:
function- transforms the value of this step to aClosingFuturewith the value of the derived stepexecutor- executor to run the function in- Returns:
- the derived step
- Throws:
IllegalStateException- if aClosingFuturehas already been derived from this one, or if thisClosingFuturehas already been finished
- Use this method only when calling an API that returns a
-
withoutCloser
public static <V extends @Nullable Object, U extends @Nullable Object> ClosingFuture.AsyncClosingFunction<V,U> withoutCloser(AsyncFunction<V, U> function) Returns anClosingFuture.AsyncClosingFunctionthat applies anAsyncFunctionto an input, ignoring the DeferredCloser and returning aClosingFuturederived from the returnedListenableFuture.Use this method to pass a transformation to
transformAsync(AsyncClosingFunction, Executor)or tocatchingAsync(Class, AsyncClosingFunction, Executor)as long as it meets these conditions:- It does not need to capture any
AutoCloseableobjects by callingClosingFuture.DeferredCloser.eventuallyClose(Object, Executor). - It returns a
ListenableFuture.
Example usage:
// Result.getRowsFuture() returns a ListenableFuture. ClosingFuture<List<Row>> rowsFuture = queryFuture.transformAsync(withoutCloser(Result::getRowsFuture), executor);- Parameters:
function- transforms the value of aClosingFuturestep to aListenableFuturewith the value of a derived step
- It does not need to capture any
-
catching
public <X extends Throwable> ClosingFuture<V> catching(Class<X> exceptionType, ClosingFuture.ClosingFunction<? super X, ? extends V> fallback, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function to its exception if it is an instance of a given exception type. The function can use aClosingFuture.DeferredCloserto capture objects to be closed when the pipeline is done.If this
ClosingFuturesucceeds or fails with a different exception type, the function will not be called, and the derivedClosingFuturewill be equivalent to this one.If the function throws an exception, that exception is used as the result of the derived
ClosingFuture.Example usage:
ClosingFuture<QueryResult> queryFuture = queryFuture.catching( QueryException.class, (closer, x) -> Query.emptyQueryResult(), executor);When selecting an executor, note that
directExecutoris dangerous in some cases. See the discussion in theListenableFuture.addListener(Runnable, Executor)documentation. All its warnings about heavyweight listeners are also applicable to heavyweight functions passed to this method.After calling this method, you may not call
finishToFuture(),finishToValueAndCloser(ValueAndCloserConsumer, Executor), or any other derivation method on the originalClosingFutureinstance.- Parameters:
exceptionType- the exception type that triggers use offallback. The exception type is matched against this step's exception. "This step's exception" means the cause of theExecutionExceptionthrown byFuture.get()on theFutureunderlying this step or, ifget()throws a different kind of exception, that exception itself. To avoid hiding bugs and other unrecoverable errors, callers should prefer more specific types, avoidingThrowable.classin particular.fallback- 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 theExecutionExceptionthrown byFuture.get()on theFutureunderlying this step or, ifget()throws a different kind of exception, that exception itself.executor- the executor that runsfallbackif the input fails
-
catchingAsync
public <X extends Throwable> ClosingFuture<V> catchingAsync(Class<X> exceptionType, ClosingFuture.AsyncClosingFunction<? super X, ? extends V> fallback, Executor executor) Returns a newClosingFuturepipeline step derived from this one by applying a function that returns aClosingFutureto its exception if it is an instance of a given exception type. The function can use aClosingFuture.DeferredCloserto capture objects to be closed when the pipeline is done (other than those captured by the returnedClosingFuture).If this
ClosingFuturefails with an exception of the given type, the derivedClosingFuturewill be equivalent to the one returned by the function.If this
ClosingFuturesucceeds or fails with a different exception type, the function will not be called, and the derivedClosingFuturewill be equivalent to this one.If the function throws an exception, that exception is used as the result of the derived
ClosingFuture. But if the exception is thrown after the function creates aClosingFuture, then none of the closeable objects in thatClosingFuturewill be closed.Usage guidelines for this method:
- Use this method only when calling an API that returns a
ListenableFutureor aClosingFuture. If possible, prefer callingcatching(Class, ClosingFunction, Executor)instead, with a function that returns the next value directly. - Call
closer.eventuallyClose()for every closeable object this step creates in order to capture it for later closing. - Return a
ClosingFuture. To turn aListenableFutureinto aClosingFuturecallfrom(ListenableFuture). - In case this step doesn't create new closeables, you can adapt an API that returns a
ListenableFutureto return aClosingFutureby wrapping it with a call towithoutCloser(AsyncFunction)
Example usage:
// Fall back to a secondary input stream in case of IOException. ClosingFuture<InputStream> inputFuture = firstInputFuture.catchingAsync( IOException.class, (closer, x) -> secondaryInputStreamClosingFuture(), executor);When selecting an executor, note that
directExecutoris dangerous in some cases. See the discussion in theListenableFuture.addListener(Runnable, Executor)documentation. All its warnings about heavyweight listeners are also applicable to heavyweight functions passed to this method. (Specifically,directExecutorfunctions should avoid heavyweight operations insideAsyncClosingFunction.apply. Any heavyweight operations should occur in other threads responsible for completing the returnedClosingFuture.)After calling this method, you may not call
finishToFuture(),finishToValueAndCloser(ValueAndCloserConsumer, Executor), or any other derivation method on the originalClosingFutureinstance.- Parameters:
exceptionType- the exception type that triggers use offallback. The exception type is matched against this step's exception. "This step's exception" means the cause of theExecutionExceptionthrown byFuture.get()on theFutureunderlying this step or, ifget()throws a different kind of exception, that exception itself. To avoid hiding bugs and other unrecoverable errors, callers should prefer more specific types, avoidingThrowable.classin particular.fallback- 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 theExecutionExceptionthrown byFuture.get()on theFutureunderlying this step or, ifget()throws a different kind of exception, that exception itself.executor- the executor that runsfallbackif the input fails
- Use this method only when calling an API that returns a
-
finishToFuture
Marks this step as the last step in theClosingFuturepipeline.The returned
Futureis completed when the pipeline's computation completes, or when the pipeline is cancelled.All objects the pipeline has captured for closing will begin to be closed asynchronously after the returned
Futureis done: the future completes before closing starts, rather than once it has finished.After calling this method, you may not call
finishToValueAndCloser(ValueAndCloserConsumer, Executor), this method, or any other derivation method on the originalClosingFutureinstance.- Returns:
- a
Futurethat represents the final value or exception of the pipeline
-
finishToValueAndCloser
public void finishToValueAndCloser(ClosingFuture.ValueAndCloserConsumer<? super V> consumer, Executor executor) Marks this step as the last step in theClosingFuturepipeline. When this step is done,receiverwill be called with an object that contains the result of the operation. The receiver can store theClosingFuture.ValueAndCloseroutside the receiver for later synchronous use.After calling this method, you may not call
finishToFuture(), this method again, or any other derivation method on the originalClosingFutureinstance.- Parameters:
consumer- a callback whose method will be called (usingexecutor) when this operation is done
-
cancel
Attempts to cancel execution of this step. This attempt will fail if the step has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this step has not started whencancelis called, this step should never run.If successful, causes the objects captured by this step (if already started) and its input step(s) for later closing to be closed on their respective
Executors. If any such calls specifiedMoreExecutors.directExecutor(), those objects will be closed synchronously.- Parameters:
mayInterruptIfRunning-trueif the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete, but the step will be cancelled regardless- Returns:
falseif the step could not be cancelled, typically because it has already completed normally;trueotherwise
-
toString
-
finalize
-
Futures of closeable types is dangerous in general because the underlying value may never be closed if theFutureis canceled after its operation begins.