Skip to content

Commit c8bb32b

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

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
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: 36 additions & 14 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,6 +2803,12 @@ as follow to automatically send a response:
27962803
}
27972804
----
27982805

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+
====
2811+
27992812
If you need to set additional headers in a transport-independent manner, you could return a
28002813
`Message` instead, something like:
28012814

@@ -2834,7 +2847,7 @@ example can be rewritten as follows:
28342847

28352848

28362849
[[jms-namespace]]
2837-
=== JMS Namespace Support
2850+
=== JMS namespace support
28382851
Spring provides an XML namespace for simplifying JMS configuration. To use the JMS
28392852
namespace elements you will need to reference the JMS schema:
28402853

@@ -6491,7 +6504,7 @@ reference is provided for managing those methods annotated with `@Scheduled`.
64916504

64926505

64936506
[[scheduling-annotation-support-scheduled]]
6494-
==== The @Scheduled Annotation
6507+
==== The @Scheduled annotation
64956508
The `@Scheduled` annotation can be added to a method along with trigger metadata. For
64966509
example, the following method would be invoked every 5 seconds with a fixed delay,
64976510
meaning that the period will be measured from the completion time of each preceding
@@ -6549,7 +6562,6 @@ You can additionally use the `zone` attribute to specify the time zone in which
65496562
expression will be resolved.
65506563
====
65516564

6552-
65536565
Notice that the methods to be scheduled must have void returns and must not expect any
65546566
arguments. If the method needs to interact with other objects from the Application
65556567
Context, then those would typically have been provided through dependency injection.
@@ -6568,7 +6580,7 @@ container and once through the `@Configurable` aspect, with the consequence of e
65686580

65696581

65706582
[[scheduling-annotation-support-async]]
6571-
==== The @Async Annotation
6583+
==== The @Async annotation
65726584
The `@Async` annotation can be provided on a method so that invocation of that method
65736585
will occur asynchronously. In other words, the caller will return immediately upon
65746586
invocation and the actual execution of the method will occur in a task that has been
@@ -6612,6 +6624,14 @@ asynchronous execution so that the caller can perform other tasks prior to calli
66126624
}
66136625
----
66146626

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

6723+
6724+
6725+
67036726
[[scheduling-task-namespace]]
6704-
=== The Task Namespace
6727+
=== The task namespace
67056728
Beginning with Spring 3.0, there is an XML namespace for configuring `TaskExecutor` and
67066729
`TaskScheduler` instances. It also provides a convenient way to configure tasks to be
67076730
scheduled with a trigger.
@@ -7756,7 +7779,6 @@ set some default property values, or specify a custom `MetaClass`.
77567779
public interface GroovyObjectCustomizer {
77577780
77587781
void customize(GroovyObject goo);
7759-
77607782
}
77617783
----
77627784

@@ -7795,12 +7817,12 @@ of a `GroovyObjectCustomizer` is easy if you are using the Spring namespace supp
77957817
[subs="verbatim,quotes"]
77967818
----
77977819
<!-- define the GroovyObjectCustomizer just like any other bean -->
7798-
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer" />
7820+
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer"/>
77997821
78007822
<!-- ... and plug it into the desired Groovy bean via the 'customizer-ref' attribute -->
78017823
<lang:groovy id="calculator"
78027824
script-source="classpath:org/springframework/scripting/groovy/Calculator.groovy"
7803-
customizer-ref="tracingCustomizer" />
7825+
customizer-ref="tracingCustomizer"/>
78047826
----
78057827

78067828
If you are not using the Spring namespace support, you can still use the
@@ -7813,7 +7835,7 @@ If you are not using the Spring namespace support, you can still use the
78137835
<constructor-arg value="classpath:org/springframework/scripting/groovy/Calculator.groovy"/>
78147836
<!-- define the GroovyObjectCustomizer (as an inner bean) -->
78157837
<constructor-arg>
7816-
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer" />
7838+
<bean id="tracingCustomizer" class="example.SimpleMethodTracingCustomizer"/>
78177839
</constructor-arg>
78187840
</bean>
78197841

0 commit comments

Comments
 (0)