FluxProcessor:检索rx的Subject上的最后发出的值

时间:2019-10-11 05:56:14

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

当前,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

1 个答案:

答案 0 :(得分: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