10

I am using Spring RestTemplate and want to make a call to another service that doesn't return any response body. So, I don't want to wait for the response. So, it's just fire and forget, and continue with the remaining code. I am thinking of creating a new Thread to do this but really not sure what's the correct approach.

user10937286
  • 321
  • 1
  • 3
  • 10
  • Why not to do request and very short timeout? Do you still get status code as a return and you need it for request verification? – Dmytro Chasovskyi May 22 '19 at 06:02
  • HTTP Response code would be there but don't need it as failed calls ( like 502 ) would not matter much. Basically, this end point is invoked just to say something like "hey, we have got this new order, keep the details ready for quick future lookups" – user10937286 May 22 '19 at 06:45

3 Answers3

14

If you use Java 11, java support asynchronous HTTP Client. Asynchronous client using CompletableFuture in the back. You can see javadoc.

HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://openjdk.java.net/"))
            .timeout(Duration.ofMinutes(1))
            .header("Content-Type", "application/json")
            .POST(BodyPublishers.ofFile(Paths.get("file.json")))
            .build();

    client.sendAsync(request, BodyHandlers.ofString())
            .thenApply(response -> { System.out.println(response.statusCode());
                return response; } )
            .thenApply(HttpResponse::body)
            .thenAccept(System.out::println);
Turac
  • 667
  • 1
  • 4
  • 19
7

The correct approach is to execute the async with a callback (using DeferredResult, like this (assuming we have a class someClass that we want to retrieve from the API:

@GetMapping(path = "/testingAsync")
public DeferredResult<String> value() throws ExecutionException, InterruptedException, TimeoutException {
   AsyncRestTemplate restTemplate = new AsyncRestTemplate();
   String baseUrl = "http://someUrl/blabla";
   HttpHeaders requestHeaders = new HttpHeaders();
   requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
   String value = "";

   HttpEntity entity = new HttpEntity("parameters", requestHeaders);
   final DeferredResult<String> result = new DeferredResult<>();
   ListenableFuture<ResponseEntity<someClass>> futureEntity = restTemplate.getForEntity(baseUrl, someClass.class);

   futureEntity.addCallback(new ListenableFutureCallback<ResponseEntity<someClass>>() {
      @Override
      public void onSuccess(ResponseEntity<someClass> result) {
         System.out.println(result.getBody().getName());
         result.setResult(result.getBody().getName());
      }

      @Override
      public void onFailure(Throwable ex) {
         result.setErrorResult(ex.getMessage());
      }
  });

  return result;
}
Gal Naor
  • 2,397
  • 14
  • 17
  • 1
    It's throwing error as 'org.springframework.http.ResponseEntity cannot be cast to org.springframework.util.concurrent.ListenableFuture' for this line "ListenableFuture> response = restTemplate.postForEntity(url, request, InitiateEventResponseVO.class); – viswa Nov 21 '19 at 11:19
  • `AsyncRestTemplate` is Deprecated – Pravin Aug 12 '20 at 20:09
0

There are many ways you can use to fire the request using the AsyncRestTemplate

The simplest way is just like restTemplate and call exchange method:

AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
JSONObject json = new JSONObject();
json.put("firstName","testUser");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>(json.toString(), headers);

Class<String> responseType = String.class;
ListenableFuture<ResponseEntity<String>> future = asyncRestTemplate.exchange("https://xxxxx.com/", HttpMethod.POST, requestEntity,responseType );

// If you want for the result then you can use 
 try {
        //waits for the result
        ResponseEntity<String> entity = future.get();
        //prints body source code for the given URL
        log.info(entity.getBody());
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
}

If we want to play with the failure (fallback scenario) or success in that case we can use the below code :

AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
JSONObject json = new JSONObject();
json.put("firstName","testUser");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>(json.toString(), headers);

 //final DeferredResult<String> result = new DeferredResult<>();
 ListenableFuture<ResponseEntity<String>> future = 
 asyncRestTemplate.postForEntity("https://xxxx.com", requestEntity, String.class);

  future.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {

 @Override
 public void onFailure(Throwable ex) {
// insert into the table or log or some other decision
 log.info(ex.getMessage());
 }

        @Override
        public void onSuccess(ResponseEntity<String> result) {
            log.info(result.getBody());
            log.info("Sucess");
        }
    });