Home > Mobile >  Send a list of objects one at time in reactive way
Send a list of objects one at time in reactive way

Time:10-19

I'm receiving a request through a rest controller method with an object that I'm then passing to a method in the service layer.

The object in this request contains a list as follows:

public class BalanceAlertServiceRequestData extends AlertServiceRequestData {
    
   private List<BalanceAlertAttribute> balanceAlertList;

Instead of just sending the whole list over at once in a Mono, as I've done in other methods, in this case, there is a requirement to send one list item at a time.

Here's the method with questions in the code comment:

public Mono<AlertServiceResponse> sendBalanceAlertDataToPrepaid(BalanceAlertServiceRequest cartasRequest) {
    return Mono.just(cartasRequest.getBalanceAlertServiceRequestData().getBalanceAlertList())
        // what goes here to read off each element, transform it, and then send it of as a single element in a Mono?
        // Would it be something like "doOnEach(mapBalanceAlertServiceRequestToBalanceChangeAlertResponse)?"
        .flatMap(prepaidClient::sendBalanceAlertData)
        .thenReturn(cartaResponse(cartasRequest.getServiceRequestId()));
}

CodePudding user response:

The expected way to do that is to actually use the fromIterable method and provide your List:

 return Flux.fromIterable(cartasRequest.getBalanceAlertServiceRequestData().getBalanceAlertList())
                .flatMap(prepaidClient::sendBalanceAlertData)
// convert to Mono again
  • Related