Enable Spring Boot for Asynchronous rest call in your application

Enable Spring Boot for Asynchronous rest call in your application

Enable Spring Boot for Asynchronous rest call in your application

In this article, we are trying to explore how can we enable Spring Boot for asynchronous rest services so that multiple calls to back-end /delayed tasks can be run in parallel and handled with the help of Spring Boot framework.

There could be may scenarios when you need to process multiple calls for different back-end services or complex calculations before aggregating this resulting data for further processing or presenting aggregate data as a view. This is a quick checklist on how would you enable Spring Boot for Asynchronous rest call to process the multiple requests in parallel and then wait for all of them to be finished.

Steps to Enable Spring Boot for Asynchronous rest call

  • Add @EnableAsync to your Application class (@SpringBootApplication) – SpringBootAsyncRestApplication.java in below example.
  • Let your application class extend AsyncConfigurerSupport and implement the getAsyncExecutor()
@SpringBootApplication
@EnableAsync
public class SpringBootAsyncRestApplication extends AsyncConfigurerSupport{
        @Override
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor myExecutor = new ThreadPoolTaskExecutor();
		myExecutor.setCorePoolSize(2);
		myExecutor.setMaxPoolSize(6);
		myExecutor.setQueueCapacity(500);
		myExecutor.initialize();
		myExecutor.setThreadNamePrefix("MyAsync: ");
		return myExecutor;
	}
  • Add @Async on each service call which performs rest backend calls and returns the response wrapped into a CompletableFuture as follows –
return CompletableFuture.completedFuture(response);
  • Invoke every service method which performs the rest call and put the responses into variables.
CompletableFuture<BackendResponse> response1 = myAsyncRestService1.call(sampleRequest1);
CompletableFuture<BackendResponse> response2 = myAsyncRestService2.call(sampleRequest2);
  • After this wait till all calls are processed.
CompletableFuture<Void> futureToJoin = CompletableFuture.allOf(responseFuture1, responseFuture2);
futureToJoin.join();
  • Finally you can get the results like –
Response response1 = responseFuture1.get();
Response response2 = responseFuture2.get();

Summary

These simple steps help you enable Spring Boot for Asynchronous rest call for your application. Also, it makes your program to perform much faster with the help of Async Rest calls in your Spring Boot application.

Please feel free to ask your questions in the comments section and let us know if you feel the article has helped you in understanding the use of java version in Spring Boot projects. 

Reference

http://javapapo.blogspot.com/2016/04/spring-async-and-javas-8.html

Quick Start Tutorial for Spring Boot – First Spring Boot Tutorial

Recommendation

Was this page helpful? Please use the commenting form below to ask any questions or to make a comment. Please do not put the same comment multiple times. Your comment will appear after some time. Also please note that we do not reply to emails or comments on social media. So if you have any question, please put it in the form below and we will try to reply to it as soon as possible. If you are facing any issue with the code provided in the blog, please be as specific with your requirements as possible and share the complete stack trace with us. We usually reply within a day. 

Leave a Reply

Your email address will not be published. Required fields are marked *