public interface Futures
CompletionStage
operations.Modifier and Type | Method and Description |
---|---|
CompletionStage<akka.Done> |
delay(Duration duration)
Creates a completion stage which is only completed after the delay.
|
CompletionStage<akka.Done> |
delay(long amount,
TimeUnit unit)
Creates a completion stage which is only completed after the delay.
|
<A> CompletionStage<A> |
delayed(Callable<CompletionStage<A>> callable,
Duration duration)
Create a
CompletionStage which, after a delay, will be redeemed with the result of a
given supplier. |
<A> CompletionStage<A> |
delayed(Callable<CompletionStage<A>> callable,
long amount,
TimeUnit unit)
Create a
CompletionStage which, after a delay, will be redeemed with the result of a
given callable. |
static <A> CompletionStage<List<A>> |
sequence(CompletionStage<A>... promises)
Combine the given CompletionStages into a single CompletionStage for the list of results.
|
static <A> CompletionStage<List<A>> |
sequence(Iterable<? extends CompletionStage<A>> promises)
Combine the given CompletionStages into a single
CompletionStage for the list of
results. |
<A> CompletionStage<A> |
timeout(CompletionStage<A> stage,
Duration duration)
|
<A> CompletionStage<A> |
timeout(CompletionStage<A> stage,
long amount,
TimeUnit unit)
Creates a
CompletionStage that returns either the input stage, or a timeout. |
<A> CompletionStage<A> timeout(CompletionStage<A> stage, long amount, 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> CompletionStage<A> timeout(CompletionStage<A> stage, Duration 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> CompletionStage<A> delayed(Callable<CompletionStage<A>> callable, long amount, 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.CompletionStage<akka.Done> delay(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.CompletionStage<akka.Done> delay(long amount, TimeUnit unit)
amount
- The time to wait.unit
- The units to use for the amount.<A> CompletionStage<A> delayed(Callable<CompletionStage<A>> callable, 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> CompletionStage<List<A>> sequence(Iterable<? extends 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 combine@SafeVarargs static <A> CompletionStage<List<A>> sequence(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