亲宝软件园·资讯

展开

注解方式的异步请求 Spring中注解方式的异步请求

煎丶包 人气:0
想了解Spring中注解方式的异步请求的相关内容吗,煎丶包在本文为您仔细讲解注解方式的异步请求的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:注解方式的异步请求,Spring注解,下面大家一起来学习吧。

一、Servlet3.0异步请求

@WebServlet(value = "/async", asyncSupported = true)
public class HelloAsyncServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、设置支持异步处理asyncSupported = true
        //2、开启异步模式
        System.out.println("主线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
        AsyncContext startAsync = req.startAsync();
        //3、业务逻辑进行异步处理,开始异步处理
        startAsync.start(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("副线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
                    sayHello();
                    //获取到异步的上下文
                    AsyncContext asyncContext = req.getAsyncContext();
                    startAsync.complete();
                    ServletResponse response = asyncContext.getResponse();
                    response.getWriter().write("hello async!");
                    System.out.println("副线程结束:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
                } catch (Exception e) {
                    e.printStackTrace();
                }

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

    public void sayHello() throws Exception {
        System.out.println(Thread.currentThread() + "processing...");
        Thread.sleep(3000);
    }
}

打印结果:

在这里插入图片描述

二、SpringMVC的异步请求

返回Callable

@Controller
public class AsyncController {

    @ResponseBody
    @RequestMapping("/async01")
    public Callable<String> async01(){
        System.out.println("主线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());

        Callable<String> callable = new Callable<String>() {

            public String call() throws Exception {
                System.out.println("副线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());
                Thread.sleep(2000);
                System.out.println("副线程开始:" + Thread.currentThread() + "==>" + System.currentTimeMillis());

                return "async01";
            }
        };

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

    }
}

输出结果:

在这里插入图片描述

返回DeferredResult

模拟一个消息中间件

public class DeferredResultQueue {

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

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

/createOrder请求会暂时保存DeferredResultQueue中,/create请求会获取DeferredResultQueue中的请求,进行业务逻辑的处理并返回结果

@Controller
public class AsyncController {

    @ResponseBody
    @RequestMapping("/createOrder")
    public DeferredResult<Object> createOrder(){
        DeferredResult<Object> deferredResult = new DeferredResult<Object>((long)3000,"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;
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论