26
26
import org .springframework .web .context .request .NativeWebRequest ;
27
27
28
28
/**
29
- * {@code DeferredResult} provides an alternative to using a {@link Callable}
30
- * for asynchronous request processing. While a {@code Callable} is executed
31
- * concurrently on behalf of the application, with a {@code DeferredResult} the
32
- * application can produce the result from a thread of its choice.
29
+ * {@code DeferredResult} provides an alternative to using a {@link Callable} for
30
+ * asynchronous request processing. While a {@code Callable} is executed concurrently
31
+ * on behalf of the application, with a {@code DeferredResult} the application can
32
+ * produce the result from a thread of its choice.
33
33
*
34
- * <p>Subclasses can extend this class to easily associate additional data or
35
- * behavior with the {@link DeferredResult}. For example, one might want to
36
- * associate the user used to create the {@link DeferredResult} by extending the
37
- * class and adding an additional property for the user. In this way, the user
38
- * could easily be accessed later without the need to use a data structure to do
39
- * the mapping.
34
+ * <p>Subclasses can extend this class to easily associate additional data or behavior
35
+ * with the {@link DeferredResult}. For example, one might want to associate the user
36
+ * used to create the {@link DeferredResult} by extending the class and adding an
37
+ * additional property for the user. In this way, the user could easily be accessed
38
+ * later without the need to use a data structure to do the mapping.
40
39
*
41
- * <p>An example of associating additional behavior to this class might be
42
- * realized by extending the class to implement an additional interface. For
43
- * example, one might want to implement {@link Comparable} so that when the
44
- * {@link DeferredResult} is added to a {@link PriorityQueue} it is handled in
45
- * the correct order.
40
+ * <p>An example of associating additional behavior to this class might be realized
41
+ * by extending the class to implement an additional interface. For example, one
42
+ * might want to implement {@link Comparable} so that when the {@link DeferredResult}
43
+ * is added to a {@link PriorityQueue} it is handled in the correct order.
46
44
*
47
45
* @author Rossen Stoyanchev
48
46
* @author Rob Winch
49
47
* @since 3.2
50
48
*/
51
49
public class DeferredResult <T > {
52
50
53
- private static final Log logger = LogFactory .getLog (DeferredResult .class );
54
-
55
51
private static final Object RESULT_NONE = new Object ();
56
52
53
+ private static final Log logger = LogFactory .getLog (DeferredResult .class );
54
+
57
55
58
56
private final Long timeout ;
59
57
@@ -88,7 +86,7 @@ public DeferredResult(long timeout) {
88
86
/**
89
87
* Create a DeferredResult with a timeout value and a default result to use
90
88
* in case of timeout.
91
- * @param timeout timeout value in milliseconds; ignored if {@code null}
89
+ * @param timeout timeout value in milliseconds ( ignored if {@code null})
92
90
* @param timeoutResult the result to use
93
91
*/
94
92
public DeferredResult (Long timeout , Object timeoutResult ) {
@@ -111,6 +109,7 @@ public final boolean isSetOrExpired() {
111
109
112
110
/**
113
111
* Return {@code true} if the DeferredResult has been set.
112
+ * @since 4.0
114
113
*/
115
114
public boolean hasResult () {
116
115
return (this .result != RESULT_NONE );
@@ -120,6 +119,7 @@ public boolean hasResult() {
120
119
* Return the result, or {@code null} if the result wasn't set. Since the result
121
120
* can also be {@code null}, it is recommended to use {@link #hasResult()} first
122
121
* to check if there is a result prior to calling this method.
122
+ * @since 4.0
123
123
*/
124
124
public Object getResult () {
125
125
Object resultToCheck = this .result ;
@@ -134,22 +134,21 @@ final Long getTimeoutValue() {
134
134
}
135
135
136
136
/**
137
- * Register code to invoke when the async request times out. This method is
138
- * called from a container thread when an async request times out before the
139
- * {@code DeferredResult} has been set. It may invoke
140
- * {@link DeferredResult#setResult(Object) setResult} or
141
- * {@link DeferredResult#setErrorResult(Object) setErrorResult} to resume
142
- * processing.
137
+ * Register code to invoke when the async request times out.
138
+ * <p>This method is called from a container thread when an async request
139
+ * times out before the {@code DeferredResult} has been populated.
140
+ * It may invoke {@link DeferredResult#setResult setResult} or
141
+ * {@link DeferredResult#setErrorResult setErrorResult} to resume processing.
143
142
*/
144
143
public void onTimeout (Runnable callback ) {
145
144
this .timeoutCallback = callback ;
146
145
}
147
146
148
147
/**
149
- * Register code to invoke when the async request completes. This method is
150
- * called from a container thread when an async request completed for any
151
- * reason including timeout and network error. This method is useful for
152
- * detecting that a {@code DeferredResult} instance is no longer usable.
148
+ * Register code to invoke when the async request completes.
149
+ * <p>This method is called from a container thread when an async request
150
+ * completed for any reason including timeout and network error. This is useful
151
+ * for detecting that a {@code DeferredResult} instance is no longer usable.
153
152
*/
154
153
public void onCompletion (Runnable callback ) {
155
154
this .completionCallback = callback ;
@@ -178,8 +177,8 @@ public final void setResultHandler(DeferredResultHandler resultHandler) {
178
177
/**
179
178
* Set the value for the DeferredResult and handle it.
180
179
* @param result the value to set
181
- * @return "true" if the result was set and passed on for handling; "false"
182
- * if the result was already set or the async request expired.
180
+ * @return "true" if the result was set and passed on for handling;
181
+ * "false" if the result was already set or the async request expired
183
182
* @see #isSetOrExpired()
184
183
*/
185
184
public boolean setResult (T result ) {
@@ -200,13 +199,12 @@ private boolean setResultInternal(Object result) {
200
199
}
201
200
202
201
/**
203
- * Set an error value for the {@link DeferredResult} and handle it. The value
204
- * may be an {@link Exception} or {@link Throwable} in which case it will be
205
- * processed as if a handler raised the exception.
202
+ * Set an error value for the {@link DeferredResult} and handle it.
203
+ * The value may be an {@link Exception} or {@link Throwable} in which case
204
+ * it will be processed as if a handler raised the exception.
206
205
* @param result the error result value
207
206
* @return "true" if the result was set to the error value and passed on for
208
- * handling; "false" if the result was already set or the async request
209
- * expired.
207
+ * handling; "false" if the result was already set or the async request expired
210
208
* @see #isSetOrExpired()
211
209
*/
212
210
public boolean setErrorResult (Object result ) {
0 commit comments