How to group events by periods of inactivity using reactive extensions

I have a problem for which Reactive Extensions seems particularly well suited. I have a source of events that creates events in short bursts with relatively long intervals between outages between them. I would like to group these events in batches where (ideally) each burst of events would end in one batch. Using RxJava, is there a good way to do this? Observable.buffer (Observable) or Observable.buffer (Func0) seems promising, but one could use Observable.window () or Observable.groupByUntil ().

+4
source share
2 answers

Here is the code that seems to work for the debut buffer:

import java.util.List;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class DebounceBuffer {

    public static void main(String args[]) {
        // see all bursts in a single sequence
        //        intermittentBursts().toBlocking().forEach(System.out::println);

        // debounce to the last value in each burst
        //        intermittentBursts().debounce(10, TimeUnit.MILLISECONDS).toBlocking().forEach(System.out::println);

        /* The following will emit a buffered list as it is debounced */
        // first we multicast the stream ... using refCount so it handles the subscribe/unsubscribe
        Observable<Integer> burstStream = intermittentBursts().publish().refCount();
        // then we get the debounced version
        Observable<Integer> debounced = burstStream.debounce(10, TimeUnit.MILLISECONDS);
        // then the buffered one that uses the debounced stream to demark window start/stop
        Observable<List<Integer>> buffered = burstStream.buffer(debounced);
        // then we subscribe to the buffered stream so it does what we want
        buffered.take(20).toBlocking().forEach(System.out::println);
    }

    public static Observable<Integer> intermittentBursts() {
        return Observable.create((Subscriber<? super Integer> s) -> {
            while (!s.isUnsubscribed()) {
                // burst some number of items
                for (int i = 0; i < Math.random() * 20; i++) {
                    s.onNext(i);
                }
                try {
                    // sleep for a random amount of time
                    Thread.sleep((long) (Math.random() * 1000));
                } catch (Exception e) {
                    // do nothing
                }
            }
        }).subscribeOn(Schedulers.newThread()); // use newThread since we are using sleep to block
    }

}

It emits the following:

[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1]
[0, 1, 2, 3, 4, 5]
[0, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3]
[0]
[0, 1, 2]
[0]
+15
source

Any of the operators you mentioned will work, it depends only on what semantics you want.

If you want each group to be a list, use a buffer: https://github.com/Netflix/RxJava/wiki/Transforming-Observables#buffer

If you want each group to be a sequence, use the window: https://github.com/Netflix/RxJava/wiki/Transforming-Observables#window

, . , , , , . , , . , .

groupByUntil, , , , - , , groupBy/groupByUntil - .

, /, , , . , -, , debounce, debounce , .

: , , debounce debounce / . , .

/, bufferedDebounce?

+4

All Articles