Reactor取消订阅的方式 [英] Reactor way to cancel Subscriptions
问题描述
我试图找出Reactor Project,我正在寻找一种取消订阅的方法。
我知道在进行例如Flux的订阅后我可以获得可用于发送onCancel信号的Cancellation对象的引用,但这只是在订阅之后我需要在某种Collection中保存该引用。
I try to figure out Reactor Project and I'm looking for a way to cancel Subscriptions. I know that after making Subscription of for example Flux I can get reference to Cancellation object which can be used to send onCancel Signal, but this is only after making subscription and I need to hold that reference in some kind of Collection.
是否有更好的方法来获取取消对象?或者只是取消订阅。也许某种地方包含对所有活跃订阅的引用 - 是的,这将是非常棒的...
Is there better way to get Cancellation object? Or just to cancel Subscriptions. Maybe some kind of place which contains reference to all active Subscriptions - yeah that will be awesome...
推荐答案
在Reactor,那里在您调用 subscribe()
之前想要取消订阅
是没有意义的(因为这是非常方法)创建订阅
并在链中传播该信号以开始发送数据。)
In Reactor, there is no sense in wanting to cancel a Subscription
before you've called subscribe()
(as it is that very method that creates the Subscription
and propagates that signal up the chain to start the emission of data).
没有具有所有订阅的集中位置,这没有多大意义,因为您需要一种方法来查找要取消的特定订阅(并且请记住,链中的每个运营商也可以使用中间订阅... )。
There is no centralized place with all subscriptions, that doesn't make much sense because you'd need a way of finding the specific subscriptions you want to cancel (and keep in mind that each operator in your chain can use an intermediate Subscription as well...).
注意一些运营商也会代表您取消订阅!例如, take(int)
就是这种情况,一旦发出足够的项目就会取消上游:
Note some operators will also cancel subscriptions on your behalf! That is the case for take(int)
for instance, which will cancel upstream once enough items have been emitted:
Flux.just(1, 2, 3, 4).log().take(2).subscribe(System.out::println);
将输出:
14:17:48.729 [main] INFO reactor.Flux.Array.1 - | onSubscribe([Synchronous Fuseable] FluxArray.ArraySubscription)
14:17:48.732 [main] INFO reactor.Flux.Array.1 - | request(unbounded)
14:17:48.732 [main] INFO reactor.Flux.Array.1 - | onNext(1)
1
14:17:48.732 [main] INFO reactor.Flux.Array.1 - | onNext(2)
2
14:17:48.732 [main] INFO reactor.Flux.Array.1 - | cancel()
这篇关于Reactor取消订阅的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!