需要先配置SpringMVC支持异步访问,需要Servlet 3.0支持,导入3.0包
web.xml 也需要用3.0的配置,一下不支持
Archetype Created Web Application
contextConfigLocation
classpath:spring/spring.xml
org.springframework.web.context.ContextLoaderListener
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-mvc1.xml
1
true
springmvc
/
spring配置
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 m2(){
System.out.println("abc");
return new Callable() {
@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> future = template.getForEntity("http://localhost:8086/s3/sp1/m2", User.class);
future.addCallback(new ListenableFutureCallback>() {
@Override
public void onSuccess(ResponseEntity 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); //为了防止测试提前结束,关闭程序
}
}
日志:
客户端:
[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
[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