1

I have a gui events producer. I want to take the latest emission from it and have it processed on a different thread. While processing the emission I need gui producer emissions to be dropped. After emission is processed I want to take latest emission from gui producer.

I was trying to use onBackpressureLatest() overflow strategy but queue size is my problem. With the default 256 queue size I get the expected behaviour but must process 255 emissions which are useless for me. Decreasing queue size to 16 leaves me with 15 useless emissions. I imagine I would achieve expected behaviour with a queue size=1 but 16 is the min value.

I attach code described in the previous paragraph.

import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import java.util.concurrent.TimeUnit;

public class App {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("reactor.bufferSize.small", "16");

        guiEventsProducer()
                .onBackpressureLatest()
                .log()
                .publishOn(Schedulers.single())
                .subscribe(next -> {
                    System.out.println("Processing " + next);
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("Processing " + next  + " done");
                });

        TimeUnit.SECONDS.sleep(60);
    }

    private static Flux<Integer> guiEventsProducer() {
        return Flux.range(0, 10000);
    }
}

1 Answers1

0
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

import java.util.concurrent.TimeUnit;

public class App {

    public static void main(String[] args) throws InterruptedException {

        Flux.range(1, 10000)
                .log()
                .onBackpressureLatest()
                .log()
                .flatMap(next -> Mono.just(next).subscribeOn(Schedulers.single()), 1, 1)
                .subscribe(integer -> {
                    System.out.println("[" + Thread.currentThread().getName() + "] result=" + integer);
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });

        TimeUnit.SECONDS.sleep(60);
    }
}