-
@Documented @Inherited @InterceptorBinding @Retention(RUNTIME) @Target({METHOD,TYPE}) public @interface AsynchronousAnnotates a CDI managed bean method to run asynchronously. The CDI managed bean must not be a Jakarta Enterprise Bean, and neither the method nor its class can be annotated with the MicroProfile Asynchronous annotation.The Jakarta EE Product Provider runs the method on a
ManagedExecutorServiceand returns to the caller aCompletableFuturethat is backed by the sameManagedExecutorServiceto represent the execution of the method. TheManagedExecutorServiceis the default asynchronous execution facility for theCompletableFutureand and all dependent stages that are created from those, and so on, as defined by theManagedExecutorServiceJavaDoc API. The Jakarta EE Product Provider makes thisCompletableFutureavailable to the asynchronous method implementation via theAsynchronous.Result.getFutureandAsynchronous.Result.completemethods.For example,
@Asynchronous public CompletableFuture<Double> hoursWorked(LocalDate from, LocalDate to) { // Application component's context is made available to the async method, try (Connection con = ((DataSource) InitialContext.doLookup( "java:comp/env/jdbc/timesheetDB")).getConnection()) { ... return Asynchronous.Result.complete(total); } catch (NamingException | SQLException x) { throw new CompletionException(x); } }with usage,hoursWorked(mon, fri).thenAccept(total -> { // Application component's context is made available to dependent stage actions, DataSource ds = InitialContext.doLookup( "java:comp/env/jdbc/payrollDB"); ... });When the asynchronous method implementation returns a differentCompletableFutureinstance, the Jakarta EE Product Provider uses the completion of that instance to complete theCompletableFuturethat the Jakarta EE Product Provider returns to the caller, completing it with the same result or exception.For example,
@Asynchronous public CompletableFuture<List<Itinerary>> findSingleLayoverFlights(Location source, Location dest) { try { ManagedExecutorService executor = InitialContext.doLookup( "java:comp/DefaultManagedExecutorService"); return executor.supplyAsync(source::flightsFrom) .thenCombine(executor.completedFuture(dest.flightsTo()), Itinerary::sourceMatchingDest); } catch (NamingException x) { throw new CompletionException(x); } }with usage,findSingleLayoverFlights(RST, DEN).thenApply(Itinerary::sortByPrice);
Methods with the following return types can be annotated to be asynchronous methods:
CompletableFutureCompletionStagevoid
The Jakarta EE Product Provider raises
UnsupportedOperationExceptionif other return types are used or if the annotation is placed at the class level. The injection target ofElementType.TYPEis to be used only by the CDI extension that is implemented by the Jakarta EE Product Provider to register the asynchronous method interceptor. Applications must only use the asynchronous method annotation at method level.Exceptions that are raised by asynchronous methods are not raised directly to the caller because the method runs asynchronously to the caller. Instead, the
CompletableFuturethat represents the result is completed with the raised exception. Asynchronous methods are discouraged from raising checked exceptions because checked exceptions force the caller to write exception handling code that is unreachable. When a checked exception occurs, the asynchronous method implementation can flow the exception back to the resultingCompletableFutureeither by raising aCompletionExceptionwith the original exception as the cause, or it can take the equivalent approach of exceptionally completing theCompletableFuture, usingcompleteExceptionallyto supply the original exception as the cause.Except where otherwise stated, the Jakarta EE Product Provider raises
RejectedExecutionExceptionupon invocation of the asynchronous method if evident upfront that it cannot be accepted, for example if the JNDI name is not valid or points to something other than a managed executor resource. If determined at a later point that the asynchronous method cannot run (for example, if unable to establish thread context), then the Jakarta EE Product Provider completes theCompletableFutureexceptionally withCancellationException, and chains a cause exception if there is any.The Jakarta EE Product Provider must assign the interceptor for asynchronous methods to have priority of
Interceptor.Priority.PLATFORM_BEFORE + 5. Interceptors with a lower priority, such asTransactional, must run on the thread where the asynchronous method executes, rather than on the submitting thread. When an asynchronous method is annotated asTransactional, the transactional types which can be used are:TxType.REQUIRES_NEW, which causes the method to run in a new transaction, andTxType.NOT_SUPPORTED, which causes the method to run with no transaction. All other transaction attributes must result inUnsupportedOperationExceptionupon invocation of the asynchronous method.- Since:
- 3.0
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description java.lang.StringexecutorJNDI name of aManagedExecutorServiceorManagedScheduledExecutorServiceupon which to run the asynchronous method.Schedule[]runAtEstablishes a schedule for repeated execution of the method.
-
-
-
Element Detail
-
executor
java.lang.String executor
JNDI name of aManagedExecutorServiceorManagedScheduledExecutorServiceupon which to run the asynchronous method.The default value is the JNDI name of the built-in
ManagedExecutorServicethat is provided by the Jakarta EE platform provider:
java:comp/DefaultManagedExecutorService- Returns:
- managed executor service JNDI name.
- Default:
- "java:comp/DefaultManagedExecutorService"
-
-
-
runAt
Schedule[] runAt
Establishes a schedule for repeated execution of the method. A single future represents the completion of all executions in the schedule. The Jakarta EE product attempts to run the method at the scheduled times until its future is completed or the method returns a non-null result value or raises an exception. The future is always accessible from within the method via
Asynchronous.Resultwhich returns a typedCompletableFuturewhich matches the return type of the method. If the method return type isvoidthenAsynchronous.Resultwill return aCompletableFuture<Void>.Computation of the start time for the next execution occurs after the completion of the current execution. This prevents overlap of executions from the same asynchronous method request. Scheduled execution times that overlap a prior execution that is still running are skipped. For example, if an asynchronous method is scheduled to run every minute on the minute and execution of the method starts at 8:00 AM, lasting for 2 minutes and 10 seconds, then the Jakarta EE product attempts to start the next execution of the method at 8:03 AM.
Scheduled asynchronous methods are treated similar to other scheduled tasks in that they are not subject to
max-asyncconstraints ofmanaged-scheduled-executor-definitionandmanaged-executor-definitionand the correspondingManagedScheduledExecutorDefinition.maxAsync()andManagedExecutorDefinition.maxAsync().When a list of multiple
Scheduleannotations is specified, the next execution time is computed according to each, choosing the closest future time after the current time. This allows composite schedules such as,@Asynchronous(runAt = { @Schedule(daysOfWeek = { DayOfWeek.TUESDAY, DayOfWeek.THURSDAY }, hours = 8), @Schedule(daysOfweek = DayOfWeek.WEDNESDAY, hours = 10, minutes = 30) }) public CompletableFuture<String> attendLectureAndLab(String course) { ... if (endOfSemester) return Asynchronous.Result.complete(courseRecord); else return null; // continue at next scheduled time } ... student.attendLectureAndLab(courseName).thenApply(this::assignGrade);The default value of empty array indicates that the task does not run on a schedule and instead runs one time.
- Returns:
- a schedule for the task or an empty array, where the latter indicates to run once without a schedule.
- Default:
- {}
-
-