一、SpringMVC 异步请求-返回 Callable

@Controller
public class AsyncController {

    /**
     * 1、控制器返回Callable
     * 2、Spring异步处理,将Callable 提交到 TaskExecutor 使用一个隔离的线程进行执行
     * 3、DispatcherServlet和所有的Filter退出web容器的线程,但是response 保持打开状态;
     * 4、Callable返回结果,SpringMVC将请求重新派发给容器,恢复之前的处理;
     * 5、根据Callable返回的结果。SpringMVC继续进行视图渲染流程等(从收请求-视图渲染)。
     *
     * preHandle.../springmvc-annotation/async01
     主线程开始...Thread[http-bio-8081-exec-3,5,main]==>1513932494700
     主线程结束...Thread[http-bio-8081-exec-3,5,main]==>1513932494700
     =========DispatcherServlet及所有的Filter退出线程============================

     ================等待Callable执行==========
     副线程开始...Thread[MvcAsync1,5,main]==>1513932494707
     副线程开始...Thread[MvcAsync1,5,main]==>1513932496708
     ================Callable执行完成==========

     ================再次收到之前重发过来的请求========
     preHandle.../springmvc-annotation/async01
     postHandle...(Callable的之前的返回值就是目标方法的返回值)
     afterCompletion...

     异步的拦截器:
     1)、原生API的AsyncListener
     2)、SpringMVC:实现AsyncHandlerInterceptor;
     * @return
     */

    @ResponseBody
    @RequestMapping("/async01")
    public Callable<String> async01(){
        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> async01()";
            }
        };

        System.out.println("主线程结束..."+Thread.currentThread()+"==>"+System.currentTimeMillis());
        return callable;
    }

}

处理流程:

1、控制器返回 Callable
2、Spring 异步处理,将 Callable 提交到 TaskExecutor 使用一个隔离的线程进行执行
3、DispatcherServlet 和所有的 Filter 退出 web 容器的线程,但是 response 保持打开状态;
4、Callable 返回结果,SpringMVC 将请求重新派发给容器,恢复之前的处理;
5、根据 Callable 返回的结果。SpringMVC 继续进行视图渲染流程等(从收请求-视图渲染)。

图解:

二、SpringMVC 异步请求-返回 DeferredResult

模拟场景:

模拟队列:

//模拟消息队列
public class DeferredResultQueue {

    private static Queue<DeferredResult<Object>> queue = new ConcurrentLinkedQueue<DeferredResult<Object>>();

    public static void save(DeferredResult<Object> deferredResult){
        queue.add(deferredResult);
    }

    public static DeferredResult<Object> get( ){
        return queue.poll();
    }
}

处理业务:

@Controller
public class OrderController {

    @ResponseBody
    @RequestMapping("/createOrder")
    public DeferredResult<Object> createOrder(){
        DeferredResult<Object> deferredResult = new DeferredResult<>((long)5000"create fail...");

        DeferredResultQueue.save(deferredResult);

        return deferredResult;
    }

    @ResponseBody
    @RequestMapping("/create")
    public String create(){
        //创建订单
        String order = UUID.randomUUID().toString();
        DeferredResult<Object> deferredResult = DeferredResultQueue.get();
        deferredResult.setResult(order);
        return "success===>"+order;
    }

}

参考文档:

SpringMVC 之 异步请求 1

SpringMVC 之 异步请求 2

posted on 2021-11-23 14:52  格物致知_Tony  阅读(176)  评论(0编辑  收藏  举报