springmvc-异步请求-返回Callable

package com.zdc.mvc.async.controller;

import java.util.concurrent.Callable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AsyncController {

    
    /**
     * 过程解析:
     *         1、控制器返回Callable;
     *         2、Spring异步处理,将Callable提交到TaskExecutor 使用一个隔离器的线程进行执行;
     *      3、DispatcherServlet和所有的Filter退出web容器的线程;但是response保持打开状态
     *      4、Callable返回结果,SpringMvc将请求重新派发给容器,恢复之前的处理;
     *      5、根据Callable返回结果。SpringMVC继续进行视图渲染流程等(从受请求---》视图渲染)
     * ############测试结果############################################
     *         preHandle------------------>在目标运行之前运行:http://localhost:8080/mvc/async
            主线程开始。。。。。。。。。。。。。。。。。。Thread[http-nio-8080-exec-4,5,main]==>1560258412199
            主线程结束。。。。。。。。。。。。。。。。。。Thread[http-nio-8080-exec-4,5,main]==>1560258412204
            副线程开始。。。。。。。。。。。。。。。。。。Thread[MvcAsync1,5,main]==>1560258412222
            副线程结束。。。。。。。。。。。。。。。。。。Thread[MvcAsync1,5,main]==>1560258414223
            preHandle------------------>在目标运行之前运行:http://localhost:8080/mvc/async
            postHandle-------------->在目标运行之后运行(Callable之前的返回值就是目标方法的返回值)
            afterCompletion--------------->在页面响应后运行

     */
    @ResponseBody
    @RequestMapping("/async")
    public Callable<String> asynCallable(){
        System.out.println("主线程开始。。。。。。。。。。。。。。。。。。"+Thread.currentThread()+"==>"+System.currentTimeMillis());
        Callable<String> callable = new Callable<String>() {

            @Override
            public String call() throws Exception {
                System.out.println("副线程开始。。。。。。。。。。。。。。。。。。"+Thread.currentThread()+"==>"+System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("副线程结束。。。。。。。。。。。。。。。。。。"+Thread.currentThread()+"==>"+System.currentTimeMillis());
                return "Callable<String> asynCallable";
            }
        };
        System.out.println("主线程结束。。。。。。。。。。。。。。。。。。"+Thread.currentThread()+"==>"+System.currentTimeMillis());
        return callable;
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值