Show Table of Contents
46.6. Asynchronous Processing on the Client
Overview
JAX-RS 2.0 supports asynchronous processing of invocations on the client side. Two different styles of asynchronous processing are supported: either using a
java.util.concurrent.Future<V> return value; or by registering an invocation callback.
Asynchronous invocation with Future<V> return value
Using the
Future<V> approach to asynchronous processing, you can invoke a client request asynchronously, as follows:
// Java
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import java.util.concurrent.Future;
import javax.ws.rs.core.Response;
...
Client client = ClientBuilder.newClient();
Future<Response> futureResp = client.target("http://example.org/bookstore/books/123")
.request("application/xml")
.async()
.get();
...
// At a later time, check (and wait) for the response:
Response resp = futureResp.get();
You can use a similar approach for typed responses. For example, to get a response of type,
BookInfo:
Client client = ClientBuilder.newClient();
Future<BookInfo> futureResp = client.target("http://example.org/bookstore/books/123")
.request("application/xml")
.async()
.get(BookInfo.class);
...
// At a later time, check (and wait) for the response:
BookInfo resp = futureResp.get();Asynchronous invocation with invocation callback
Instead of accessing the return value using a Future<V> object, you can define an invocation callback (using
javax.ws.rs.client.InvocationCallback<RESPONSE>), as follows:
// Java
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import java.util.concurrent.Future;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.InvocationCallback;
...
Client client = ClientBuilder.newClient();
Future<Response> futureResp = client.target("http://example.org/bookstore/books/123")
.request("application/xml")
.async()
.get(
new InvocationCallback<Response>() {
@Override
public void completed(final Response resp) {
// Do something when invocation is complete
...
}
@Override
public void failed(final Throwable throwable) {
throwable.printStackTrace();
}
});
...
You can use a similar approach for typed responses:
// Java
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import java.util.concurrent.Future;
import javax.ws.rs.core.Response;
import javax.ws.rs.client.InvocationCallback;
...
Client client = ClientBuilder.newClient();
Future<BookInfo> futureResp = client.target("http://example.org/bookstore/books/123")
.request("application/xml")
.async()
.get(
new InvocationCallback<BookInfo>() {
@Override
public void completed(final BookInfo resp) {
// Do something when invocation is complete
...
}
@Override
public void failed(final Throwable throwable) {
throwable.printStackTrace();
}
});
...
Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.