Skip to content

Fetching data from 2 REST endpoints asynchronously and merging the responses

License

Notifications You must be signed in to change notification settings

prasadus92/spring-async-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-boot-async-example

Fetching data from 2 REST endpoints asynchronously and merging the responses.

Task Description

This Application gathers data from two REST endpoints asynchronously, merges the responses and displays them as a JSON. As an example, you could use these two endpoints:

Development

This project is built using the following tools:

  1. JDK 1.8
  2. Spring Boot 2
  3. Spring Async
  4. Maven
  5. Swagger (for API documentation; using SpringFox)

Solution

The logic of the solution is very straightforward, the Application uses Spring's @EnableAsync to configure a ThreadPoolTaskExecutor which will be used to run async methods. In this application, fetching user and his data configured to run asynchronously using this ThreadPoolTaskExecutor provided. Spring takes care of managing this executor.

Building and running the Application

As Maven is used as the build system, run:

./mvnw clean install

To run the project, just run one of the following commands:

java -jar target/data-1.0-SNAPSHOT.jar

./mvnw spring-boot:run

Navigate to http://localhost:8080/swagger-ui.html to see the available endpoints.

Testing

To run the tests, use:

./mvnw clean test

ToDo

  • Make the ThreadPoolTaskExecutor in AsyncConfig configurable from Spring Application properties or from environment variables.
  • Improve the test coverage.
  • Integration Tests using SpringBootTest are expensive as it results in loading the entire Application Context, add more tests using WebMvcTest.
  • Initially tried with Spring WebFlux (with Reactor Core), but it seemed like an overkill for this task. But if the requirements are changed to have User's posts as a stream, better to implement this using WebFlux.

About

Fetching data from 2 REST endpoints asynchronously and merging the responses

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages