【问题标题】:FluxProcessor: retrieve last emitted value on subscribe like rx's SubjectFluxProcessor:检索订阅时最后发出的值,如 rx 的主题
【发布时间】:2019-10-11 05:56:14
【问题描述】:

目前,FluxProcessor 订阅仅检索订阅后发出的那些值。但我想在订阅时检索 Flux 中的最后一个值,例如,像 RX 的 Subject 那样。

我有这个设置:

FluxProcessor<Integer, Integer> processor = DirectProcessor.<Integer>create().serialize();
FluxSink<Integer> sink = processor.sink();

sink.next(1);

stateProcessor.subscribe(System.out:println);

sink.next(2);

输出是:

1

期望的输出:

1
2

【问题讨论】:

    标签: java spring spring-webflux project-reactor reactive


    【解决方案1】:

    使用ReplayProcessor 修复它。它能够存储 N 个最后发出的值以供进一步订阅。同样的例子:

    FluxProcessor<Integer, Integer> processor = ReplayProcessor.<Integer>create(1).serialize(); //1 is the history size
    FluxSink<Integer> sink = processor.sink();
    
    sink.next(1);
    
    stateProcessor.subscribe(System.out:println);
    
    sink.next(2);
    

    打印:

    1
    2
    

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多