需要先配置SpringMVC支持异步访问,需要Servlet 3.0支持,导入3.0包
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
web.xml 也需要用3.0的配置,一下不支持 <async-supported>true</async-supported>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc1.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:component-scan base-package="com.smart.web"/>
<mvc:annotation-driven />
</beans>
Controller
package com.smart.web;
import java.util.concurrent.Callable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.smart.domain.User;
@RestController
@RequestMapping("/sp1")
public class Sp1Controller {
@RequestMapping("/m1")
public String m1(){
return "1";
}
@RequestMapping("/m2")
public Callable<User> m2(){
System.out.println("abc");
return new Callable<User>() {
@Override
public User call() throws Exception {
Thread.sleep(10L*1000);
User user = new User();
user.setUserId("1");
user.setUserName("tom");
return user;
}
};
}
}
m2方法,模拟延时10秒
测试:
package com.smart.test;
import org.junit.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate;
import com.smart.domain.User;
public class Sp1Test {
@Test
public void m2Test() throws InterruptedException{
AsyncRestTemplate template = new AsyncRestTemplate();
ListenableFuture<ResponseEntity<User>> future = template.getForEntity("http://localhost:8086/s3/sp1/m2", User.class);
future.addCallback(new ListenableFutureCallback<ResponseEntity<User>>() {
@Override
public void onSuccess(ResponseEntity<User> arg0) {
System.out.println("### handler OK"+arg0.getBody());
}
@Override
public void onFailure(Throwable arg0) {
System.out.println("### handler error"+arg0);
}
});
System.out.println("=========no wait");
Thread.sleep(20L*1000); //为了防止测试提前结束,关闭程序
}
}
使用junit测试,为防止主程序结束而结束整个执行,需要加入延时20秒
日志:
客户端:
[DEBUG] [2017-11-23 15:43:08,290] [org.springframework.web.client.AsyncRestTemplate main] - Created asynchronous GET request for "http://localhost:8086/s3/sp1/m2"
[DEBUG] [2017-11-23 15:43:08,383] [org.springframework.web.client.RestTemplate main] - Setting request Accept header to [application/json, application/*+json]
=========no wait
[DEBUG] [2017-11-23 15:43:18,411] [org.springframework.web.client.AsyncRestTemplate SimpleAsyncTaskExecutor-1] - Async GET request for "http://localhost:8086/s3/sp1/m2" resulted in 200 (OK)
[DEBUG] [2017-11-23 15:43:18,413] [org.springframework.web.client.RestTemplate SimpleAsyncTaskExecutor-1] - Reading [class com.smart.domain.User] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@66720ae]
### handler OKcom.smart.domain.User@321bc965
服务端
abc
[DEBUG] [2017-11-23 15:43:08,409] [org.springframework.web.context.request.async.WebAsyncManager http-bio-8080-exec-3] - Concurrent handling starting for GET [/s3/sp1/m2]
[DEBUG] [2017-11-23 15:43:08,409] [org.springframework.web.servlet.DispatcherServlet http-bio-8080-exec-3] - Leaving response open for concurrent processing
[DEBUG] [2017-11-23 15:43:18,406] [org.springframework.web.context.request.async.WebAsyncManager MvcAsync2] - Concurrent result value [com.smart.domain.User@646ce1c3] - dispatching request to resume processing
[DEBUG] [2017-11-23 15:43:18,406] [org.springframework.web.servlet.DispatcherServlet http-bio-8080-exec-4] - DispatcherServlet with name 'springmvc' resumed processing GET request for [/s3/sp1/m2]
[DEBUG] [2017-11-23 15:43:18,407] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping http-bio-8080-exec-4] - Looking up handler method for path /sp1/m2
[DEBUG] [2017-11-23 15:43:18,407] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping http-bio-8080-exec-4] - Returning handler method [public java.util.concurrent.Callable<com.smart.domain.User> com.smart.web.Sp1Controller.m2()]
[DEBUG] [2017-11-23 15:43:18,407] [org.springframework.beans.factory.support.DefaultListableBeanFactory http-bio-8080-exec-4] - Returning cached instance of singleton bean 'sp1Controller'
[DEBUG] [2017-11-23 15:43:18,407] [org.springframework.web.servlet.DispatcherServlet http-bio-8080-exec-4] - Last-Modified value for [/s3/sp1/m2] is: -1
[DEBUG] [2017-11-23 15:43:18,407] [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter http-bio-8080-exec-4] - Found concurrent result value [com.smart.domain.User@646ce1c3]
[DEBUG] [2017-11-23 15:43:18,408] [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor http-bio-8080-exec-4] - Written [com.smart.domain.User@646ce1c3] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2eab8f37]
[DEBUG] [2017-11-23 15:43:18,409] [org.springframework.web.servlet.DispatcherServlet http-bio-8080-exec-4] - Null ModelAndView returned to DispatcherServlet with name 'springmvc': assuming HandlerAdapter completed request handling
[DEBUG] [2017-11-23 15:43:18,409] [org.springframework.web.servlet.DispatcherServlet http-bio-8080-exec-4] - Successfully completed request