0

I have a requirement where, as part of a Web Service [ Java ], I enter details of a job into Database, which is asynchronously processed by a Windows Service [ C# ] and makes a HTTP Restful call to the Java web service notifying the status of the job.

The scenario:

  • Client makes a synchronous Restful call to the Java Web Service.
  • The Java Web Service enters the job details into database (similar to making an asynchronous call) and waits for a response back from the Windows Service (which is a new HTTP request to the Java Web Service).
  • Based on the response received, the Java Web Service needs to respond back to the client who is waiting on the synchronous call.

How can we achieve this in the Java Web Service?

EDIT: I've implemented restful web-service using Jersey framework and is running on a Jetty Server.

4

1 回答 1

0

根据您使用的 Jersey 和 Jetty 版本,您可以使用Servlet 3.0 中添加的异步处理请求支持。(根据Jetty Wikipedia 页面,我相信你至少需要 Jetty 8.x。根据这篇文章,我相信你至少需要 Jersey/JAX-RS 2.0。)

Jersey 文档JAX-RS API 文档提供了如何异步(即在另一个线程中)完成未完成请求的示例:

@Path("/resource")
public class AsyncResource {
    @GET
    public void asyncGet(@Suspended final AsyncResponse asyncResponse) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = veryExpensiveOperation();
                asyncResponse.resume(result);
            }

            private String veryExpensiveOperation() {
                // ... very expensive operation
            }
        }).start();
    }
}

在您的情况下,您将存储AsyncResponse对象的方式是,一旦您收到来自其他 Web 服务的响应,您可以通过调用resume您想要发送给客户端的任何响应来完成请求。

也可以看看:

异步 JAX-RS 的目的是什么

JAX-RS 和长轮询

于 2014-01-22T22:19:51.887 回答