Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java REST使用JAX-RS-处理长时间运行的操作_Java_Multithreading_Rest_Jersey_Jax Rs - Fatal编程技术网

Java REST使用JAX-RS-处理长时间运行的操作

Java REST使用JAX-RS-处理长时间运行的操作,java,multithreading,rest,jersey,jax-rs,Java,Multithreading,Rest,Jersey,Jax Rs,我用JAX-RS实现了一个REST服务。有些操作需要很长时间才能完成,可能需要15-30分钟。对于这些情况,我倾向于调度一个后台线程来处理长时间运行的操作,然后立即响应HTTP状态202 ACCEPTED。响应将包含一个带有url的位置标头,客户端可以使用该url轮询进度 这种方法需要创建线程来处理长时间运行的操作,以便可以立即返回。我还知道,在JavaEE容器中创建自己的线程通常是不好的做法 我的问题如下: 人们是否同意这是一种正确的方法 假设这是正确的,人们能推荐一个“良好实践”解决方案,使

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

这种方法需要创建线程来处理长时间运行的操作,以便可以立即返回。我还知道,在JavaEE容器中创建自己的线程通常是不好的做法

我的问题如下:

  • 人们是否同意这是一种正确的方法
  • 假设这是正确的,人们能推荐一个“良好实践”解决方案,使我能够在后台调度长时间运行的操作并立即返回吗
  • 另外,为了避免管理自己的线程,我研究了JAX-RS异步服务器api。不幸的是,尽管这提高了服务器吞吐量,但它不允许我立即用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.
    
    return Response.status(Status.ACCEPTED).build();
    
    感谢您的帮助。谢谢

    我还知道,在JavaEE容器中创建自己的线程通常是不好的做法

    虽然上述情况在大多数情况下都是正确的,但在这一次你别无选择。至少不要自己创建
    线程
    实例。让
    执行器服务
    为您执行。如果有的话,让这个
    ExecutorService
    有一个足够大的池,并与您的所有组件共享

    我认为这个话题很好。以下是一个简短的片段:

    @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!");
                    } });
      }
    }
    
    当涉及到以下文件中的报价时:

    注意,使用服务器端异步处理模型将 无法提高客户端感知到的请求处理时间。(…)

    我想你有点误解了。这些文档的作者在这里试图表达的是,异步处理本身不会加快速度。但可以使用以下方法立即返回响应:

    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.
    
    return Response.status(Status.ACCEPTED).build();
    

    我将创建一个方法,该方法立即返回带有进程id和完成时间的响应。计算在后台开始,并在完成后缓存一段时间。然后,客户端尝试检索具有特定id的信息并获得相关响应。

    谢谢。我同意应该使用ExecutorService之类的工具来管理线程。我仍然对派遣他们感到不安。此外,我正在使用EJB,EJB规范规定:•企业bean不得试图管理线程。企业bean不能尝试启动、停止、挂起或恢复线程,也不能更改线程的优先级或名称。企业bean不能试图管理线程组。我的Runnable可以是有状态会话bean吗?也许是吧?使用ExecutorService分派线程的类是否可以是无状态单例?否?@cmd With
    EJB
    ,也许可以看一下非常接近我要找的内容的感谢信息。@SotiriosDelimanolis请查看此信息并提供帮助。谢谢你抽出时间。返回202将无法到达客户端。我真的试过了!不起作用。不应该是公认的答案。Sotirios Delimanolis的回答是正确的。同意-这不能回答问题。请求者想要从他的方法返回202的代码。