小编Scy*_*yla的帖子

Jersey:异步请求后立即响应

我试着理解异步响应与Jersey一起工作的方式.我阅读了Jersey文档的第10章(https://jersey.java.net/documentation/latest/async.html),但它对我的问题没有帮助.此处关于stackoverflow的研究也没有得到令人满意的答案(我能理解).

我要做的是类似于这篇文章中的一个问题(使用http状态202进行异步操作).我想使用HTML表单文档将大文件上传到服务器.在将请求发送到服务器之后,Web服务应立即响应状态202和URI,其中在请求完成后可以找到该文件.

阅读帖子后,似乎有可能,但遗憾的是没有提示如何在给定的情况下实现这样的行为.

我写了一个小的Web服务来测试功能:

    @Path("/test/async/")
    public class TestAsyncResponse {

        @GET
        @Path("get")
        public Response asyncGet(@Suspended final AsyncResponse response) {

            new Thread(new Runnable() {

                @Override
                public void run() {

                    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");

                    System.out.println("#### thread started: " 
                                       + df.format(new Date()) + " ####");
                    String result = veryExpensiveOperation();
                    System.out.println("#### thread finished: " 
                                        + df.format(new Date()) + " ####");

                    response.resume(result);
                }

                private String veryExpensiveOperation() {

                    try {

                        Thread.sleep(10000);
                    } 
                    catch (InterruptedException e) {

                        e.printStackTrace();
                    }

                    return "Woke up!"; …
Run Code Online (Sandbox Code Playgroud)

java rest multithreading asynchronous jersey

8
推荐指数
1
解决办法
3684
查看次数

标签 统计

asynchronous ×1

java ×1

jersey ×1

multithreading ×1

rest ×1