项目作者: prasadus92

项目描述 :
Fetching data from 2 REST endpoints asynchronously and merging the responses
高级语言: Java
项目地址: git://github.com/prasadus92/spring-async-example.git
创建时间: 2018-07-21T19:25:44Z
项目社区:https://github.com/prasadus92/spring-async-example

开源协议:MIT License

下载


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:

  1. ./mvnw clean install

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

  1. java -jar target/data-1.0-SNAPSHOT.jar
  2. ./mvnw spring-boot:run

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

Testing

To run the tests, use:

  1. ./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.