简体   繁体   English

Mono.flatMap 没有被调用

[英]Mono.flatMap is not getting invoked

I am still new to Spring Webflux and flatMap on Mono doesn't seem to work.我还是 Spring Webflux 的新手,Mono 上的 flatMap 似乎不起作用。 I have the following function and call to kafkaPublisher.publishToTopic is not working.我有以下功能,对kafkaPublisher.publishToTopic 的调用不起作用。 I inserted the print statement to test if it prints anything and it doesn't even execute the print statement.我插入了打印语句来测试它是否打印了任何东西,它甚至不执行打印语句。 publishToTopic returns Mono<Void> . publishToTopic返回Mono<Void>

private Mono<Void> test(Long gId, UUID pId) {
    Mono<UUID> nId = pDao.findNId(pId);
    Mono<List<String>> channels = nId.flatMapMany(pDao::findChannels).collectList();
    return Mono.zip(nId, channels)
           .flatMap(t -> {
               System.out.println(t.getT1());
               return kafkaPublisher.publishToTopic(gId, t.getT1().toString(), t.getT2());
           });

} }

It gets invoked if .block is called on flatMap as shown below.如果.blockflatMap上被调用,它就会被调用,如下所示。

private Mono<Void> test(Long gId, UUID pId) {
    Mono<UUID> nId = pDao.findNId(pId);
    Mono<List<String>> channels = nId.flatMapMany(pDao::findChannels).collectList();
    Mono.zip(nId, channels)
           .flatMap(t -> {
               System.out.println(t.getT1());
               return kafkaPublisher.publishToTopic(gId, t.getT1().toString(), t.getT2());
           }).block();
    return Mono.empty();

} }

I found my mistake.我发现了我的错误。 I wasn't not using the result of test anywhere in the function where I was calling this test method.我没有在调用此test方法的函数中的任何地方使用test结果。 Here is the code I was using to call test这是我用来调用test的代码

public Mono<Void> saveNew(NewPre pre) {
    preDao.insert(pre)
                        .flatMap(p -> test(p.pId(), p.nId()));
    return Mono.empty();
}

I changed it to following and it works.我将其更改为跟随并且有效。

public Mono<Void> saveNew(NewPre pre) {
    return preDao.insert(preference)
                        .flatMap(p -> test(p.p(), p.n())
                            .then(Mono.empty()));
}

flatMap hangs indefinitely, Best way to close the Asynchronous operation by converting it into future object . flatMap 无限期挂起,通过将异步操作转换为未来对象来关闭异步操作的最佳方法。

public Mono<Void> saveNew(NewPre pre) {
return preDao.insert(preference)
                    .flatMap({
                       p -> test(p.p(), p.n())
                      
                    }).toFuture();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM