[英]How to collect values over a period of time with max count from a signal?
Is there a way to collect values over a period of time with max count from a signal?有没有办法在一段时间内收集信号中的最大计数值? It seems like a combination of
collect(every:on:skipEmpty:discardWhenCompleted:)
and collect(count:)
.它似乎是
collect(every:on:skipEmpty:discardWhenCompleted:)
和collect(count:)
的组合。 The resulting signal would send an event at interval seconds apart if the count of accumulated values doesn't reach max count during time interval.如果累积值的计数在时间间隔内未达到最大计数,则生成的信号将每隔几秒发送一个事件。 Otherwise, it will send immediately.
否则,它将立即发送。
Question is hard to grasp, but I will try.问题很难掌握,但我会尝试。
If you want to batch emitted values by count and time, you can use bufferTimeout method.如果要按计数和时间批量发送值,可以使用 bufferTimeout 方法。 See documentation here https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#bufferTimeout-int-java.time.Duration-
请参阅此处的文档https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Flux.html#bufferTimeout-int-java.time.Duration-
Some example:一些例子:
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);
}
Output will be list of items.输出将是项目列表。
Flux.interval
is generating sequential long number (ie 1, 2, 3, 4, 5, ...), it is filtered on second line of method (to get some non interval behavior) and than buffer-ed. Flux.interval
正在生成连续的长数(即 1、2、3、4、5,...),它在方法的第二行进行过滤(以获得一些非间隔行为),然后进行缓冲。 After buffer, there is no long on stream, but it has changed to list of longs.缓冲区之后,没有 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]
Is this what you want?这是你想要的吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.