Making an asynchronous call from the client

We have as yet discussed only the asynchronous support in the Server API. The asynchronous request processing has also been made available in the client API. A client request can be sent asynchronously using the async() method.

To invoke a resource asynchronously implies that the call returns immediately. Optionally, a callback can be registered using the InvocationCallback interface. Implement the completed(RESPONSE response) and failed(Throwable throwable) methods. The completed(RESPONSE response) method is called when the invocation completes successfully and the failed(Throwable throwable) method is called when the invocation fails. A client request is made asynchronously in the AsyncClient.java client as follows:

WebTarget target = client.target("http://localhost:8080/jboss-resteasy/rest/helloworld/timeout/60");
AsyncInvoker asyncInvoker = target.request("text/plain").async();
asyncInvoker.get(new InvocationCallback<String>() {
  @Override
  public void completed(String response) {
    System.out.println("Invocation completed and response available");
  }
  @Override
  public void failed(Throwable arg0) {}
});
System.out.println("Call to get returns immediately");

Set a suspended timeout and timeout handler in the resource method and resume the request in the timeout handler as follows:

Response hello = Response.ok("Hello after a timeout").type(MediaType.TEXT_PLAIN).build();
ar.resume(hello);

When the application is run, the call returns immediately with the message Call to get returns immediately (the message can get output and the processing continue without the message being noticed).

Making an asynchronous call from the client

And the suspend timeout starts with the message. This is shown in the following screenshot:

Making an asynchronous call from the client

After the suspended timeout has run, the response is returned as shown in the following screenshot. The message output when the call returns immediately is also shown here:

Making an asynchronous call from the client
..................Content has been hidden....................

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