Search in sources :

Example 1 with LongConsumer

use of io.reactivex.rxjava3.functions.LongConsumer in project redisson by redisson.

the class RedissonTopicRx method getMessages.

public <M> Flowable<M> getMessages(Class<M> type) {
    ReplayProcessor<M> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        @Override
        public void accept(long n) throws Exception {
            AtomicLong counter = new AtomicLong(n);
            RFuture<Integer> t = topic.addListenerAsync(type, new MessageListener<M>() {

                @Override
                public void onMessage(CharSequence channel, M msg) {
                    p.onNext(msg);
                    if (counter.decrementAndGet() == 0) {
                        topic.removeListenerAsync(this);
                        p.onComplete();
                    }
                }
            });
            t.whenComplete((id, e) -> {
                if (e != null) {
                    p.onError(e);
                    return;
                }
                p.doOnCancel(new Action() {

                    @Override
                    public void run() throws Exception {
                        topic.removeListenerAsync(id);
                    }
                });
            });
        }
    });
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Flowable(io.reactivex.rxjava3.core.Flowable) RTopic(org.redisson.api.RTopic) MessageListener(org.redisson.api.listener.MessageListener) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(io.reactivex.rxjava3.functions.Action) MessageListener(org.redisson.api.listener.MessageListener) RFuture(org.redisson.api.RFuture)

Example 2 with LongConsumer

use of io.reactivex.rxjava3.functions.LongConsumer in project RxJava by ReactiveX.

the class FlowableSkipTest method backpressureMultipleSmallAsyncRequests.

@Test
public void backpressureMultipleSmallAsyncRequests() throws InterruptedException {
    final AtomicLong requests = new AtomicLong(0);
    TestSubscriber<Long> ts = new TestSubscriber<>(0L);
    Flowable.interval(100, TimeUnit.MILLISECONDS).doOnRequest(new LongConsumer() {

        @Override
        public void accept(long n) {
            requests.addAndGet(n);
        }
    }).skip(4).subscribe(ts);
    Thread.sleep(100);
    ts.request(1);
    ts.request(1);
    Thread.sleep(100);
    ts.cancel();
    ts.assertNoErrors();
    assertEquals(6, requests.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) TestSubscriber(io.reactivex.rxjava3.subscribers.TestSubscriber) Test(org.junit.Test)

Example 3 with LongConsumer

use of io.reactivex.rxjava3.functions.LongConsumer in project RxJava by ReactiveX.

the class FlowableDoOnLifecycleTest method requestCrashed.

@Test
public void requestCrashed() {
    List<Throwable> errors = TestHelper.trackPluginErrors();
    try {
        Flowable.just(1).doOnLifecycle(Functions.emptyConsumer(), new LongConsumer() {

            @Override
            public void accept(long v) throws Exception {
                throw new TestException();
            }
        }, Functions.EMPTY_ACTION).test().assertResult(1);
        TestHelper.assertUndeliverable(errors, 0, TestException.class);
    } finally {
        RxJavaPlugins.reset();
    }
}
Also used : TestException(io.reactivex.rxjava3.exceptions.TestException) TestException(io.reactivex.rxjava3.exceptions.TestException) Test(org.junit.Test)

Example 4 with LongConsumer

use of io.reactivex.rxjava3.functions.LongConsumer in project redisson by redisson.

the class PublisherAdder method addAll.

public Single<Boolean> addAll(Publisher<? extends V> c) {
    final Flowable<? extends V> cc = Flowable.fromPublisher(c);
    final ReplayProcessor<Boolean> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        @Override
        public void accept(long t) throws Exception {
            final AtomicBoolean completed = new AtomicBoolean();
            final AtomicLong values = new AtomicLong();
            final AtomicBoolean lastSize = new AtomicBoolean();
            cc.subscribe(new Consumer<V>() {

                @Override
                public void accept(V t) throws Exception {
                    values.getAndIncrement();
                    add(t).whenComplete((res, e) -> {
                        if (e != null) {
                            p.onError(e);
                            return;
                        }
                        if (res) {
                            lastSize.set(true);
                        }
                        if (values.decrementAndGet() == 0 && completed.get()) {
                            p.onNext(lastSize.get());
                            p.onComplete();
                        }
                    });
                }
            }, new Consumer<Throwable>() {

                @Override
                public void accept(Throwable t) throws Exception {
                    p.onError(t);
                }
            }, new Action() {

                @Override
                public void run() throws Exception {
                    completed.set(true);
                    if (values.get() == 0) {
                        p.onNext(lastSize.get());
                        p.onComplete();
                    }
                }
            });
        }
    }).singleOrError();
}
Also used : RFuture(org.redisson.api.RFuture) AtomicLong(java.util.concurrent.atomic.AtomicLong) Single(io.reactivex.rxjava3.core.Single) Flowable(io.reactivex.rxjava3.core.Flowable) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Consumer(io.reactivex.rxjava3.functions.Consumer) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) Action(io.reactivex.rxjava3.functions.Action) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) Action(io.reactivex.rxjava3.functions.Action) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 5 with LongConsumer

