jeudi 16 décembre 2021

How to execute two Mono parrallel tasks in controller. Spring WebFlux

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, becourse 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);
    }



Aucun commentaire:

Enregistrer un commentaire