public interface Futures
CompletionStage
operations.Modifier and Type | Method and Description |
---|---|
java.util.concurrent.CompletionStage<akka.Done> |
delay(java.time.Duration duration)
Creates a completion stage which is only completed after the delay.
|
java.util.concurrent.CompletionStage<akka.Done> |
delay(long amount,
java.util.concurrent.TimeUnit unit)
Creates a completion stage which is only completed after the delay.
|
<A> java.util.concurrent.CompletionStage<A> |
delayed(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<A>> callable,
java.time.Duration duration)
Create a
CompletionStage which, after a delay, will be redeemed with the result of a
given supplier. |
<A> java.util.concurrent.CompletionStage<A> |
delayed(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<A>> callable,
long amount,
java.util.concurrent.TimeUnit unit)
Create a
CompletionStage which, after a delay, will be redeemed with the result of a
given callable. |
static <A> java.util.concurrent.CompletionStage<A> |
delayed(java.util.function.Supplier<A> supplier,
long delay,
java.util.concurrent.TimeUnit unit,
java.util.concurrent.Executor executor)
Deprecated.
Use injected
delayed(Callable, long, TimeUnit) , since 2.6.0 |
static <A> java.util.concurrent.CompletionStage<java.util.List<A>> |
sequence(java.util.concurrent.CompletionStage<A>... promises)
Combine the given CompletionStages into a single CompletionStage for the list of results.
|
static <A> java.util.concurrent.CompletionStage<java.util.List<A>> |
sequence(java.lang.Iterable<? extends java.util.concurrent.CompletionStage<A>> promises)
Combine the given CompletionStages into a single
CompletionStage for the list of
results. |
static <A> java.util.concurrent.CompletionStage<A> |
timeout(A value,
long amount,
java.util.concurrent.TimeUnit unit)
Deprecated.
Use injected
timeout(CompletionStage, long, TimeUnit) , since 2.6.0 |
<A> java.util.concurrent.CompletionStage<A> |
timeout(java.util.concurrent.CompletionStage<A> stage,
java.time.Duration duration)
An alias for
timeout that uses a Duration . |
<A> java.util.concurrent.CompletionStage<A> |
timeout(java.util.concurrent.CompletionStage<A> stage,
long amount,
java.util.concurrent.TimeUnit unit)
Creates a
CompletionStage that returns either the input stage, or a timeout. |
static <A> java.util.concurrent.CompletionStage<A> |
timeout(long delay,
java.util.concurrent.TimeUnit unit)
Deprecated.
Use injected
timeout(CompletionStage, long, TimeUnit) , since 2.6.0 |
<A> java.util.concurrent.CompletionStage<A> timeout(java.util.concurrent.CompletionStage<A> stage, long amount, java.util.concurrent.TimeUnit unit)
CompletionStage
that returns either the input stage, or a timeout.
Note that timeout is not the same as cancellation. Even in case of timeout, the given completion stage will still complete, even though that completed value is not returned.
CompletionStage<Double> callWithTimeout() {
return futures.timeout(delayByOneSecond(), Duration.ofMillis(300));
}
A
- the completion's result type.stage
- the input completion stage that may time out.amount
- The amount (expressed with the corresponding unit).unit
- The time Unit.<A> java.util.concurrent.CompletionStage<A> timeout(java.util.concurrent.CompletionStage<A> stage, java.time.Duration duration)
timeout
that uses a Duration
.A
- the completion stage that should be wrapped with a timeout.stage
- the input completion stage that may time out.duration
- The duration after which there is a timeout.<A> java.util.concurrent.CompletionStage<A> delayed(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<A>> callable, long amount, java.util.concurrent.TimeUnit unit)
CompletionStage
which, after a delay, will be redeemed with the result of a
given callable. The completion stage will be called after the delay.A
- the type of the completion's result.callable
- the input completion stage that is called after the delay.amount
- The time to wait.unit
- The units to use for the amount.java.util.concurrent.CompletionStage<akka.Done> delay(java.time.Duration duration)
Duration expected = Duration.ofSeconds(2);
long start = System.currentTimeMillis();
CompletionStage<Long> stage = futures.delay(expected).thenApply((v) -> {
long end = System.currentTimeMillis();
return (end - start);
});
duration
- the duration after which the completion stage is run.java.util.concurrent.CompletionStage<akka.Done> delay(long amount, java.util.concurrent.TimeUnit unit)
amount
- The time to wait.unit
- The units to use for the amount.<A> java.util.concurrent.CompletionStage<A> delayed(java.util.concurrent.Callable<java.util.concurrent.CompletionStage<A>> callable, java.time.Duration duration)
CompletionStage
which, after a delay, will be redeemed with the result of a
given supplier. The completion stage will be called after the delay.
For example, to render a number indicating the delay, you can use the following method:
private CompletionStage<Long> renderAfter(Duration duration) {
long start = System.currentTimeMillis();
return futures.delayed(() -> {
long end = System.currentTimeMillis();
return CompletableFuture.completedFuture(end - start);
}, duration);
}
A
- the type of the completion's result.callable
- the input completion stage that is called after the delay.duration
- to wait.static <A> java.util.concurrent.CompletionStage<java.util.List<A>> sequence(java.lang.Iterable<? extends java.util.concurrent.CompletionStage<A>> promises)
CompletionStage
for the list of
results.
The sequencing operations are performed in the default ExecutionContext.
A
- the type of the completion's result.promises
- The CompletionStages to combinestatic <A> java.util.concurrent.CompletionStage<java.util.List<A>> sequence(java.util.concurrent.CompletionStage<A>... promises)
The sequencing operations are performed in the default ExecutionContext.
A
- the type of the completion's result.promises
- The CompletionStages to combine@Deprecated static <A> java.util.concurrent.CompletionStage<A> timeout(A value, long amount, java.util.concurrent.TimeUnit unit)
timeout(CompletionStage, long, TimeUnit)
, since 2.6.0The underlying implementation uses TimerTask, which has a resolution in milliseconds.
A
- the type of the completion's result.value
- The result value to use to complete the CompletionStage.amount
- The amount (expressed with the corresponding unit).unit
- The time unit, i.e. java.util.concurrent.TimeUnit.MILLISECONDS@Deprecated static <A> java.util.concurrent.CompletionStage<A> timeout(long delay, java.util.concurrent.TimeUnit unit)
timeout(CompletionStage, long, TimeUnit)
, since 2.6.0CompletionStage
timer that throws a PromiseTimeoutException after a given
timeout.
The returned CompletionStage is usually combined with other CompletionStage, i.e. completionStage.applyToEither(Futures.timeout(delay, unit), Function.identity())
The underlying implementation uses TimerTask, which has a resolution in milliseconds.
A previous implementation used CompletionStage<Void>
which made it unsuitable for
composition. Cast with Futures.<Void>timeout
if necessary.
A
- the type of the completion's result.delay
- The delay (expressed with the corresponding unit).unit
- The time Unit.@Deprecated static <A> java.util.concurrent.CompletionStage<A> delayed(java.util.function.Supplier<A> supplier, long delay, java.util.concurrent.TimeUnit unit, java.util.concurrent.Executor executor)
delayed(Callable, long, TimeUnit)
, since 2.6.0CompletionStage
which, after a delay, will be redeemed with the result of a
given supplier. The completion stage will be called after the delay.A
- the type of the completion's result.supplier
- The supplier to call to fulfill the CompletionStage.delay
- The time to wait.unit
- The units to use for the delay.executor
- The executor to run the supplier in.