REST with JAX-RS - 处理长时间运行的操作

REST with JAX-RS - Handling long running operations(REST with JAX-RS - 处理长时间运行的操作)

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

问题描述

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

感谢任何帮助.谢谢!

推荐答案

我认为 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();

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

本文标题为:REST with JAX-RS - 处理长时间运行的操作

基础教程推荐