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

最佳答案

使用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

09-10 02:00
查看更多