/** * Child Subscribers will observe the events of the ConnectableObservable on the * specified scheduler. * @param <T> the value type * @param cf the ConnectableFlowable to wrap * @param scheduler the target scheduler * @return the new ConnectableObservable instance */ public static <T> ConnectableFlowable<T> observeOn(final ConnectableFlowable<T> cf, final Scheduler scheduler) { final Flowable<T> flowable = cf.observeOn(scheduler); return RxJavaPlugins.onAssembly(new ConnectableFlowableReplay<T>(cf, flowable)); }
/** * Child Subscribers will observe the events of the ConnectableObservable on the * specified scheduler. * @param <T> the value type * @param cf the ConnectableFlowable to wrap * @param scheduler the target scheduler * @return the new ConnectableObservable instance */ public static <T> ConnectableFlowable<T> observeOn(final ConnectableFlowable<T> cf, final Scheduler scheduler) { final Flowable<T> observable = cf.observeOn(scheduler); return RxJavaPlugins.onAssembly(new ConnectableFlowableReplay<T>(cf, observable)); }
@Test public void syncFusedObserveOn2() { ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).publish(); Flowable<Integer> obs = cf.observeOn(ImmediateThinScheduler.INSTANCE); for (int i = 0; i < 1000; i++) { for (int j = 1; j < 6; j++) { List<TestSubscriber<Integer>> tss = new ArrayList<TestSubscriber<Integer>>(); for (int k = 1; k < j; k++) { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); tss.add(ts); obs.subscribe(ts); } Disposable connection = cf.connect(); for (TestSubscriber<Integer> ts : tss) { ts.awaitDone(5, TimeUnit.SECONDS) .assertSubscribed() .assertValueCount(1000) .assertNoErrors() .assertComplete(); } connection.dispose(); } } }
@Test public void syncFusedObserveOn() { ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).publish(); Flowable<Integer> obs = cf.observeOn(Schedulers.computation()); for (int i = 0; i < 1000; i++) { for (int j = 1; j < 6; j++) { List<TestSubscriber<Integer>> tss = new ArrayList<TestSubscriber<Integer>>(); for (int k = 1; k < j; k++) { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); tss.add(ts); obs.subscribe(ts); } Disposable connection = cf.connect(); for (TestSubscriber<Integer> ts : tss) { ts.awaitDone(5, TimeUnit.SECONDS) .assertSubscribed() .assertValueCount(1000) .assertNoErrors() .assertComplete(); } connection.dispose(); } } }
@Test public void testObserveOn() { ConnectableFlowable<Integer> cf = Flowable.range(0, 1000).hide().publish(); Flowable<Integer> obs = cf.observeOn(Schedulers.computation()); for (int i = 0; i < 1000; i++) { for (int j = 1; j < 6; j++) { List<TestSubscriber<Integer>> tss = new ArrayList<TestSubscriber<Integer>>(); for (int k = 1; k < j; k++) { TestSubscriber<Integer> ts = new TestSubscriber<Integer>(); tss.add(ts); obs.subscribe(ts); } Disposable connection = cf.connect(); for (TestSubscriber<Integer> ts : tss) { ts.awaitDone(5, TimeUnit.SECONDS) .assertSubscribed() .assertValueCount(1000) .assertNoErrors() .assertComplete(); } connection.dispose(); } } }
@Test public void testBackpressureFastSlow() { ConnectableFlowable<Integer> is = Flowable.range(1, Flowable.bufferSize() * 2).publish(); Flowable<Integer> fast = is.observeOn(Schedulers.computation()) .doOnComplete(new Action() { @Override Flowable<Integer> slow = is.observeOn(Schedulers.computation()).map(new Function<Integer, Integer>() { int c;
public static void main(String[] args) throws IOException { ReactiveJournal reactiveJournal = new ReactiveJournal("src/main/java/org/reactivejournal/examples/fastproducerslowconsumer/resources/"); //Get the input from the recorder note that we have to set the replayRate to ACTUAL_TIME //to replicate the conditions in the 'real world'. PlayOptions options = new PlayOptions().filter("input").replayRate(ReplayRate.ACTUAL_TIME); ConnectableFlowable journalInput = new RxJavaPlayer(reactiveJournal).play(options).publish(); //Reduce the latency of the consumer to 5ms - try reducing or increasing to study the effects. Consumer onNextSlowConsumer = FastProducerSlowConsumer.createOnNextSlowConsumer(3); long startTime = System.currentTimeMillis(); journalInput.observeOn(Schedulers.io()).subscribe(onNextSlowConsumer::accept, e -> System.out.println("ReactiveRecorder " + " " + e), () -> System.out.println("ReactiveRecorder complete [" + (System.currentTimeMillis()-startTime) + "ms]") ); journalInput.connect(); DSUtil.sleep(1000); }
public static void main(String[] args) throws IOException { ReactiveJournal reactiveJournal = new ReactiveJournal("/tmp/fastproducer"); reactiveJournal.clearCache(); Flowable<Long> fastProducer = FastProducerSlowConsumer.createFastProducer(BackpressureStrategy.MISSING, 2500); ReactiveRecorder recorder = reactiveJournal.createReactiveRecorder(); recorder.recordAsync(fastProducer,"input"); //Set the replay strategy to ReplayRate.FAST as e want to process the event as soon as it is //received from the publisher. PlayOptions options = new PlayOptions().filter("input").replayRate(PlayOptions.ReplayRate.FAST); ConnectableFlowable journalInput = new RxJavaPlayer(reactiveJournal).play(options).publish(); Consumer onNextSlowConsumer = FastProducerSlowConsumer.createOnNextSlowConsumer(10); recorder.record(journalInput, "consumed"); long startTime = System.currentTimeMillis(); journalInput.observeOn(Schedulers.io()).subscribe(onNextSlowConsumer::accept, e -> System.out.println("ReactiveRecorder " + " " + e), () -> System.out.println("ReactiveRecorder complete [" + (System.currentTimeMillis()-startTime) + "]") ); journalInput.connect(); DSUtil.sleep(3000); } }
@Test(timeout = 5000) public void schedulerTestRx() throws InterruptedException { CountDownLatch latch = new CountDownLatch(2); ConnectableFlowable<Integer> connectableFlux = Flowable.just(1, 2, 3, 4, 5).publish(); connectableFlux .doOnEach(System.out::println) .subscribe(v -> {} , e -> {}, latch::countDown); connectableFlux .observeOn(Schedulers.computation()) .doOnEach(System.out::println) .map(v -> v * 10) .observeOn(Schedulers.single()) .doOnEach(System.out::println) .subscribeOn(Schedulers.io()) .subscribe(v -> {} , e -> {}, latch::countDown); Thread.sleep(100); connectableFlux.connect(); latch.await(); } }