use of org.reactivestreams.Subscription in project camel by apache.
the class CamelSubscriber method refill.
protected void refill() {
Long toBeRequested = null;
Subscription subs = null;
synchronized (this) {
if (consumer != null && this.subscription != null) {
Integer consMax = consumer.getEndpoint().getMaxInflightExchanges();
long max = (consMax != null && consMax > 0) ? consMax.longValue() : MAX_INFLIGHT_UNBOUNDED;
long newRequest = max - requested - inflightCount;
if (newRequest > 0) {
toBeRequested = newRequest;
requested += toBeRequested;
subs = this.subscription;
}
}
}
if (toBeRequested != null) {
subs.request(toBeRequested);
}
}
use of org.reactivestreams.Subscription in project camel by apache.
the class ConvertingPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super R> subscriber) {
delegate.subscribe(new Subscriber<Exchange>() {
private AtomicBoolean active = new AtomicBoolean(true);
private Subscription subscription;
@Override
public void onSubscribe(Subscription newSubscription) {
if (newSubscription == null) {
throw new NullPointerException("subscription is null");
} else if (newSubscription == this.subscription) {
throw new IllegalArgumentException("already subscribed to the subscription: " + newSubscription);
}
if (this.subscription != null) {
newSubscription.cancel();
} else {
this.subscription = newSubscription;
subscriber.onSubscribe(newSubscription);
}
}
@Override
public void onNext(Exchange ex) {
if (!active.get()) {
return;
}
R r;
try {
if (ex.hasOut()) {
r = ex.getOut().getBody(type);
} else {
r = ex.getIn().getBody(type);
}
} catch (TypeConversionException e) {
LOG.warn("Unable to convert body to the specified type: " + type.getName(), e);
r = null;
}
if (r == null && ex.getIn().getBody() != null) {
this.onError(new ClassCastException("Unable to convert body to the specified type: " + type.getName()));
active.set(false);
subscription.cancel();
} else {
subscriber.onNext(r);
}
}
@Override
public void onError(Throwable throwable) {
if (!active.get()) {
return;
}
subscriber.onError(throwable);
}
@Override
public void onComplete() {
if (!active.get()) {
return;
}
subscriber.onComplete();
}
});
}
use of org.reactivestreams.Subscription in project camel by apache.
the class TestPublisher method subscribe.
@Override
public void subscribe(Subscriber<? super T> subscriber) {
subscriber.onSubscribe(new Subscription() {
private Iterator<T> it = data.iterator();
private AtomicLong requested = new AtomicLong(0);
private Object monitor = new Object();
@Override
public void request(long l) {
this.requested.addAndGet(l);
new Thread() {
@Override
public void run() {
synchronized (monitor) {
boolean wasNonEmpty = it.hasNext();
while (requested.longValue() > 0 && it.hasNext()) {
T d = it.next();
requested.decrementAndGet();
if (delay > 0) {
try {
Thread.sleep(delay);
} catch (InterruptedException ex) {
}
}
subscriber.onNext(d);
}
if (wasNonEmpty && !it.hasNext()) {
// data cannot be added to this test publisher
subscriber.onComplete();
}
}
}
}.start();
}
@Override
public void cancel() {
synchronized (monitor) {
this.requested.set(0);
this.it = new Iterator<T>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
throw new NoSuchElementException();
}
};
new Thread() {
public void run() {
subscriber.onComplete();
}
}.start();
}
}
});
}
use of org.reactivestreams.Subscription in project RxDownload by ssseasonnn.
the class DownloadType method startDownload.
public Observable<DownloadStatus> startDownload() {
return Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription subscription) throws Exception {
log(startLog());
record.start();
}
}).flatMap(new Function<Integer, Publisher<DownloadStatus>>() {
@Override
public Publisher<DownloadStatus> apply(Integer integer) throws Exception {
return download();
}
}).doOnNext(new Consumer<DownloadStatus>() {
@Override
public void accept(DownloadStatus status) throws Exception {
record.update(status);
}
}).doOnError(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
log(errorLog());
record.error();
}
}).doOnComplete(new Action() {
@Override
public void run() throws Exception {
log(completeLog());
record.complete();
}
}).doOnCancel(new Action() {
@Override
public void run() throws Exception {
log(cancelLog());
record.cancel();
}
}).doFinally(new Action() {
@Override
public void run() throws Exception {
log(finishLog());
record.finish();
}
}).toObservable();
}
use of org.reactivestreams.Subscription in project redisson by redisson.
the class RedissonMapReactiveIterator method stream.
public Publisher<M> stream() {
return new Stream<M>() {
@Override
public void subscribe(final Subscriber<? super M> t) {
t.onSubscribe(new ReactiveSubscription<M>(this, t) {
private Map<ByteBuf, ByteBuf> firstValues;
private long iterPos = 0;
private InetSocketAddress client;
private long currentIndex;
@Override
protected void onRequest(final long n) {
currentIndex = n;
nextValues();
}
private Map<ByteBuf, ByteBuf> convert(Map<ScanObjectEntry, ScanObjectEntry> map) {
Map<ByteBuf, ByteBuf> result = new HashMap<ByteBuf, ByteBuf>(map.size());
for (Entry<ScanObjectEntry, ScanObjectEntry> entry : map.entrySet()) {
result.put(entry.getKey().getBuf(), entry.getValue().getBuf());
}
return result;
}
protected void nextValues() {
final ReactiveSubscription<M> m = this;
map.scanIteratorReactive(client, iterPos).subscribe(new Subscriber<MapScanResult<ScanObjectEntry, ScanObjectEntry>>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(MapScanResult<ScanObjectEntry, ScanObjectEntry> res) {
client = res.getRedisClient();
if (iterPos == 0 && firstValues == null) {
firstValues = convert(res.getMap());
} else if (convert(res.getMap()).equals(firstValues)) {
m.onComplete();
currentIndex = 0;
return;
}
iterPos = res.getPos();
for (Entry<ScanObjectEntry, ScanObjectEntry> entry : res.getMap().entrySet()) {
M val = getValue(entry);
m.onNext(val);
currentIndex--;
if (currentIndex == 0) {
m.onComplete();
return;
}
}
}
@Override
public void onError(Throwable error) {
m.onError(error);
}
@Override
public void onComplete() {
if (currentIndex == 0) {
return;
}
nextValues();
}
});
}
});
}
};
}
Aggregations