[英]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.