0

How to print elements in buffer when using backpressure:

 Flowable.range(1, 1000)
                .onBackpressureBuffer(20, () ->{}, BackpressureOverflowStrategy.DROP_OLDEST)
                .onBackpressureDrop(v -> System.out.println("Dropped.. :" + v))
                .delay(0, TimeUnit.MILLISECONDS, Schedulers.io())
                .doOnNext(value -> System.out.println("got " + value))
                .map(value -> {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    return value;
                })
                .observeOn(Schedulers.newThread(),false, 20)
                .subscribe(
                        value -> System.out.println("handled:  " + value),
                        Throwable::printStackTrace,
                        () -> System.out.println("completed")
                );
        sleep(10000);

the output:
got 1
Dropped.. :21
Dropped.. :22
Dropped.. :23
Dropped.. :24
Dropped.. :25
.
.
.

I want
got 1
elements in buffer: 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
Dropped.. :21
.
.
.

I do not know if that is possible or not. Please help I'am new in reactive programming
Thanks

Brahian
  • 46
  • 1
  • 5
  • Why do you have that `onBackpressureDrop` in the sequence? – akarnokd Apr 19 '21 at 16:54
  • I understand that when I use **onBackpressureBuffe** , when the 20 spaces in the Buffer are full, then the **onBackpressureDrop** will discard the item is this case base on"DROP_OLDEST", is that right? – Brahian Apr 19 '21 at 17:28
  • `onBackpressureDrop` will practically negate `onBackpressureBuffer` because drop will remove any backpressure from buffer. Thus buffer will think it can emit immediately. Remove `onBackpressureDrop`. Also you can't get the dropped item with `onBackpressureBuffer` at the moment. – akarnokd Apr 20 '21 at 06:47

0 Answers0