dea*_*mon 1 java reactive-programming project-reactor
我想通过重复发射随机数来模拟测量数据.我尝试使用Reactor,但它不会产生任何输出:
private static Random random = new Random();
public static void main(String[] args) throws InterruptedException {
Flux<Double> doubleGenerator = Flux.generate(
() -> random.nextDouble(),
(ignored, sink) -> {
sink.next(random.nextDouble());
return 0.0;
});
Flux<Long> timer = Flux.intervalMillis(1000);
Flux.combineLatest(doubleGenerator, timer, (value, ignored) -> value)
.subscribe(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
如何使用Reactor创建随机值流?
我使用Reactor 3.1解决了这个问题,因此在下面的解决方案中Flux.intervalMillis(long)已经改变了Flux.interval(Duration).基本上我创建一个Flux,以一定间隔发出一个数字(在我的情况下每秒一次),然后将其映射到一个随机数.
final Random random = new Random();
Flux.interval(Duration.ofSeconds(1))
.map(pulse -> random.nextDouble())
.subscribe(System.out::println);
Thread.sleep(5000);
Run Code Online (Sandbox Code Playgroud)
我只是用一个简单的main()方法做了我的例子,所以为了使Flux实际上足够长以发出数据,我加入了睡眠.