- Enclosing class:
Asynchronous
CompletableFuture
instance that the Jakarta EE Product Provider supplies to the caller
of the asynchronous method.
Before invoking the asynchronous method implementation on a thread,
the Jakarta EE Product Provider invokes the setFuture(java.util.concurrent.CompletableFuture<T>)
method
which makes available to the asynchronous method implementation
the same CompletableFuture
that the Jakarta EE Product Provider
returns to the caller.
The asynchronous method implementation invokes the getFuture()
method
to obtain the same CompletableFuture
that the
Jakarta EE Product Provider returns to the caller.
The asynchronous method implementation can choose to complete
this future (normally or exceptionally) or otherwise arrange for its
completion, for example upon completion of a pipeline of completion stages.
Having this same CompletableFuture
also enables the asynchronous
method implementation to determine if the caller has forcibly completed
(such as by cancellation or any other means) the CompletableFuture
,
in which case the asynchronous method implementation could decide to end
immediately rather than continue processing.
For example,
@Asynchronous public CompletableFuture<Double> hoursWorked(LocalDateTime from, LocalDateTime to) { CompletableFuture<Double> future = Asynchronous.Result.getFuture(); if (future.isDone()) return future; try (Connection con = ((DataSource) InitialContext.doLookup( "java:comp/env/jdbc/timesheetDB")).getConnection()) { ... for (ResultSet result = stmt.executeQuery(); result.next() && !future.isDone(); ) ... future.complete(total); } catch (NamingException | SQLException x) { future.completeExceptionally(x); } return future; }After the asynchronous method completes, the Jakarta EE Product Provider invokes the
setFuture(java.util.concurrent.CompletableFuture<T>)
method with a null
value
to clear it from the thread.- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> CompletableFuture
<T> complete
(T result) Completes theCompletableFuture
instance that the Jakarta EE Product Provider supplies to the caller of the asynchronous method.static <T> CompletableFuture
<T> Obtains the sameCompletableFuture
instance that the Jakarta EE Product Provider supplies to the caller of the asynchronous method.static <T> void
setFuture
(CompletableFuture<T> future) Before invoking the asynchronous method implementation on a thread, the Jakarta EE Product Provider invokes this method to make available to the asynchronous method implementation the sameCompletableFuture
that the Jakarta EE Product Provider returns to the caller.
-
Method Details
-
complete
Completes theCompletableFuture
instance that the Jakarta EE Product Provider supplies to the caller of the asynchronous method.This method must only be invoked by the asynchronous method implementation.
- Type Parameters:
T
- type of result returned by the asynchronous method'sCompletableFuture
.- Parameters:
result
- result with which to complete the asynchronous method'sCompletableFuture
.- Returns:
- the same
CompletableFuture
that the container returns to the caller. - Throws:
IllegalStateException
- if theCompletableFuture
for an asynchronous method is not present on the thread.
-
getFuture
Obtains the sameCompletableFuture
instance that the Jakarta EE Product Provider supplies to the caller of the asynchronous method.This method must only be invoked by the asynchronous method implementation.
- Type Parameters:
T
- type of result returned by the asynchronous method'sCompletableFuture
.- Returns:
- the same
CompletableFuture
that the container returns to the caller. - Throws:
IllegalStateException
- if theCompletableFuture
for an asynchronous method is not present on the thread.
-
setFuture
Before invoking the asynchronous method implementation on a thread, the Jakarta EE Product Provider invokes this method to make available to the asynchronous method implementation the sameCompletableFuture
that the Jakarta EE Product Provider returns to the caller.After the asynchronous method completes, the Jakarta EE Product Provider invokes this method with a
null
value to clear it from the thread.This method must only be invoked by the Jakarta EE Product Provider.
- Type Parameters:
T
- type of result returned by the asynchronous method'sCompletableFuture
.- Parameters:
future
-CompletableFuture
that the container returns to the caller, ornull
to clear it.
-