【问题标题】:How to capture the reactive stream cancel signal?如何捕获反应流取消信号?
【发布时间】:2019-12-19 14:37:21
【问题描述】:

我想在流因任何原因(包括取消)终止后最终做点什么,我 找到了doFinally方法,但是取消的时候没用,因为https://github.com/reactor/reactor-core/issues/1090#issuecomment-367633241显示:

取消仅在上游进行

那么,如何捕获取消信号呢?

这是我的代码:

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

【问题讨论】:

    标签: java spring-webflux project-reactor


    【解决方案1】:

    您不能采用这种特定的安排,因为 foo() 方法/库似乎自己管理订阅(取消),而不是将责任留给消费者。因此,这样管理订阅不一定是好事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多