Class Asynchronous.Result
- java.lang.Object
-
- jakarta.enterprise.concurrent.Asynchronous.Result
-
- Enclosing class:
- Asynchronous
public static final class Asynchronous.Result extends Object
Mechanism by which the Jakarta EE Product Provider makes available to the asynchronous method implementation the sameCompletableFuture
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 sameCompletableFuture
that the Jakarta EE Product Provider returns to the caller.The asynchronous method implementation invokes the
getFuture()
method to obtain the sameCompletableFuture
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 sameCompletableFuture
also enables the asynchronous method implementation to determine if the caller has forcibly completed (such as by cancellation or any other means) theCompletableFuture
, 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 thesetFuture(java.util.concurrent.CompletableFuture<T>)
method with anull
value to clear it from the thread.- Since:
- 3.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <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>
getFuture()
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 Detail
-
complete
public static <T> CompletableFuture<T> complete(T result)
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
public static <T> CompletableFuture<T> 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
public 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.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.
-
-