Skip to content

Commit 8df45dd

Browse files
committed
@async docs explicitly mention ListenableFuture and CompletableFuture
Issue: SPR-14881
1 parent cd398ed commit 8df45dd

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

spring-context/src/main/java/org/springframework/scheduling/annotation/Async.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@
2929
*
3030
* <p>In terms of target method signatures, any parameter types are supported.
3131
* However, the return type is constrained to either {@code void} or
32-
* {@link java.util.concurrent.Future}. In the latter case, the {@code Future} handle
33-
* returned from the proxy will be an actual asynchronous {@code Future} that can be used
34-
* to track the result of the asynchronous method execution. However, since the
35-
* target method needs to implement the same signature, it will have to return
36-
* a temporary {@code Future} handle that just passes the return value through: e.g.
37-
* Spring's {@link AsyncResult} or EJB 3.1's {@link javax.ejb.AsyncResult}.
32+
* {@link java.util.concurrent.Future}. In the latter case, you may declare the
33+
* more specific {@link org.springframework.util.concurrent.ListenableFuture} or
34+
* {@link java.util.concurrent.CompletableFuture} types which allow for richer
35+
* interaction with the asynchronous task and for immediate composition with
36+
* further processing steps.
37+
*
38+
* <p>A {@code Future} handle returned from the proxy will be an actual asynchronous
39+
* {@code Future} that can be used to track the result of the asynchronous method
40+
* execution. However, since the target method needs to implement the same signature,
41+
* it will have to return a temporary {@code Future} handle that just passes a value
42+
* through: e.g. Spring's {@link AsyncResult}, EJB 3.1's {@link javax.ejb.AsyncResult},
43+
* or {@link java.util.concurrent.CompletableFuture#completedFuture(Object)}.
3844
*
3945
* @author Juergen Hoeller
4046
* @author Chris Beams

src/asciidoc/integration.adoc

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ proxy will take care of forwarding the call to the server-side object via JMS.
866866
----
867867

868868

869+
870+
869871
[[remoting-amqp]]
870872
=== AMQP
871873
Refer to the {doc-spring-amqp}/html/_reference.html#remoting[Spring AMQP Reference Document
@@ -1087,6 +1089,7 @@ Note that the `java.net` implementation for HTTP requests may raise an exception
10871089
accessing the status of a response that represents an error (e.g. 401). If this is an
10881090
issue, switch to `HttpComponentsClientHttpRequestFactory` instead.
10891091
====
1092+
10901093
The previous example using Apache HttpComponents `HttpClient` directly rewritten to use
10911094
the `RestTemplate` is shown below
10921095

@@ -1124,6 +1127,7 @@ construct a `HttpComponentsClientHttpRequestFactory` like so:
11241127
RestTemplate restTemplate = new RestTemplate(requestFactory);
11251128
----
11261129
====
1130+
11271131
The general callback interface is `RequestCallback` and is called when the execute
11281132
method is invoked.
11291133

@@ -2591,13 +2595,16 @@ what the <<jms-receiving-async-message-listener-adapter, `MessageListenerAdapter
25912595
provides).
25922596

25932597
The annotated endpoint infrastructure creates a message listener container
2594-
behind the scenes for each annotated method, using a `JmsListenerContainerFactory`. Such
2595-
container is not registered against the application context but can be easily
2598+
behind the scenes for each annotated method, using a `JmsListenerContainerFactory`.
2599+
Such a container is not registered against the application context but can be easily
25962600
located for management purposes using the `JmsListenerEndpointRegistry` bean.
25972601

2598-
TIP: `@JmsListener` is a _repeatable_ annotation so it is possible to associate several
2599-
JMS destinations to the same method by adding additional `@JmsListener` declaration on
2600-
it. For pre Java8 use cases, you can use `@JmsListeners`.
2602+
[TIP]
2603+
====
2604+
`@JmsListener` is a _repeatable_ annotation on Java 8, so it is possible to associate
2605+
several JMS destinations to the same method by adding additional `@JmsListener`
2606+
declarations to it. On Java 6 and 7, you can use the `@JmsListeners` annotation.
2607+
====
26012608

26022609
[[jms-annotated-support]]
26032610
==== Enable listener endpoint annotations
@@ -2785,8 +2792,11 @@ as follow to automatically send a response:
27852792
}
27862793
----
27872794

2788-
TIP: If you have several `@JmsListener`-annotated methods, you can also place the `@SendTo`
2789-
annotation at class-level to share a default reply destination.
2795+
[TIP]
2796+
====
2797+
If you have several `@JmsListener`-annotated methods, you can also place the `@SendTo`
2798+
annotation at the class level to share a default reply destination.
2799+
====
27902800

27912801
If you need to set additional headers in a transport-independent manner, you could return a
27922802
`Message` instead, something like:
@@ -2826,7 +2836,7 @@ example can be rewritten as follows:
28262836

28272837

28282838
[[jms-namespace]]
2829-
=== JMS Namespace Support
2839+
=== JMS namespace support
28302840
Spring provides an XML namespace for simplifying JMS configuration. To use the JMS
28312841
namespace elements you will need to reference the JMS schema:
28322842

@@ -6372,7 +6382,7 @@ reference is provided for managing those methods annotated with `@Scheduled`.
63726382

63736383

63746384
[[scheduling-annotation-support-scheduled]]
6375-
==== The @Scheduled Annotation
6385+
==== The @Scheduled annotation
63766386
The `@Scheduled` annotation can be added to a method along with trigger metadata. For
63776387
example, the following method would be invoked every 5 seconds with a fixed delay,
63786388
meaning that the period will be measured from the completion time of each preceding
@@ -6430,7 +6440,6 @@ You can additionally use the `zone` attribute to specify the time zone in which
64306440
expression will be resolved.
64316441
====
64326442

6433-
64346443
Notice that the methods to be scheduled must have void returns and must not expect any
64356444
arguments. If the method needs to interact with other objects from the Application
64366445
Context, then those would typically have been provided through dependency injection.
@@ -6451,7 +6460,7 @@ container and once through the `@Configurable` aspect, with the consequence of e
64516460

64526461

64536462
[[scheduling-annotation-support-async]]
6454-
==== The @Async Annotation
6463+
==== The @Async annotation
64556464
The `@Async` annotation can be provided on a method so that invocation of that method
64566465
will occur asynchronously. In other words, the caller will return immediately upon
64576466
invocation and the actual execution of the method will occur in a task that has been
@@ -6495,6 +6504,14 @@ asynchronous execution so that the caller can perform other tasks prior to calli
64956504
}
64966505
----
64976506

6507+
[TIP]
6508+
====
6509+
`@Async` methods may not only declare a regular `java.util.concurrent.Future` return type
6510+
but also Spring's `org.springframework.util.concurrent.ListenableFuture` or, as of Spring
6511+
4.2, JDK 8's `java.util.concurrent.CompletableFuture`: for richer interaction with the
6512+
asynchronous task and for immediate composition with further processing steps.
6513+
====
6514+
64986515
`@Async` can not be used in conjunction with lifecycle callbacks such as
64996516
`@PostConstruct`. To asynchronously initialize Spring beans you currently have to use
65006517
a separate initializing Spring bean that invokes the `@Async` annotated method on the
@@ -6583,8 +6600,11 @@ however, the exception is uncaught and cannot be transmitted. For those cases, a
65836600
By default, the exception is simply logged. A custom `AsyncUncaughtExceptionHandler` can
65846601
be defined _via_ `AsyncConfigurer` or the `task:annotation-driven` XML element.
65856602

6603+
6604+
6605+
65866606
[[scheduling-task-namespace]]
6587-
=== The Task Namespace
6607+
=== The task namespace
65886608
Beginning with Spring 3.0, there is an XML namespace for configuring `TaskExecutor` and
65896609
`TaskScheduler` instances. It also provides a convenient way to configure tasks to be
65906610
scheduled with a trigger.

0 commit comments

Comments
 (0)