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:
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.
Depending on which versions of Jersey and Jetty you are using, you may be able to use the support for asynchronously handling requests added in Servlet 3.0. (Based on the Jetty Wikipedia page , I believe you need at least Jetty 8.x. Based on this article , I believe you need at least Jersey/JAX-RS 2.0.)
The Jersey documentation and the JAX-RS API docs have examples of how to asynchronously (ie in another thread) complete an outstanding request:
@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();
}
}
In your case you would store the AsyncResponse
object in such a way that, once you have received the response from the other web service, you complete the request by calling resume
with whatever response you want to send to the client.
See also:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.