Home > database >  Need To Send A List Of Objects One At A Time In A Reactive Way
Need To Send A List Of Objects One At A Time In A 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;

Intead 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.

I'm still somewhat of a newbie with reactive and to be honest, it confuses me quite a bit. In addition this is the first time I've ever encountered this situation before, so I'm hoping someone out here in the community might be able to help.

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()));
}

Any and all help greatly appreciated!

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