Asynchronous callbacks in Springboot (Part 2)

In the previous Post of asynchronous callbacks we discussed how to do asynchronous callbacks using CompletableFuture class in java.

In this post I will share the method which I used to call different REST APIs asynchronously.For this we can use ListenableFuture.

Both ListenableFuture and CompletableFuture has an edge over Future class of Java and Java 8 ships both of them together.
More info in these links:

Steps are:

  • Create AsyncRestTemplate Object.
  • Methods which is used to call the function is
  • This is the most common way to call the rest api of other microservices.
//Example of exchange used 
HttpHeaders headers = new HttpHeaders();
Map<String, Object> testMap = new HashMap<>();
HttpEntity<Map<String,Object>> entity = new HttpEntity<>(testMap, headers);, HttpMethod.PUT, entity, Object.class);

If someone wants to use delete , there’s one more method asyncRestTemplate.delete() which does the work.It is described below:

    String someurl = "http://localhost:8082/ratingsdata";
    String some2url = "http://localhost:8081/movies";
    private static AsyncRestTemplate asyncRestTemplate = null;  

    public void getCatalog(@PathVariable("userId") String userId) throws Exception {
        asyncRestTemplate = new AsyncRestTemplate();


    public void invokeAsyncDelete(String fullUri) throws Exception {

        //This will call the DELETE API of the respective URL if present 
        ListenableFuture<?> deletedFuture = asyncRestTemplate.delete(new URI(fullUri));
        deletedFuture.addCallback(new ListenableFutureCallback<Object>() {
            public void onSuccess(Object result) {
                System.out.println("Success for : " + fullUri);

            public void onFailure(Throwable genEx) {
                System.out.println("Failure for:" + fullUri);

/*Output:Success for : http://localhost:8081/movies
         Success for : http://localhost:8082/ratingsdata */

Use cases:

  • If Someone wants to delete the records by calling the DELETE API of other services in database with no dependecies with each other , then we can use above method.