Add beforeConcurrentHandling support

Previously CallableProcessingInterceptor and
DeferredResultProcessingInterceptor did not have support for capturing
the state of the original Thread just prior to processing. This made it
difficult to transfer the state of one Thread (i.e. ThreadLocal) to the
Thread used to process the Callable.

This commit adds a new method to CallableProcessingInterceptor and
DeferredResultProcessingInterceptor named beforeConcurrentHandling
which will be invoked on the original Thread used to submit the Callable
or DeferredResult. This means the state of the original Thread can be
captured in beforeConcurrentHandling and transfered to the new Thread
in preProcess.

Issue: SPR-10052
This commit is contained in:
Rob Winch
2012-11-29 11:55:53 -06:00
committed by Rossen Stoyanchev
parent 0d73d199ec
commit 1e62ad8665
9 changed files with 143 additions and 7 deletions

View File

@@ -66,7 +66,7 @@ public class WebAsyncManagerTests {
}
@Test
public void startAsyncProcessingWithoutAsyncWebRequest() {
public void startAsyncProcessingWithoutAsyncWebRequest() throws Exception {
WebAsyncManager manager = WebAsyncUtils.getAsyncManager(new MockHttpServletRequest());
try {
@@ -118,6 +118,7 @@ public class WebAsyncManagerTests {
Callable<Object> task = new StubCallable(concurrentResult);
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, task);
interceptor.preProcess(this.asyncWebRequest, task);
interceptor.postProcess(this.asyncWebRequest, task, new Integer(concurrentResult));
replay(interceptor);
@@ -140,6 +141,7 @@ public class WebAsyncManagerTests {
Callable<Object> task = new StubCallable(concurrentResult);
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, task);
interceptor.preProcess(this.asyncWebRequest, task);
interceptor.postProcess(this.asyncWebRequest, task, concurrentResult);
replay(interceptor);
@@ -155,6 +157,34 @@ public class WebAsyncManagerTests {
verify(interceptor, this.asyncWebRequest);
}
@Test
public void startCallableProcessingBeforeConcurrentHandlingException() throws Exception {
Callable<Object> task = new StubCallable(21);
Exception exception = new Exception();
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, task);
expectLastCall().andThrow(exception);
replay(interceptor);
this.asyncWebRequest.addTimeoutHandler((Runnable) notNull());
this.asyncWebRequest.addCompletionHandler((Runnable) notNull());
replay(this.asyncWebRequest);
this.asyncManager.registerCallableInterceptor("interceptor", interceptor);
try {
this.asyncManager.startCallableProcessing(task);
fail("Expected Exception");
}catch(Exception e) {
assertEquals(exception, e);
}
assertFalse(this.asyncManager.hasConcurrentResult());
verify(this.asyncWebRequest, interceptor);
}
@Test
public void startCallableProcessingPreProcessException() throws Exception {
@@ -162,6 +192,7 @@ public class WebAsyncManagerTests {
Exception exception = new Exception();
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, task);
interceptor.preProcess(this.asyncWebRequest, task);
expectLastCall().andThrow(exception);
replay(interceptor);
@@ -184,6 +215,7 @@ public class WebAsyncManagerTests {
Exception exception = new Exception();
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, task);
interceptor.preProcess(this.asyncWebRequest, task);
interceptor.postProcess(this.asyncWebRequest, task, 21);
expectLastCall().andThrow(exception);
@@ -207,11 +239,13 @@ public class WebAsyncManagerTests {
Exception exception = new Exception();
CallableProcessingInterceptor interceptor1 = createMock(CallableProcessingInterceptor.class);
interceptor1.beforeConcurrentHandling(this.asyncWebRequest, task);
interceptor1.preProcess(this.asyncWebRequest, task);
interceptor1.postProcess(this.asyncWebRequest, task, 21);
replay(interceptor1);
CallableProcessingInterceptor interceptor2 = createMock(CallableProcessingInterceptor.class);
interceptor2.beforeConcurrentHandling(this.asyncWebRequest, task);
interceptor2.preProcess(this.asyncWebRequest, task);
interceptor2.postProcess(this.asyncWebRequest, task, 21);
expectLastCall().andThrow(exception);
@@ -231,7 +265,7 @@ public class WebAsyncManagerTests {
}
@Test
public void startCallableProcessingWithAsyncTask() {
public void startCallableProcessingWithAsyncTask() throws Exception {
AsyncTaskExecutor executor = createMock(AsyncTaskExecutor.class);
expect(executor.submit((Runnable) notNull())).andReturn(null);
@@ -251,7 +285,7 @@ public class WebAsyncManagerTests {
}
@Test
public void startCallableProcessingNullInput() {
public void startCallableProcessingNullInput() throws Exception {
try {
this.asyncManager.startCallableProcessing((Callable<?>) null);
fail("Expected exception");
@@ -268,6 +302,7 @@ public class WebAsyncManagerTests {
String concurrentResult = "abc";
DeferredResultProcessingInterceptor interceptor = createStrictMock(DeferredResultProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
interceptor.preProcess(this.asyncWebRequest, deferredResult);
interceptor.postProcess(asyncWebRequest, deferredResult, concurrentResult);
replay(interceptor);
@@ -284,6 +319,36 @@ public class WebAsyncManagerTests {
verify(this.asyncWebRequest, interceptor);
}
@Test
public void startDeferredResultProcessingBeforeConcurrentHandlingException() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
Exception exception = new Exception();
DeferredResultProcessingInterceptor interceptor = createStrictMock(DeferredResultProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
expectLastCall().andThrow(exception);
replay(interceptor);
this.asyncWebRequest.addTimeoutHandler((Runnable) notNull());
this.asyncWebRequest.addCompletionHandler((Runnable) notNull());
replay(this.asyncWebRequest);
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
try {
this.asyncManager.startDeferredResultProcessing(deferredResult);
fail("Expected Exception");
}
catch(Exception success) {
assertEquals(exception, success);
}
assertFalse(this.asyncManager.hasConcurrentResult());
verify(this.asyncWebRequest, interceptor);
}
@Test
public void startDeferredResultProcessingPreProcessException() throws Exception {
@@ -291,6 +356,7 @@ public class WebAsyncManagerTests {
Exception exception = new Exception();
DeferredResultProcessingInterceptor interceptor = createStrictMock(DeferredResultProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
interceptor.preProcess(this.asyncWebRequest, deferredResult);
expectLastCall().andThrow(exception);
replay(interceptor);
@@ -313,6 +379,7 @@ public class WebAsyncManagerTests {
Exception exception = new Exception();
DeferredResultProcessingInterceptor interceptor = createStrictMock(DeferredResultProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
interceptor.preProcess(this.asyncWebRequest, deferredResult);
interceptor.postProcess(this.asyncWebRequest, deferredResult, 25);
expectLastCall().andThrow(exception);
@@ -330,7 +397,7 @@ public class WebAsyncManagerTests {
}
@Test
public void startDeferredResultProcessingNullInput() {
public void startDeferredResultProcessingNullInput() throws Exception {
try {
this.asyncManager.startDeferredResultProcessing((DeferredResult<?>) null);
fail("Expected exception");

View File

@@ -80,6 +80,7 @@ public class WebAsyncManagerTimeoutTests {
StubCallable callable = new StubCallable();
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, callable);
expect(interceptor.handleTimeout(this.asyncWebRequest, callable)).andReturn(RESULT_NONE);
interceptor.afterCompletion(this.asyncWebRequest, callable);
replay(interceptor);
@@ -123,6 +124,7 @@ public class WebAsyncManagerTimeoutTests {
StubCallable callable = new StubCallable();
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, callable);
expect(interceptor.handleTimeout(this.asyncWebRequest, callable)).andReturn(22);
replay(interceptor);
@@ -145,6 +147,7 @@ public class WebAsyncManagerTimeoutTests {
Exception exception = new Exception();
CallableProcessingInterceptor interceptor = createStrictMock(CallableProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, callable);
expect(interceptor.handleTimeout(this.asyncWebRequest, callable)).andThrow(exception);
replay(interceptor);
@@ -166,6 +169,7 @@ public class WebAsyncManagerTimeoutTests {
DeferredResult<Integer> deferredResult = new DeferredResult<Integer>();
DeferredResultProcessingInterceptor interceptor = createStrictMock(DeferredResultProcessingInterceptor.class);
interceptor.beforeConcurrentHandling(this.asyncWebRequest, deferredResult);
interceptor.preProcess(this.asyncWebRequest, deferredResult);
expect(interceptor.handleTimeout(this.asyncWebRequest, deferredResult)).andReturn(true);
interceptor.afterCompletion(this.asyncWebRequest, deferredResult);