• SpringMVC异步调用,Callable和DeferredResult的使用


    Callable和DeferredResult都是springMVC里面的异步调用,Callable主要用来处理一些简单的逻辑,DeferredResult主要用于处理一些复杂逻辑

    1.Callable的使用

    github地址:https://github.com/showkawa/spring-annotation/blob/master/src/main/java/com/brian/async/Callable01.java

    package com.brian.async;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.concurrent.Callable;
    
    /**
     * SpringMVC异步调用 Callable =>执行过程
     * 1.控制器返回Callable
     * 2.Spring异步调用,将Callable提交到TaskExecutor使用一个隔离的线程进行执行
     * 3.DispatcherServlet和所有的Filter退出web容器的线程,而response保持打开的状态
     * 4.Callable返回结果,SpringMVC将请求重新派发给容器,并且恢复之前的处理。
     * 5.根据Callable返回的结果。SpringMVC继续进行试图渲染流程等等等等。
     */
    @Controller
    public class Callable01 {
    
        @ResponseBody
        @RequestMapping("/async01")
        public Callable<String> async01() {
    
            System.out.println("主线程start...." + Thread.currentThread()+"-"+System.currentTimeMillis());
            Callable<String> callable = new Callable<String>() {
    
                public String call() throws Exception {
                    System.out.println("callable线程start...." + Thread.currentThread() + "-" + System.currentTimeMillis());
                    Thread.sleep(3000);
                    System.out.println("callable线程end...." + Thread.currentThread() + "-" + System.currentTimeMillis());
                    return "ok";
                }
            };
            System.out.println("主线程end...." + Thread.currentThread()+"-"+System.currentTimeMillis());
            return callable;
        }
    
    }

    2.DeferredResult的使用

    github地址:https://github.com/showkawa/spring-annotation/blob/master/src/main/java/com/brian/async/DeferredResult02.java

    package com.brian.async;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.context.request.async.DeferredResult;
    
    import java.util.UUID;
    
    /**
     * DeferredResult异步调用
     *
     */
    @Controller
    public class DeferredResult02 {
    
        @RequestMapping("/create")
        public DeferredResult<Object> createOrder() {
            DeferredResult<Object> deferredResult =
                    new DeferredResult<Object>((long)15000,"create order fail");
            //存放到模拟的队列中
            ResultQueue.add(deferredResult);
            return deferredResult;
        }
    
    
        @RequestMapping("/create/action")
        public String createAction() {
            DeferredResult<Object> deferredResult = ResultQueue.get();
            String order = UUID.randomUUID().toString();
            deferredResult.setResult(order);
            return " create order id: " + order;
        }
    
    }
  • 相关阅读:
    CWMP开源代码研究6——libcwmp动态库开发
    CWMP开源代码研究5——CWMP程序设计思想
    CWMP开源代码研究4——认证流程
    CWMP开源代码研究2——easycwmp安装和学习
    CWMP开源代码研究3——ACS介绍
    CWMP开源代码研究1——开篇之作
    usb驱动开发之大结局
    usb驱动开发24之接口驱动
    usb驱动开发23之驱动生命线
    usb驱动开发22之驱动生命线
  • 原文地址:https://www.cnblogs.com/hlkawa/p/9699605.html
Copyright © 2020-2023  润新知