简体   繁体   English

如何捕获反应流取消信号?

[英]How to capture the reactive stream cancel signal?

I want to do something finally after stream terminates for any reason including cancellation, and I found the doFinally method, but it dose not work when cancellation, because https://github.com/reactor/reactor-core/issues/1090#issuecomment-367633241 show :我想在流因任何原因(包括取消)终止后最终做一些事情,我找到了doFinally方法,但是取消时它不起作用,因为https://github.com/reactor/reactor-core/issues/1090#issuecomment -367633241显示:

Cancellation travels only upstream取消仅向上游传播

So, how to capture the cancel signal?那么,如何捕捉取消信号呢?

There is my code:有我的代码:

    public Mono<Void> myFunction() {
        return Mono.just("hello")
                .flatMap(s -> foo(s))
                .doFinally(signalType -> {
                    // do something finally, but the doFinally won't be called
                    System.out.println(signalType);
                });
    }

    // some other library's function that I cant not modify any way
    public Mono<Void> foo(String s) {
        // return a reactive stream, and will cancel it after it be subscribed, like:
        return Mono.just(s)
                .doOnSubscribe(subscription -> subscription.cancel())
                .then();
    }

You can't in that particular arrangement, because the foo() method/library seems to manage the subscription (the cancellation) itself, instead of leaving that responsibility to the consumer.你不能在那个特定的安排中,因为foo()方法/库似乎管理订阅(取消)本身,而不是把这个责任留给消费者。 Managing the subscription like that is thus not necessarily a good thing.因此,像这样管理订阅不一定是一件好事。

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

相关问题 如何向 ConnectableFlowable 发送取消信号? - How to send cancel signal to ConnectableFlowable? RxJava:如何将复杂的 if 语句转换为响应式流? - RxJava: how to turn a complicated if statement into a reactive stream? 如何重新分配反应通量 stream 中的变量? - How to reassign a variable in reactive Flux stream? 如何将轮询API转换为反应流 - How to turn a polling api into reactive stream 为什么在 Spring Cloud Stream 反应式消费者中遇到异常时会收到 onComplete 信号? - Why am I getting onComplete signal when an exception is encountered in Spring Cloud Stream reactive consumer? 作为if语句的结果,我如何在另一个反应流中使用一个反应流 - How can I use one reactive stream inside another reactive stream as result of if statement 在反应式 stream 中对项目进行分组 - Grouping items in a reactive stream 如何转换返回列表的方法<String>进入 Reactive Stream smallrye 兵变? - How to convert a method returning List<String> into Reactive Stream smallrye mutiny? 如何将反应流连接到 quarkus / smallrye 中的 AMQP 代理 - How to connect a reactive stream to an AMQP broker in quarkus / smallrye 如何使用Spring data-mongodb-reactive从受限制的集合中流式传输 - How to stream from a capped collection with Spring data-mongodb-reactive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM