1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+
16
17
package org .springframework .web .context .request .async ;
17
18
18
19
import java .util .ArrayList ;
19
20
import java .util .LinkedHashMap ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .concurrent .Callable ;
23
-
24
24
import javax .servlet .http .HttpServletRequest ;
25
25
26
26
import org .apache .commons .logging .Log ;
27
27
import org .apache .commons .logging .LogFactory ;
28
+
28
29
import org .springframework .core .task .AsyncTaskExecutor ;
29
30
import org .springframework .core .task .SimpleAsyncTaskExecutor ;
30
31
import org .springframework .util .Assert ;
48
49
*
49
50
* @author Rossen Stoyanchev
50
51
* @since 3.2
51
- *
52
52
* @see org.springframework.web.context.request.AsyncWebRequestInterceptor
53
53
* @see org.springframework.web.servlet.AsyncHandlerInterceptor
54
54
* @see org.springframework.web.filter.OncePerRequestFilter#shouldNotFilterAsyncDispatch
@@ -85,21 +85,21 @@ public final class WebAsyncManager {
85
85
86
86
87
87
/**
88
- * Package private constructor.
88
+ * Package- private constructor.
89
89
* @see WebAsyncUtils#getAsyncManager(javax.servlet.ServletRequest)
90
90
* @see WebAsyncUtils#getAsyncManager(org.springframework.web.context.request.WebRequest)
91
91
*/
92
92
WebAsyncManager () {
93
93
}
94
94
95
+
95
96
/**
96
97
* Configure the {@link AsyncWebRequest} to use. This property may be set
97
98
* more than once during a single request to accurately reflect the current
98
99
* state of the request (e.g. following a forward, request/response
99
100
* wrapping, etc). However, it should not be set while concurrent handling
100
101
* is in progress, i.e. while {@link #isConcurrentHandlingStarted()} is
101
102
* {@code true}.
102
- *
103
103
* @param asyncWebRequest the web request to use
104
104
*/
105
105
public void setAsyncWebRequest (final AsyncWebRequest asyncWebRequest ) {
@@ -144,7 +144,6 @@ public boolean hasConcurrentResult() {
144
144
145
145
/**
146
146
* Provides access to the result from concurrent handling.
147
- *
148
147
* @return an Object, possibly an {@code Exception} or {@code Throwable} if
149
148
* concurrent handling raised one.
150
149
* @see #clearConcurrentResult()
@@ -156,7 +155,6 @@ public Object getConcurrentResult() {
156
155
/**
157
156
* Provides access to additional processing context saved at the start of
158
157
* concurrent handling.
159
- *
160
158
* @see #clearConcurrentResult()
161
159
*/
162
160
public Object [] getConcurrentResultContext () {
@@ -217,14 +215,14 @@ public void registerDeferredResultInterceptor(Object key, DeferredResultProcessi
217
215
}
218
216
219
217
/**
220
- * Register a {@link DeferredResultProcessingInterceptor} without a key.
221
- * The key is derived from the class name and hashcode .
218
+ * Register one or more {@link DeferredResultProcessingInterceptor}s without a specified key.
219
+ * The default key is derived from the interceptor class name and hash code .
222
220
* @param interceptors one or more interceptors to register
223
221
*/
224
222
public void registerDeferredResultInterceptors (DeferredResultProcessingInterceptor ... interceptors ) {
225
223
Assert .notNull (interceptors , "A DeferredResultProcessingInterceptor is required" );
226
224
for (DeferredResultProcessingInterceptor interceptor : interceptors ) {
227
- String key = interceptors .getClass ().getName () + ":" + interceptors .hashCode ();
225
+ String key = interceptor .getClass ().getName () + ":" + interceptor .hashCode ();
228
226
this .deferredResultInterceptors .put (key , interceptor );
229
227
}
230
228
}
@@ -244,12 +242,10 @@ public void clearConcurrentResult() {
244
242
* from the task execution is saved and the request dispatched in order to
245
243
* resume processing of that result. If the task raises an Exception then
246
244
* the saved result will be the raised Exception.
247
- *
248
245
* @param callable a unit of work to be executed asynchronously
249
246
* @param processingContext additional context to save that can be accessed
250
247
* via {@link #getConcurrentResultContext()}
251
- * @throws Exception If concurrent processing failed to start
252
- *
248
+ * @throws Exception if concurrent processing failed to start
253
249
* @see #getConcurrentResult()
254
250
* @see #getConcurrentResultContext()
255
251
*/
@@ -263,11 +259,10 @@ public void startCallableProcessing(final Callable<?> callable, Object... proces
263
259
* Use the given {@link WebAsyncTask} to configure the task executor as well as
264
260
* the timeout value of the {@code AsyncWebRequest} before delegating to
265
261
* {@link #startCallableProcessing(Callable, Object...)}.
266
- *
267
262
* @param webAsyncTask a WebAsyncTask containing the target {@code Callable}
268
263
* @param processingContext additional context to save that can be accessed
269
264
* via {@link #getConcurrentResultContext()}
270
- * @throws Exception If concurrent processing failed to start
265
+ * @throws Exception if concurrent processing failed to start
271
266
*/
272
267
public void startCallableProcessing (final WebAsyncTask <?> webAsyncTask , Object ... processingContext ) throws Exception {
273
268
Assert .notNull (webAsyncTask , "WebAsyncTask must not be null" );
@@ -309,8 +304,7 @@ public void run() {
309
304
}
310
305
});
311
306
312
- interceptorChain .applyBeforeConcurrentHandling (asyncWebRequest , callable );
313
-
307
+ interceptorChain .applyBeforeConcurrentHandling (this .asyncWebRequest , callable );
314
308
startAsyncProcessing (processingContext );
315
309
316
310
this .taskExecutor .submit (new Runnable () {
@@ -321,8 +315,8 @@ public void run() {
321
315
interceptorChain .applyPreProcess (asyncWebRequest , callable );
322
316
result = callable .call ();
323
317
}
324
- catch (Throwable t ) {
325
- result = t ;
318
+ catch (Throwable ex ) {
319
+ result = ex ;
326
320
}
327
321
finally {
328
322
result = interceptorChain .applyPostProcess (asyncWebRequest , callable , result );
@@ -337,18 +331,20 @@ private void setConcurrentResultAndDispatch(Object result) {
337
331
if (hasConcurrentResult ()) {
338
332
return ;
339
333
}
340
- concurrentResult = result ;
334
+ this . concurrentResult = result ;
341
335
}
342
336
343
- if (asyncWebRequest .isAsyncComplete ()) {
337
+ if (this . asyncWebRequest .isAsyncComplete ()) {
344
338
logger .error ("Could not complete async processing due to timeout or network error" );
345
339
return ;
346
340
}
347
341
348
- logger .debug ("Concurrent result value [" + concurrentResult + "]" );
349
- logger .debug ("Dispatching request to resume processing" );
342
+ if (logger .isDebugEnabled ()) {
343
+ logger .debug ("Concurrent result value [" + this .concurrentResult +
344
+ "] - dispatching request to resume processing" );
345
+ }
350
346
351
- asyncWebRequest .dispatch ();
347
+ this . asyncWebRequest .dispatch ();
352
348
}
353
349
354
350
/**
@@ -358,12 +354,10 @@ private void setConcurrentResultAndDispatch(Object result) {
358
354
* result. The {@code AsyncWebRequest} is also updated with a completion
359
355
* handler that expires the {@code DeferredResult} and a timeout handler
360
356
* assuming the {@code DeferredResult} has a default timeout result.
361
- *
362
357
* @param deferredResult the DeferredResult instance to initialize
363
358
* @param processingContext additional context to save that can be accessed
364
359
* via {@link #getConcurrentResultContext()}
365
- * @throws Exception If concurrent processing failed to start
366
- *
360
+ * @throws Exception if concurrent processing failed to start
367
361
* @see #getConcurrentResult()
368
362
* @see #getConcurrentResultContext()
369
363
*/
@@ -391,8 +385,8 @@ public void run() {
391
385
try {
392
386
interceptorChain .triggerAfterTimeout (asyncWebRequest , deferredResult );
393
387
}
394
- catch (Throwable t ) {
395
- setConcurrentResultAndDispatch (t );
388
+ catch (Throwable ex ) {
389
+ setConcurrentResultAndDispatch (ex );
396
390
}
397
391
}
398
392
});
@@ -404,8 +398,7 @@ public void run() {
404
398
}
405
399
});
406
400
407
- interceptorChain .applyBeforeConcurrentHandling (asyncWebRequest , deferredResult );
408
-
401
+ interceptorChain .applyBeforeConcurrentHandling (this .asyncWebRequest , deferredResult );
409
402
startAsyncProcessing (processingContext );
410
403
411
404
try {
@@ -418,16 +411,14 @@ public void handleResult(Object result) {
418
411
}
419
412
});
420
413
}
421
- catch (Throwable t ) {
422
- setConcurrentResultAndDispatch (t );
414
+ catch (Throwable ex ) {
415
+ setConcurrentResultAndDispatch (ex );
423
416
}
424
417
}
425
418
426
419
private void startAsyncProcessing (Object [] processingContext ) {
427
-
428
420
clearConcurrentResult ();
429
421
this .concurrentResultContext = processingContext ;
430
-
431
422
this .asyncWebRequest .startAsync ();
432
423
433
424
if (logger .isDebugEnabled ()) {
0 commit comments