SignalType.CANCEL忽略publishOn运算符

时间:2019-01-28 13:42:33

标签: java project-reactor

我想知道为什么取消信号的处理方式不同于其他信号。 有人知道这是否是故意的吗?

Scheduler scheduler = Schedulers.elastic();

Disposable disposable =
   Flux.interval(Duration.ofSeconds(1))
       .publishOn(scheduler)
       .doFinally(signalType -> {
           if (signalType == SignalType.CANCEL) {
               // caller thread  - why ??
           } else {
              // elastic thread
           }
       }).log()
      .subscribe();

例如

Mono.just(1)
    .delayElement(Duration.ofSeconds(3))
    .doFinally(signalType -> {
        disposable.dispose();
     })
    .subscribe();

产生输出:

14:37:36.308 [main] INFO reactor.Flux.DoFinallyFuseable.1 - | onSubscribe([Fuseable] FluxDoFinally.DoFinallyFuseableSubscriber)
14:37:36.308 [main] INFO reactor.Flux.DoFinallyFuseable.1 - | request(unbounded)
14:37:37.324 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(0)
14:37:38.324 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(1)
14:37:39.325 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(2)
14:37:39.387 [parallel-2] INFO reactor.Flux.DoFinallyFuseable.1 - | cancel()

还有

Thread.sleep(4000);
disposable.dispose();

产生输出:

14:39:22.380 [main] INFO reactor.Flux.DoFinallyFuseable.1 - | onSubscribe([Fuseable] FluxDoFinally.DoFinallyFuseableSubscriber)
14:39:22.380 [main] INFO reactor.Flux.DoFinallyFuseable.1 - | request(unbounded)
14:39:23.412 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(0)
14:39:24.412 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(1)
14:39:25.414 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(2)
14:39:26.400 [elastic-2] INFO reactor.Flux.DoFinallyFuseable.1 - | onNext(3)
14:39:26.400 [main] INFO reactor.Flux.DoFinallyFuseable.1 - | cancel()

1 个答案:

答案 0 :(得分:1)

publishOn操作符仅在信号源发出并向下游传播时才被调用。

但是,取消请求被转发到上游,然后执行处理程序。信号源不发出取消信号。