0

In the below code, the save to retrivedPrev and retrivedNext marked as //1 and //2 are not getting saved using Mongo reactive and control is only saving retrievedPortCall marked as //3. Pls suggest how can I achieve the same using Spring Webflux. If I use block(), I get Exception - block() is blocking which is not supported by thread reactor http-nio-2

public Mono<PortCall> updateByFindById(String gsisKey, PortCall portCall) {
    
    PortCall nextPort = portCallRepository.findById(portCall.getNextPortCall().getNextScheduleEntryKey()).toProcessor().block();

    PortCall prevPort = portCallRepository.findById(portCall.getPreviousPortCall().getPreviousScheduleEntryKey()).toProcessor().block();

    Mono<PortCall> prev = portCallRepository.findById(portCall.getPreviousPortCall()
            .getPreviousScheduleEntryKey()).flatMap(retrivedPrev -> {
                                    
                retrivedPrev.getNextPortCall().setNextScheduleEntryKey(nextPort.getGsisKey());
                retrivedPrev.getNextPortCall().setTerminalCode(nextPort.getSiteRkstCode());
                return portCallRepository.save(retrivedPrev);   //1
            });
    
     Mono<PortCall> next = portCallRepository.findById(portCall.getNextPortCall()
              .getNextScheduleEntryKey()).flatMap(retrivedNext->{
                
                 retrivedNext.getPreviousPortCall().setPreviousScheduleEntryKey(prevPort.getGsisKey());
                 retrivedNext.getPreviousPortCall().setTerminalCode(prevPort.getSiteRkstCode());
                return portCallRepository.save(retrivedNext);  //2
              });   
     
    return portCallRepository.findById(gsisKey)
            .flatMap(retrivedPortCall -> {
                    retrivedPortCall.setSiteCallStatus(SiteCallStatus.DELETED);
                    return portCallRepository.save(retrivedPortCall)  //3
                });
}
Puneet
  • 13
  • 1
  • 6
  • I can't see if you are using `@Transactional` annotation. – Rafael Guillen Feb 02 '21 at 18:00
  • This is mongo Db , in mongo we dont use @Transactional, also one object is getting saved only other 2 are not getting saved – Puneet Feb 02 '21 at 18:01
  • 1
    I think you are not subscribing to those reactive objects, son that's why they aren't doing anything. You could chain them all in the return statement. – Rafael Guillen Feb 02 '21 at 18:04
  • @RafaelGuillen : how can I do that, can you please help? – Puneet Feb 02 '21 at 18:11
  • This answer should help you...https://stackoverflow.com/questions/60836450/java-reactor-chain-monovoid-with-another-async-task-producing-monoobject – Rafael Guillen Feb 02 '21 at 18:36
  • You could also use `.zip` to join the 3 `Mono` objects, but I don't know if the order of execution matters to you. It looks like this: `return Mono.zip(prev, next, gsis).map(value -> tuple.getT3());`. Here it is returning the third value, but you could return any of them or even all. – Rafael Guillen Feb 02 '21 at 18:44

1 Answers1

0

Based on my last comment your code should look like this:

public Mono<PortCall> updateByFindById(String gsisKey, PortCall portCall) {
    
    PortCall nextPort = portCallRepository.findById(portCall.getNextPortCall().getNextScheduleEntryKey()).toProcessor().block();

    PortCall prevPort = portCallRepository.findById(portCall.getPreviousPortCall().getPreviousScheduleEntryKey()).toProcessor().block();

    Mono<PortCall> prev = portCallRepository.findById(portCall.getPreviousPortCall()
            .getPreviousScheduleEntryKey()).flatMap(retrivedPrev -> {
                                    
                retrivedPrev.getNextPortCall().setNextScheduleEntryKey(nextPort.getGsisKey());
                retrivedPrev.getNextPortCall().setTerminalCode(nextPort.getSiteRkstCode());
                return portCallRepository.save(retrivedPrev);   //1
            });
    
     Mono<PortCall> next = portCallRepository.findById(portCall.getNextPortCall()
              .getNextScheduleEntryKey()).flatMap(retrivedNext->{
                
                 retrivedNext.getPreviousPortCall().setPreviousScheduleEntryKey(prevPort.getGsisKey());
                 retrivedNext.getPreviousPortCall().setTerminalCode(prevPort.getSiteRkstCode());
                return portCallRepository.save(retrivedNext);  //2
              });   
     
    Mono<PortCall> gsis = portCallRepository.findById(gsisKey)
            .flatMap(retrivedPortCall -> {
                    retrivedPortCall.setSiteCallStatus(SiteCallStatus.DELETED);
                    return portCallRepository.save(retrivedPortCall)  //3
                });
    return Mono.zip(prev, next, gsis).map(value -> tuple.getT3());
}
Rafael Guillen
  • 1,343
  • 10
  • 25