use of org.reactivestreams.Subscription in project redisson by redisson.
the class SetReactiveIterator method subscribe.
@Override
public void subscribe(final Subscriber<? super V> t) {
t.onSubscribe(new ReactiveSubscription<V>(this, t) {
private List<ByteBuf> firstValues;
private List<ByteBuf> lastValues;
private long nextIterPos;
private InetSocketAddress client;
private boolean finished;
@Override
protected void onRequest(long n) {
nextValues();
}
private void handle(List<ScanObjectEntry> vals) {
for (ScanObjectEntry val : vals) {
onNext((V) val.getObj());
}
}
protected void nextValues() {
final ReactiveSubscription<V> m = this;
scanIteratorReactive(client, nextIterPos).subscribe(new Subscriber<ListScanResult<ScanObjectEntry>>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ListScanResult<ScanObjectEntry> res) {
if (finished) {
free(firstValues);
free(lastValues);
client = null;
firstValues = null;
lastValues = null;
nextIterPos = 0;
return;
}
long prevIterPos = nextIterPos;
if (lastValues != null) {
free(lastValues);
}
lastValues = convert(res.getValues());
client = res.getRedisClient();
if (nextIterPos == 0 && firstValues == null) {
firstValues = lastValues;
lastValues = null;
if (firstValues.isEmpty()) {
client = null;
firstValues = null;
nextIterPos = 0;
prevIterPos = -1;
}
} else {
if (firstValues.isEmpty()) {
firstValues = lastValues;
lastValues = null;
if (firstValues.isEmpty()) {
if (res.getPos() == 0) {
finished = true;
m.onComplete();
return;
}
}
} else if (lastValues.removeAll(firstValues)) {
free(firstValues);
free(lastValues);
client = null;
firstValues = null;
lastValues = null;
nextIterPos = 0;
prevIterPos = -1;
finished = true;
m.onComplete();
return;
}
}
handle(res.getValues());
nextIterPos = res.getPos();
if (prevIterPos == nextIterPos) {
finished = true;
m.onComplete();
}
}
@Override
public void onError(Throwable error) {
m.onError(error);
}
@Override
public void onComplete() {
if (finished) {
return;
}
nextValues();
}
});
}
});
}
use of org.reactivestreams.Subscription in project redisson by redisson.
the class RedissonKeysReactive method createKeysIterator.
private Publisher<String> createKeysIterator(final MasterSlaveEntry entry, final String pattern) {
return new Stream<String>() {
@Override
public void subscribe(final Subscriber<? super String> t) {
t.onSubscribe(new ReactiveSubscription<String>(this, t) {
private List<String> firstValues;
private long nextIterPos;
private InetSocketAddress client;
private long currentIndex;
@Override
protected void onRequest(final long n) {
currentIndex = n;
nextValues();
}
protected void nextValues() {
final ReactiveSubscription<String> m = this;
scanIterator(entry, nextIterPos, pattern).subscribe(new Subscriber<ListScanResult<String>>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(ListScanResult<String> res) {
client = res.getRedisClient();
long prevIterPos = nextIterPos;
if (nextIterPos == 0 && firstValues == null) {
firstValues = res.getValues();
} else if (res.getValues().equals(firstValues)) {
m.onComplete();
currentIndex = 0;
return;
}
nextIterPos = res.getPos();
if (prevIterPos == nextIterPos) {
nextIterPos = -1;
}
for (String val : res.getValues()) {
m.onNext(val);
currentIndex--;
if (currentIndex == 0) {
m.onComplete();
return;
}
}
if (nextIterPos == -1) {
m.onComplete();
currentIndex = 0;
}
}
@Override
public void onError(Throwable error) {
m.onError(error);
}
@Override
public void onComplete() {
if (currentIndex == 0) {
return;
}
nextValues();
}
});
}
});
}
};
}
use of org.reactivestreams.Subscription in project ratpack by ratpack.
the class MapPublisher method subscribe.
@Override
public void subscribe(final Subscriber<? super O> outSubscriber) {
input.subscribe(new Subscriber<I>() {
private Subscription subscription;
private final AtomicBoolean done = new AtomicBoolean();
@Override
public void onSubscribe(Subscription subscription) {
this.subscription = subscription;
outSubscriber.onSubscribe(this.subscription);
}
@Override
public void onNext(I in) {
O out;
try {
out = function.apply(in);
} catch (Throwable throwable) {
subscription.cancel();
onError(throwable);
return;
}
if (!done.get()) {
outSubscriber.onNext(out);
}
}
@Override
public void onError(Throwable t) {
if (done.compareAndSet(false, true)) {
outSubscriber.onError(t);
}
}
@Override
public void onComplete() {
if (done.compareAndSet(false, true)) {
outSubscriber.onComplete();
}
}
});
}
use of org.reactivestreams.Subscription in project cyclops by aol.
the class BaseSequentialTest method subscribe3ErrorOnComplete.
@Test
public void subscribe3ErrorOnComplete() throws InterruptedException {
List<Integer> result = new ArrayList<>();
AtomicBoolean onComplete = new AtomicBoolean(false);
Subscription s = of(1, 2, 3).forEachSubscribe(i -> result.add(i), e -> e.printStackTrace(), () -> onComplete.set(true));
assertThat(onComplete.get(), Matchers.equalTo(false));
s.request(4l);
assertThat(onComplete.get(), Matchers.equalTo(true));
assertThat(result.size(), Matchers.equalTo(3));
assertThat(result, hasItems(1, 2, 3));
assertThat(onComplete.get(), Matchers.equalTo(true));
}
use of org.reactivestreams.Subscription in project cyclops by aol.
the class BaseSequentialTest method subscribeError.
@Test
public void subscribeError() throws InterruptedException {
List<Integer> result = new ArrayList<>();
Subscription s = of(1, 2, 3).forEachSubscribe(i -> result.add(i), e -> e.printStackTrace());
s.request(1l);
assertThat(result.size(), Matchers.equalTo(1));
s.request(1l);
assertThat(result.size(), Matchers.equalTo(2));
s.request(1l);
assertThat(result.size(), Matchers.equalTo(3));
assertThat(result, hasItems(1, 2, 3));
}
Aggregations