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<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. |
<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. |
<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)
A
- the type of the completion's result.promises
- The CompletionStages to combine