类 CompletableFutureCancellationHandler
java.lang.Object
org.apache.pulsar.common.util.CompletableFutureCancellationHandler
Implements cancellation and timeout support for CompletableFutures.
This class ensures that the cancel action gets called once after the future completes with
either CancellationException
or TimeoutException
.
The implementation handles possible race conditions that
might happen when the future gets cancelled before the cancel action is set to this handler.
For timeouts, CompletableFuture's "orTimeout" method introduced in JDK9 can be used in client code.
Cancellation and timeout support will only be active on the future where the cancellation handler has been attached to. Cancellation won't happen if .cancel is called on any "downstream" dependent futures. A cancellation or timeout that happens in any "upstream" future will get handled.
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明void
attachToFuture
(CompletableFuture<?> future) Attaches the cancellation handler to handle cancels and timeouts.<T> CompletableFuture<T>
Creates a newCompletableFuture
and attaches the cancellation handler to handle cancels and timeouts.void
setCancelAction
(Runnable cancelAction) Set the action to run when the future gets cancelled or timeouts.
-
构造器详细资料
-
CompletableFutureCancellationHandler
public CompletableFutureCancellationHandler()
-
-
方法详细资料
-
createFuture
Creates a newCompletableFuture
and attaches the cancellation handler to handle cancels and timeouts.- 类型参数:
T
- the result type of the future- 返回:
- a new future instance
-
attachToFuture
Attaches the cancellation handler to handle cancels and timeouts. A cancellation handler instance can be used only once.- 参数:
future
- the future to attach the handler to
-
setCancelAction
Set the action to run when the future gets cancelled or timeouts. The cancellation or timeout might be originating from any "upstream" future. The implementation ensures that the cancel action gets called once. Handles possible race conditions that might happen when the future gets cancelled before the cancel action is set to this handler. In this case, the cancel action gets called when the action is set.- 参数:
cancelAction
- the action to run when the the future gets cancelled or timeouts
-