In the previous post under JAX-RS Client, I showed with example, one way of making asynchronous request for a resource using JAX-RS api.
In this post, I will show another way in which we can achieve the same thing.
In the first approach, once we made the asynchronous call, we were continuously looping, checking the status whether it is done or not.
Below is the complete code from previous post for your reference.
1 package defaultPackage;
2
3 import java.util.concurrent.ExecutionException;
4 import java.util.concurrent.Future;
5
6 import jakarta.ws.rs.client.Client;
7 import jakarta.ws.rs.client.ClientBuilder;
8 import jakarta.ws.rs.client.Invocation;
9 import jakarta.ws.rs.client.WebTarget;
10 import jakarta.ws.rs.core.Response;
11
12 public class Example11 {
13 public static void main(String[] args) throws InterruptedException, ExecutionException {
14 Client client = ClientBuilder.newClient();
15 WebTarget webTarget = client.target("https://jsonplaceholder.typicode.com/users/2");
16 Invocation.Builder builder = webTarget.request();
17 Invocation invocation = builder.buildGet();
18 Future<Response> futureResponse = invocation.submit();
19 while(!futureResponse.isDone()) {
20 System.out.append('*');
21 Thread.sleep(100);
22 };
23 System.out.append("\r\n");
24 System.out.append("Successful");
25 String result = futureResponse.get().readEntity(String.class);
26 System.out.append(result);
27 client.close();
28 }
29 }
In the above code, at line 18, we are making an asynchronous request and in while loop (at line 19), we loop checking the status of asynchronous request.
In this post, I will show you the callback approach.
In the callback approach, we don’t loop continuously, checking the status of asynchronous request.
We send the logic that has to be executed on completion to JAX-RS framework and wait for it to be called by the framework.
The framework will call the logic once the asynchronous request is done successfully or failed.
Below is the code for your reference.
Main class
1 package defaultPackage;
2
3 import java.util.concurrent.ExecutionException;
4 import java.util.concurrent.Future;
5
6 import jakarta.ws.rs.client.Client;
7 import jakarta.ws.rs.client.ClientBuilder;
8 import jakarta.ws.rs.client.Invocation;
9 import jakarta.ws.rs.client.InvocationCallback;
10 import jakarta.ws.rs.client.WebTarget;
11 import jakarta.ws.rs.core.Response;
12
13 public class Example12 {
14 public static void main(String[] args) throws InterruptedException, ExecutionException {
15 Client client = ClientBuilder.newClient();
16 WebTarget webTarget = client.target("https://jsonplaceholder.typicode.com/users/2");
17 Invocation.Builder builder = webTarget.request();
18 Invocation invocation = builder.buildGet();
19 InvocationCallback<Response> invocationCallback = new InvocationCallbackImpl();
20 Future<Response> futureResponse = invocation.submit(invocationCallback);
21 client.close();
22 }
23
24 static class InvocationCallbackImpl implements InvocationCallback<Response> {
25 @Override
26 public void completed(Response response) {
27 System.out.println("Successful");
28 String result = response.readEntity(String.class);
29 System.out.println(result);
30 }
31
32 @Override
33 public void failed(Throwable exception) {
34 System.out.println("Failed");
35 exception.printStackTrace();
36 }
37 }
38 }
In the above code, we created a callback class “InvocationCallbackImpl” that implements the “InvocationCallback” interface.
The class provides logic for “completed” and “failed” status.
If the asynchronous task is completed successfully, the “completed” logic is executed. If the asynchronous task failed, the “failed” logic is executed.
In the above code, at line 20, we call “submit” method on “Invocation” instance passing the callback instance as an argument to the method.
In this way we can make asynchronous request using callback approach.