Close

Spring MVC - CallableProcessingInterceptor Examples

Spring MVC 

CallableProcessingInterceptor is registered in HandlerInterceptor#preHandle to intercept more deeply into async Callable life-cycle request.

package com.logicbig.example;

import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.CallableProcessingInterceptor;

import java.util.concurrent.Callable;

public class MyCallableProcessingInterceptor implements
CallableProcessingInterceptor {
@Override
public <T> void beforeConcurrentHandling (
NativeWebRequest request,
Callable<T> task) throws Exception {
System.out.println("callableInterceptor#beforeConcurrentHandling called. " +
"Thread: " + Thread.currentThread().getName());
}

@Override
public <T> void preProcess (
NativeWebRequest request,
Callable<T> task) throws Exception {

System.out.println("callableInterceptor#preProcess called. " +
" Thread: " + Thread.currentThread().getName());
}

@Override
public <T> void postProcess (NativeWebRequest request,
Callable<T> task,
Object concurrentResult) throws Exception {
System.out.println("callableInterceptor#postProcess called. " +
" Thread: " + Thread.currentThread().getName());
}

@Override
public <T> Object handleTimeout (NativeWebRequest request,
Callable<T> task) throws Exception {

System.out.println("callableInterceptor#handleTimeout called." +
" Thread: " + Thread.currentThread().getName());

return RESULT_NONE;
}

@Override
public <T> void afterCompletion (NativeWebRequest request,
Callable<T> task) throws Exception {
System.out.println("callableInterceptor#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 CALLABLE_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.registerCallableInterceptor(CALLABLE_INTERCEPTOR_KEY,
new MyCallableProcessingInterceptor());
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 java.util.concurrent.Callable;

@Controller
public class MyWebController {
@RequestMapping("/")
@ResponseBody
public Callable<String> handleTestRequest () {

System.out.println("controller#handler called." +
"Thread: " + Thread.currentThread()
.getName());

Callable<String> callable = new Callable<String>() {
@Override
public String call () throws Exception {
System.out.println("controller-callable#async task started." +
" Thread: " + Thread.currentThread()
.getName());
Thread.sleep(300);
System.out.println("controller-callable#async task finished");
return "async result";
}
};

System.out.println("controller#handler finished");
return callable;
}
}

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());
}
}
}

pom.xml:

<?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-callable-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>
Original Post




See Also