使用JAX-RS的REST - 处理长时间运行的操作 [英] REST with JAX-RS - Handling long running operations

查看:99
本文介绍了使用JAX-RS的REST - 处理长时间运行的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JAX-RS实现了REST服务。有些操作需要很长时间才能完成,可能需要15-30分钟。对于这些情况,我倾向于派遣后台线程来处理长时间运行的操作,然后立即响应HTTP状态202 ACCEPTED。响应将包含一个带有URL的位置标头,客户端可以使用该标头轮询进度。

I have a REST service implemented with JAX-RS. Some of the operations take a long time to complete, potentially 15-30 minutes. For these cases, my inclination is to dispatch a background thread to process the long running operation and then respond immediately with HTTP status 202 ACCEPTED. The response would contain a location header with a url that clients can use to poll for progress.

此方法需要创建线程来处理长时间运行的操作,例如202 ACCEPTED可以立即退回。我也知道在Java EE容器中创建自己的线程通常是不好的做法!

This approach requires the creation of threads to handle long running operations, such that 202 ACCEPTED can be returned immediately. I also know that that creating your own threads in a Java EE container is generally bad practice!

我的问题如下:


  1. 人们是否同意这是一种正确的做法?

  2. 假设这是正确的,人们可以推荐一个良好做法的解决方案,让我能够在后台调度长时间运行的操作并立即返回?

另外,为了避免管理我自己的线程,我调查了JAX -RS异步服务器api。不幸的是,尽管这提高了服务器吞吐量,但它不允许我立即响应ACCEPTED。

Also, to avoid managing my own threads, I looked into the JAX-RS asynchronous server api. Unfortunately, although this improves server throughput, it will not allow me to respond immediately with ACCEPTED.

泽西州声明如下:

Note that the use of server-side asynchronous processing model will not improve the 
request processing time perceived by the client. It will however increase the
throughput of the server, by releasing the initial request processing thread back to
the I/O container while the request may still be waiting in a queue for processing or    
the processing may still be running on another dedicated thread. The released I/O  
container thread can be used to accept and process new incoming request connections.

任何帮助表示赞赏。谢谢!

Any help is appreciated. Thanks!

推荐答案

我认为 Jersey Async docs 非常有用。这是一个简短的片段:

I think that Jersey Async docs exhaust the topic quite well. Here is a brief snippet:

@Path("/async/longRunning")
public class MyResource {

   @GET
   public void longRunningOp(@Suspended final AsyncResponse ar) {
       executor.submit(
            new Runnable() {
                public void run() {
                    executeLongRunningOp();
                    ar.resume("Hello async world!");
                } });
  }
}

当涉及到以下文档引文时:

When it comes to the following quotation from the docs:


请注意,使用服务器端异步处理模型
不会改善客户端感知的请求处理时间。( ...)

Note that the use of server-side asynchronous processing model will not improve the request processing time perceived by the client.(...)

我有点误解了它。文档的作者试图在这里表达的是,异步处理不会仅仅加速事物本身。但是可以使用例如以下内容立即返回响应:

I thing you have misunderstood it a bit. What the author of the docs tried to express here is that asynchroneous processing will not speed up things just by itself. But the response can be returned immediately using for instance the following:

return Response.status(Status.ACCEPTED).build();

这篇关于使用JAX-RS的REST - 处理长时间运行的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