欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring中注解方式的异步请求

程序员文章站 2022-06-23 09:04:49
一、servlet3.0异步请求@webservlet(value = "/async", asyncsupported = true)public class helloasyncservlet e...

一、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);
    }
}

打印结果:

Spring中注解方式的异步请求

二、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;

    }
}
  • 控制器返回callable
  • spring进行异步处理,将callable提交给taskexecutor,使用一个隔离的线程进行执行
  • dispatcherservlet和所有的filter退出web容器的线程,但是response保持打开状态
  • callable返回结果,spreingmvc将请求重新派发给容器,恢复之前的处理,callable返回值就是目标方法的返回值
  • 根据callable返回的结果,springmvc继续进行视图渲染流程等(从收到请求到视图渲染)

输出结果:

Spring中注解方式的异步请求

返回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;
    }
}

到此这篇关于spring中注解方式的异步请求的文章就介绍到这了,更多相关注解方式的异步请求内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!