Skip to content

Commit 9be1710

Browse files
committed
@async docs explicitly mention ListenableFuture and CompletableFuture
Issue: SPR-14881 (cherry picked from commit 8df45dd)
1 parent 7e7504f commit 9be1710

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
@@ -877,6 +877,8 @@ proxy will take care of forwarding the call to the server-side object via JMS.
877877
----
878878

879879

880+
881+
880882
[[remoting-amqp]]
881883
=== AMQP
882884
Refer to the {doc-spring-amqp}/html/_reference.html#remoting[Spring AMQP Reference Document
@@ -1098,6 +1100,7 @@ Note that the `java.net` implementation for HTTP requests may raise an exception
10981100
accessing the status of a response that represents an error (e.g. 401). If this is an
10991101
issue, switch to `HttpComponentsClientHttpRequestFactory` instead.
11001102
====
1103+
11011104
The previous example using Apache HttpComponents `HttpClient` directly rewritten to use
11021105
the `RestTemplate` is shown below
11031106

@@ -1135,6 +1138,7 @@ construct a `HttpComponentsClientHttpRequestFactory` like so:
11351138
RestTemplate restTemplate = new RestTemplate(requestFactory);
11361139
----
11371140
====
1141+
11381142
The general callback interface is `RequestCallback` and is called when the execute
11391143
method is invoked.
11401144

@@ -2602,13 +2606,16 @@ what the <<jms-receiving-async-message-listener-adapter, `MessageListenerAdapter
26022606
provides).
26032607

26042608
The annotated endpoint infrastructure creates a message listener container
2605-
behind the scenes for each annotated method, using a `JmsListenerContainerFactory`. Such
2606-
container is not registered against the application context but can be easily
2609+
behind the scenes for each annotated method, using a `JmsListenerContainerFactory`.
2610+
Such a container is not registered against the application context but can be easily
26072611
located for management purposes using the `JmsListenerEndpointRegistry` bean.
26082612

2609-
TIP: `@JmsListener` is a _repeatable_ annotation so it is possible to associate several
2610-
JMS destinations to the same method by adding additional `@JmsListener` declaration on
2611-
it. For pre Java8 use cases, you can use `@JmsListeners`.
2613+
[TIP]
2614+
====
2615+
`@JmsListener` is a _repeatable_ annotation on Java 8, so it is possible to associate
2616+
several JMS destinations to the same method by adding additional `@JmsListener`
2617+
declarations to it. On Java 6 and 7, you can use the `@JmsListeners` annotation.
2618+
====
26122619

26132620
[[jms-annotated-support]]
26142621
==== Enable listener endpoint annotations
@@ -2796,8 +2803,11 @@ as follow to automatically send a response:
27962803
}
27972804
----
27982805

2799-
TIP: If you have several `@JmsListener`-annotated methods, you can also place the `@SendTo`
2800-
annotation at class-level to share a default reply destination.
2806+
[TIP]
2807+
====
2808+
If you have several `@JmsListener`-annotated methods, you can also place the `@SendTo`
2809+
annotation at the class level to share a default reply destination.
2810+
====
28012811

28022812
If you need to set additional headers in a transport-independent manner, you could return a
28032813
`Message` instead, something like:
@@ -2837,7 +2847,7 @@ example can be rewritten as follows:
28372847

28382848

28392849
[[jms-namespace]]
2840-
=== JMS Namespace Support
2850+
=== JMS namespace support
28412851
Spring provides an XML namespace for simplifying JMS configuration. To use the JMS
28422852
namespace elements you will need to reference the JMS schema:
28432853

@@ -6497,7 +6507,7 @@ reference is provided for managing those methods annotated with `@Scheduled`.
64976507

64986508

64996509
[[scheduling-annotation-support-scheduled]]
6500-
==== The @Scheduled Annotation
6510+
==== The @Scheduled annotation
65016511
The `@Scheduled` annotation can be added to a method along with trigger metadata. For
65026512
example, the following method would be invoked every 5 seconds with a fixed delay,
65036513
meaning that the period will be measured from the completion time of each preceding
@@ -6555,7 +6565,6 @@ You can additionally use the `zone` attribute to specify the time zone in which
65556565
expression will be resolved.
65566566
====
65576567

6558-
65596568
Notice that the methods to be scheduled must have void returns and must not expect any
65606569
arguments. If the method needs to interact with other objects from the Application
65616570
Context, then those would typically have been provided through dependency injection.
@@ -6576,7 +6585,7 @@ container and once through the `@Configurable` aspect, with the consequence of e
65766585

65776586

65786587
[[scheduling-annotation-support-async]]
6579-
==== The @Async Annotation
6588+
==== The @Async annotation
65806589
The `@Async` annotation can be provided on a method so that invocation of that method
65816590
will occur asynchronously. In other words, the caller will return immediately upon
65826591
invocation and the actual execution of the method will occur in a task that has been
@@ -6620,6 +6629,14 @@ asynchronous execution so that the caller can perform other tasks prior to calli
66206629
}
66216630
----
66226631

6632+
[TIP]
6633+
====
6634+
`@Async` methods may not only declare a regular `java.util.concurrent.Future` return type
6635+
but also Spring's `org.springframework.util.concurrent.ListenableFuture` or, as of Spring
6636+
4.2, JDK 8's `java.util.concurrent.CompletableFuture`: for richer interaction with the
6637+
asynchronous task and for immediate composition with further processing steps.
6638+
====
6639+
66236640
`@Async` can not be used in conjunction with lifecycle callbacks such as
66246641
`@PostConstruct`. To asynchronously initialize Spring beans you currently have to use
66256642
a separate initializing Spring bean that invokes the `@Async` annotated method on the
@@ -6708,8 +6725,11 @@ however, the exception is uncaught and cannot be transmitted. For those cases, a
67086725
By default, the exception is simply logged. A custom `AsyncUncaughtExceptionHandler` can
67096726
be defined _via_ `AsyncConfigurer` or the `task:annotation-driven` XML element.
67106727

6728+
6729+
6730+
67116731
[[scheduling-task-namespace]]
6712-
=== The Task Namespace
6732+
=== The task namespace
67136733
Beginning with Spring 3.0, there is an XML namespace for configuring `TaskExecutor` and
67146734
`TaskScheduler` instances. It also provides a convenient way to configure tasks to be
67156735
scheduled with a trigger.

0 commit comments

Comments
 (0)