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 (i.e. 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: