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);
}
});
});
}
});
}
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());
}
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();
}
}
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();
}
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();
});
}
});
}
Aggregations