Asynchronous clients

The asynchronous processing works on the client side too; JAX-RS APIs are available for it. The following is a simple example of a client using an asynchronous client API:

Client client = newClient();
WebTarget target = client.target(url);
final AsyncInvoker asyncInvoker = target.request().async();
Future<Response> future = asyncInvoker.get();
Response response = future.get();

Unlike synchronous invocation, the asynchronous client calls the HTTP get() method through the javax.ws.rs.client.AsyncInvoker instead of the javax.ws.rs.client.SyncInvoker. The AsyncInvoker returns calling the javax.ws.rs.client.Invocation.Builder.async() method as shown earlier.

The AsyncInvoker provides methods similar to the SyncInvoker, with the difference that these methods do not return a synchronous response. The synchronous responses are replaced by the Future class, shown in Chapter 5, Working with Distributed Transactions in the Enterprise Concurrent API section. The Future class represents the returned response data. You will note that these methods run in a very fast mode because they don't wait till the actual request completes. Unlike the AsyncInvoker, the future.get() method will wait until a response returns.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.35.77