Skip to content

Commit bf99d6a

Browse files
committed
WebAsyncManager avoids concurrentResult.toString() and builds correct DeferredResultProcessingInterceptor keys
Issue: SPR-12253
1 parent 92f7121 commit bf99d6a

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncManager.java

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,18 +13,19 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.web.context.request.async;
1718

1819
import java.util.ArrayList;
1920
import java.util.LinkedHashMap;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.concurrent.Callable;
23-
2424
import javax.servlet.http.HttpServletRequest;
2525

2626
import org.apache.commons.logging.Log;
2727
import org.apache.commons.logging.LogFactory;
28+
2829
import org.springframework.core.task.AsyncTaskExecutor;
2930
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3031
import org.springframework.util.Assert;
@@ -48,7 +49,6 @@
4849
*
4950
* @author Rossen Stoyanchev
5051
* @since 3.2
51-
*
5252
* @see org.springframework.web.context.request.AsyncWebRequestInterceptor
5353
* @see org.springframework.web.servlet.AsyncHandlerInterceptor
5454
* @see org.springframework.web.filter.OncePerRequestFilter#shouldNotFilterAsyncDispatch
@@ -85,21 +85,21 @@ public final class WebAsyncManager {
8585

8686

8787
/**
88-
* Package private constructor.
88+
* Package-private constructor.
8989
* @see WebAsyncUtils#getAsyncManager(javax.servlet.ServletRequest)
9090
* @see WebAsyncUtils#getAsyncManager(org.springframework.web.context.request.WebRequest)
9191
*/
9292
WebAsyncManager() {
9393
}
9494

95+
9596
/**
9697
* Configure the {@link AsyncWebRequest} to use. This property may be set
9798
* more than once during a single request to accurately reflect the current
9899
* state of the request (e.g. following a forward, request/response
99100
* wrapping, etc). However, it should not be set while concurrent handling
100101
* is in progress, i.e. while {@link #isConcurrentHandlingStarted()} is
101102
* {@code true}.
102-
*
103103
* @param asyncWebRequest the web request to use
104104
*/
105105
public void setAsyncWebRequest(final AsyncWebRequest asyncWebRequest) {
@@ -144,7 +144,6 @@ public boolean hasConcurrentResult() {
144144

145145
/**
146146
* Provides access to the result from concurrent handling.
147-
*
148147
* @return an Object, possibly an {@code Exception} or {@code Throwable} if
149148
* concurrent handling raised one.
150149
* @see #clearConcurrentResult()
@@ -156,7 +155,6 @@ public Object getConcurrentResult() {
156155
/**
157156
* Provides access to additional processing context saved at the start of
158157
* concurrent handling.
159-
*
160158
* @see #clearConcurrentResult()
161159
*/
162160
public Object[] getConcurrentResultContext() {
@@ -217,14 +215,14 @@ public void registerDeferredResultInterceptor(Object key, DeferredResultProcessi
217215
}
218216

219217
/**
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.
222220
* @param interceptors one or more interceptors to register
223221
*/
224222
public void registerDeferredResultInterceptors(DeferredResultProcessingInterceptor... interceptors) {
225223
Assert.notNull(interceptors, "A DeferredResultProcessingInterceptor is required");
226224
for (DeferredResultProcessingInterceptor interceptor : interceptors) {
227-
String key = interceptors.getClass().getName() + ":" + interceptors.hashCode();
225+
String key = interceptor.getClass().getName() + ":" + interceptor.hashCode();
228226
this.deferredResultInterceptors.put(key, interceptor);
229227
}
230228
}
@@ -244,12 +242,10 @@ public void clearConcurrentResult() {
244242
* from the task execution is saved and the request dispatched in order to
245243
* resume processing of that result. If the task raises an Exception then
246244
* the saved result will be the raised Exception.
247-
*
248245
* @param callable a unit of work to be executed asynchronously
249246
* @param processingContext additional context to save that can be accessed
250247
* via {@link #getConcurrentResultContext()}
251-
* @throws Exception If concurrent processing failed to start
252-
*
248+
* @throws Exception if concurrent processing failed to start
253249
* @see #getConcurrentResult()
254250
* @see #getConcurrentResultContext()
255251
*/
@@ -263,11 +259,10 @@ public void startCallableProcessing(final Callable<?> callable, Object... proces
263259
* Use the given {@link WebAsyncTask} to configure the task executor as well as
264260
* the timeout value of the {@code AsyncWebRequest} before delegating to
265261
* {@link #startCallableProcessing(Callable, Object...)}.
266-
*
267262
* @param webAsyncTask a WebAsyncTask containing the target {@code Callable}
268263
* @param processingContext additional context to save that can be accessed
269264
* via {@link #getConcurrentResultContext()}
270-
* @throws Exception If concurrent processing failed to start
265+
* @throws Exception if concurrent processing failed to start
271266
*/
272267
public void startCallableProcessing(final WebAsyncTask<?> webAsyncTask, Object... processingContext) throws Exception {
273268
Assert.notNull(webAsyncTask, "WebAsyncTask must not be null");
@@ -309,8 +304,7 @@ public void run() {
309304
}
310305
});
311306

312-
interceptorChain.applyBeforeConcurrentHandling(asyncWebRequest, callable);
313-
307+
interceptorChain.applyBeforeConcurrentHandling(this.asyncWebRequest, callable);
314308
startAsyncProcessing(processingContext);
315309

316310
this.taskExecutor.submit(new Runnable() {
@@ -321,8 +315,8 @@ public void run() {
321315
interceptorChain.applyPreProcess(asyncWebRequest, callable);
322316
result = callable.call();
323317
}
324-
catch (Throwable t) {
325-
result = t;
318+
catch (Throwable ex) {
319+
result = ex;
326320
}
327321
finally {
328322
result = interceptorChain.applyPostProcess(asyncWebRequest, callable, result);
@@ -337,18 +331,20 @@ private void setConcurrentResultAndDispatch(Object result) {
337331
if (hasConcurrentResult()) {
338332
return;
339333
}
340-
concurrentResult = result;
334+
this.concurrentResult = result;
341335
}
342336

343-
if (asyncWebRequest.isAsyncComplete()) {
337+
if (this.asyncWebRequest.isAsyncComplete()) {
344338
logger.error("Could not complete async processing due to timeout or network error");
345339
return;
346340
}
347341

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+
}
350346

351-
asyncWebRequest.dispatch();
347+
this.asyncWebRequest.dispatch();
352348
}
353349

354350
/**
@@ -358,12 +354,10 @@ private void setConcurrentResultAndDispatch(Object result) {
358354
* result. The {@code AsyncWebRequest} is also updated with a completion
359355
* handler that expires the {@code DeferredResult} and a timeout handler
360356
* assuming the {@code DeferredResult} has a default timeout result.
361-
*
362357
* @param deferredResult the DeferredResult instance to initialize
363358
* @param processingContext additional context to save that can be accessed
364359
* via {@link #getConcurrentResultContext()}
365-
* @throws Exception If concurrent processing failed to start
366-
*
360+
* @throws Exception if concurrent processing failed to start
367361
* @see #getConcurrentResult()
368362
* @see #getConcurrentResultContext()
369363
*/
@@ -391,8 +385,8 @@ public void run() {
391385
try {
392386
interceptorChain.triggerAfterTimeout(asyncWebRequest, deferredResult);
393387
}
394-
catch (Throwable t) {
395-
setConcurrentResultAndDispatch(t);
388+
catch (Throwable ex) {
389+
setConcurrentResultAndDispatch(ex);
396390
}
397391
}
398392
});
@@ -404,8 +398,7 @@ public void run() {
404398
}
405399
});
406400

407-
interceptorChain.applyBeforeConcurrentHandling(asyncWebRequest, deferredResult);
408-
401+
interceptorChain.applyBeforeConcurrentHandling(this.asyncWebRequest, deferredResult);
409402
startAsyncProcessing(processingContext);
410403

411404
try {
@@ -418,16 +411,14 @@ public void handleResult(Object result) {
418411
}
419412
});
420413
}
421-
catch (Throwable t) {
422-
setConcurrentResultAndDispatch(t);
414+
catch (Throwable ex) {
415+
setConcurrentResultAndDispatch(ex);
423416
}
424417
}
425418

426419
private void startAsyncProcessing(Object[] processingContext) {
427-
428420
clearConcurrentResult();
429421
this.concurrentResultContext = processingContext;
430-
431422
this.asyncWebRequest.startAsync();
432423

433424
if (logger.isDebugEnabled()) {

0 commit comments

Comments
 (0)