Issue
When calling the controller, I need to start two providers. One of them (personProvider) has to do its job in the background and write data to the Redis cache (I do not need the result of his work here). I need to map and send the result of the second (accountsProvider) to the calling service. Please, tell me how I can run them in parallel. My solution doesn't work, because they execute consistently.
@GetMapping(value = "/accounts", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<myDTO> accountsController(@RequestHeader("Channel") String channel,
@RequestHeader("Session") String sessionId) {
return clientSessionProvider.getClientSession(sessionId, channel) // return Mono<String>
.flatMap(clientData-> {
personProvider.getPersonCard(clientData) // My background task return Mono<PersonCard>
.subscribeOn(Schedulers.boundedElastic());
return accountsProvider.getAccounts(clientData) // return Mono<Accounts>
.subscribeOn(Schedulers.boundedElastic());
})
.map(myDTOMapper::map);
}
Solution
I create static Scheduler as field of my Controller class:
private static final Scheduler backgroundTaskScheduler = Schedulers.newParallel("backgroundTaskScheduler", 2);
public Mono<myDTO> accountsController(@RequestHeader("Channel") String channel,
@RequestHeader("Session") String sessionId) {
return clientSessionProvider.getClientSession(sessionId, channel)
.flatMap(clientData-> {
backgroundTaskScheduler.schedule(() -> personProvider.getPersonCard(clientData));
return accountsProvider.getAccounts(clientData);
})
.map(myDTOMapper::map);
In this case, my personProvider start in other thread and does't block the response from the controller.
Answered By - Stariy Voyaka