Add interceptors for async processing

This change introduces two new interceptors with callback methods
for concurrent request handling. These interfaces are
CallableProcessingInterceptor and DeferredResultProcessingInterceptor.

Unlike a HandlerInterceptor, and its AsyncHandlerInterceptor sub-type,
which intercepts the invocation of a handler in he main request
processing thread, the two new interfaces are aimed at intercepting the
asynchronous execution of a Callable or a DeferredResult.

This allows for the registration of thread initialization logic in the
case of Callable executed with an AsyncTaskExecutor, or for centralized
tracking of the completion and/or expiration of a DeferredResult.
This commit is contained in:
Rossen Stoyanchev
2012-09-19 09:25:50 -04:00
parent 30bce7fa16
commit 57c36dd395
24 changed files with 735 additions and 267 deletions

View File

@@ -181,6 +181,7 @@ public class OpenSessionInViewTests {
replay(asyncWebRequest);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
asyncManager.setTaskExecutor(new SyncTaskExecutor());
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing(new Callable<String>() {
@@ -196,10 +197,6 @@ public class OpenSessionInViewTests {
// Async dispatch thread
reset(asyncWebRequest);
expect(asyncWebRequest.isDispatched()).andReturn(true);
replay(asyncWebRequest);
interceptor.preHandle(this.webRequest);
assertTrue("Session not bound to async thread", TransactionSynchronizationManager.hasResource(sf));
@@ -496,10 +493,10 @@ public class OpenSessionInViewTests {
asyncWebRequest.addCompletionHandler((Runnable) anyObject());
asyncWebRequest.startAsync();
expect(asyncWebRequest.isAsyncStarted()).andReturn(true).anyTimes();
expect(asyncWebRequest.isDispatched()).andReturn(false).anyTimes();
replay(asyncWebRequest);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
asyncManager.setTaskExecutor(new SyncTaskExecutor());
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing(new Callable<String>() {
public String call() throws Exception {
@@ -524,7 +521,6 @@ public class OpenSessionInViewTests {
expect(session.close()).andReturn(null);
expect(asyncWebRequest.isAsyncStarted()).andReturn(false).anyTimes();
expect(asyncWebRequest.isDispatched()).andReturn(true).anyTimes();
replay(sf);
replay(session);

View File

@@ -38,6 +38,7 @@ import javax.servlet.ServletResponse;
import junit.framework.TestCase;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
@@ -158,6 +159,7 @@ public class OpenEntityManagerInViewTests extends TestCase {
replay(asyncWebRequest);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(webRequest);
asyncManager.setTaskExecutor(new SyncTaskExecutor());
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing(new Callable<String>() {
public String call() throws Exception {
@@ -172,10 +174,6 @@ public class OpenEntityManagerInViewTests extends TestCase {
// Async dispatch thread
reset(asyncWebRequest);
expect(asyncWebRequest.isDispatched()).andReturn(true);
replay(asyncWebRequest);
reset(manager, factory);
replay(manager, factory);
@@ -348,10 +346,10 @@ public class OpenEntityManagerInViewTests extends TestCase {
asyncWebRequest.addCompletionHandler((Runnable) anyObject());
asyncWebRequest.startAsync();
expect(asyncWebRequest.isAsyncStarted()).andReturn(true).anyTimes();
expect(asyncWebRequest.isDispatched()).andReturn(false).anyTimes();
replay(asyncWebRequest);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.setTaskExecutor(new SyncTaskExecutor());
asyncManager.setAsyncWebRequest(asyncWebRequest);
asyncManager.startCallableProcessing(new Callable<String>() {
public String call() throws Exception {
@@ -372,7 +370,6 @@ public class OpenEntityManagerInViewTests extends TestCase {
reset(asyncWebRequest);
expect(asyncWebRequest.isAsyncStarted()).andReturn(false).anyTimes();
expect(asyncWebRequest.isDispatched()).andReturn(true).anyTimes();
replay(asyncWebRequest);
assertFalse(TransactionSynchronizationManager.hasResource(factory));
@@ -389,4 +386,12 @@ public class OpenEntityManagerInViewTests extends TestCase {
wac.close();
}
@SuppressWarnings("serial")
private static class SyncTaskExecutor extends SimpleAsyncTaskExecutor {
@Override
public void execute(Runnable task, long startTimeout) {
task.run();
}
}
}