Spring MVC
DeferredResultProcessingInterceptor is used to intercept into DeferredResult processing life-cycle:
package com.logicbig.example;import org.springframework.web.context.request.NativeWebRequest;import org.springframework.web.context.request.async.DeferredResult;import org.springframework.web.context.request.async.DeferredResultProcessingInterceptor;public class MyDeferredResultProcessingInterceptor implements DeferredResultProcessingInterceptor { @Override public <T> void beforeConcurrentHandling (NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception { System.out.println("deferredInterceptor#beforeConcurrentHandler called." + "Thread: " + Thread.currentThread().getName()); } @Override public <T> void preProcess (NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception { System.out.println("deferredInterceptor#preProcess called." + "Thread: " + Thread.currentThread().getName()); } @Override public <T> void postProcess (NativeWebRequest request, DeferredResult<T> deferredResult, Object concurrentResult) throws Exception { System.out.println("deferredInterceptor#postProcess called." + "Thread: " + Thread.currentThread().getName()); } @Override public <T> boolean handleTimeout (NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception { System.out.println("deferredInterceptor#handleTimeout called." + "Thread: " + Thread.currentThread().getName()); return false; } @Override public <T> void afterCompletion (NativeWebRequest request, DeferredResult<T> deferredResult) throws Exception { System.out.println("deferredInterceptor#afterCompletion called." + "Thread: " + Thread.currentThread().getName()); }}
package com.logicbig.example;import org.springframework.web.context.request.async.WebAsyncManager;import org.springframework.web.context.request.async.WebAsyncUtils;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class MyAsyncHandlerInterceptor extends HandlerInterceptorAdapter { private static final Object DEFERRED_INTERCEPTOR_KEY = new Object(); @Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("interceptor#preHandle called." + " Thread: " + Thread.currentThread() .getName()); WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request); asyncManager.registerDeferredResultInterceptor(DEFERRED_INTERCEPTOR_KEY, new MyDeferredResultProcessingInterceptor()); return true; } @Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("interceptor#postHandle called." + "Thread: " + Thread.currentThread() .getName()); } @Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("interceptor#afterCompletion called Thread.: " + Thread.currentThread() .getName()); } @Override public void afterConcurrentHandlingStarted (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("interceptor#afterConcurrentHandlingStarted called. " + "Thread: " + Thread.currentThread() .getName()); }}
package com.logicbig.example;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.context.request.async.DeferredResult;@Controllerpublic class MyWebController { @RequestMapping("/") @ResponseBody public DeferredResult<String> handleTestRequest () { System.out.println("controller#handler called. Thread: " + Thread.currentThread() .getName()); final DeferredResult<String> deferredResult = new DeferredResult<>(); new Thread(() -> { System.out.println("controller-deferred#async task started. Thread: " + Thread.currentThread() .getName()); try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } deferredResult.setResult("test async result"); System.out.println("controller-deferred#async task finished"); }).start(); System.out.println("controller#handler finished"); return deferredResult; }}
package com.logicbig.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;public class Main { public static void main (String[] args) { SpringApplication.run(BootApplication.class, args); } @SpringBootApplication public static class BootApplication extends WebMvcConfigurerAdapter { @Override public void addInterceptors (InterceptorRegistry registry) { registry.addInterceptor(new MyAsyncHandlerInterceptor()); } }}
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.logicbig.example</groupId> <artifactId>async-deferred-process-interceptor</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies></project>