[英]How to collect values over a period of time with max count from a signal?
有没有办法在一段时间内收集信号中的最大计数值? 它似乎是collect(every:on:skipEmpty:discardWhenCompleted:)
和collect(count:)
的组合。 如果累积值的计数在时间间隔内未达到最大计数,则生成的信号将每隔几秒发送一个事件。 否则,它将立即发送。
问题很难掌握,但我会尝试。
如果要按计数和时间批量发送值,可以使用 bufferTimeout 方法。 请参阅此处的文档https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#bufferTimeout-int-java.time.Duration-
一些例子:
void bufferTimeoutTry() throws InterruptedException {
Flux.interval(Duration.ofMillis(157))
.filter(time -> time > 20 && time < 38 || time % 5 == 0 || time % 17 == 0)
.bufferTimeout(5, Duration.ofSeconds(1))
.doOnNext(list -> {
// we will get list of items buffered in 1-second period of time, or at most 5 items.
})
.subscribe(System.out::println);
Thread.sleep(30000);
}
输出将是项目列表。 Flux.interval
正在生成连续的长数(即 1、2、3、4、5,...),它在方法的第二行进行过滤(以获得一些非间隔行为),然后进行缓冲。 缓冲区之后,没有 long 在流中,但它已更改为 long 列表。
[0, 5]
[10, 15]
[17, 20, 21, 22, 23]
[24, 25, 26, 27, 28]
[29, 30, 31, 32, 33]
[34, 35, 36, 37, 40]
[45, 50, 51]
[55, 60]
[65, 68, 70]
[75, 80]
[85, 90]
[95, 100]
[102, 105]
[110, 115]
[119, 120, 125]
[130, 135, 136]
[140, 145]
[150, 153, 155]
[160, 165]
[170, 175]
[180, 185]
这是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.