use of io.reactivex.rxjava3.functions.LongConsumer in project redisson by redisson.

the class RedissonKeysRx method createKeysIterator.

private Publisher<String> createKeysIterator(MasterSlaveEntry entry, String pattern, int count) {
    ReplayProcessor<String> p = ReplayProcessor.create();
    return p.doOnRequest(new LongConsumer() {

        private RedisClient client;

        private List<String> firstValues;

        private long nextIterPos;

        private long currentIndex;

        @Override
        public void accept(long value) {
            currentIndex = value;
            nextValues();
        }

        protected void nextValues() {
            instance.scanIteratorAsync(client, entry, nextIterPos, pattern, count).whenComplete((res, e) -> {
                if (e != null) {
                    p.onError(e);
                    return;
                }
                client = res.getRedisClient();
                long prevIterPos = nextIterPos;
                if (nextIterPos == 0 && firstValues == null) {
                    firstValues = (List<String>) (Object) res.getValues();
                } else if (res.getValues().equals(firstValues)) {
                    p.onComplete();
                    currentIndex = 0;
                    return;
                }
                nextIterPos = res.getPos();
                if (prevIterPos == nextIterPos) {
                    nextIterPos = -1;
                }
                for (Object val : res.getValues()) {
                    p.onNext((String) val);
                    currentIndex--;
                    if (currentIndex == 0) {
                        p.onComplete();
                        return;
                    }
                }
                if (nextIterPos == -1) {
                    p.onComplete();
                    currentIndex = 0;
                }
                if (currentIndex == 0) {
                    return;
                }
                nextValues();
            });
        }
    });
}
Also used : RedisClient(org.redisson.client.RedisClient) List(java.util.List) Flowable(io.reactivex.rxjava3.core.Flowable) RedissonKeys(org.redisson.RedissonKeys) Publisher(org.reactivestreams.Publisher) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) RedisClient(org.redisson.client.RedisClient) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) ArrayList(java.util.ArrayList) ReplayProcessor(io.reactivex.rxjava3.processors.ReplayProcessor) LongConsumer(io.reactivex.rxjava3.functions.LongConsumer) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Flowable (io.reactivex.rxjava3.core.Flowable)3 LongConsumer (io.reactivex.rxjava3.functions.LongConsumer)3 ReplayProcessor (io.reactivex.rxjava3.processors.ReplayProcessor)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Action (io.reactivex.rxjava3.functions.Action)2 Test (org.junit.Test)2 Publisher (org.reactivestreams.Publisher)2 RFuture (org.redisson.api.RFuture)2 Single (io.reactivex.rxjava3.core.Single)1 TestException (io.reactivex.rxjava3.exceptions.TestException)1 Consumer (io.reactivex.rxjava3.functions.Consumer)1 TestSubscriber (io.reactivex.rxjava3.subscribers.TestSubscriber)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 RedissonKeys (org.redisson.RedissonKeys)1 RTopic (org.redisson.api.RTopic)1 MessageListener (org.redisson.api.listener.MessageListener)1 RedisClient (org.redisson.client.RedisClient)1 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)1