Search in sources :

Example 11 with Subscription

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);
    }
}
Also used : Subscription(org.reactivestreams.Subscription)

Example 12 with Subscription

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();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TypeConversionException(org.apache.camel.TypeConversionException) Subscription(org.reactivestreams.Subscription)

Example 13 with Subscription

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();
            }
        }
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Iterator(java.util.Iterator) Subscription(org.reactivestreams.Subscription) NoSuchElementException(java.util.NoSuchElementException)

Example 14 with Subscription

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();
}
Also used : Action(io.reactivex.functions.Action) Consumer(io.reactivex.functions.Consumer) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 15 with Subscription

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();
                        }
                    });
                }
            });
        }
    };
}
Also used : InetSocketAddress(java.net.InetSocketAddress) MapScanResult(org.redisson.client.protocol.decoder.MapScanResult) ByteBuf(io.netty.buffer.ByteBuf) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) ScanObjectEntry(org.redisson.client.protocol.decoder.ScanObjectEntry) Entry(java.util.Map.Entry) Subscriber(org.reactivestreams.Subscriber) Stream(reactor.rx.Stream) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) ReactiveSubscription(reactor.rx.subscription.ReactiveSubscription) Subscription(org.reactivestreams.Subscription) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Aggregations

Subscription (org.reactivestreams.Subscription)627 Test (org.junit.Test)506 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)158 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)131 ArrayList (java.util.ArrayList)117 AtomicReference (java.util.concurrent.atomic.AtomicReference)105 List (java.util.List)80 FluxOperatorTest (reactor.test.publisher.FluxOperatorTest)74 Subscriber (org.reactivestreams.Subscriber)68 AtomicLong (java.util.concurrent.atomic.AtomicLong)56 Assert (org.junit.Assert)56 Arrays (java.util.Arrays)43 BaseSequentialTest (com.oath.cyclops.streams.BaseSequentialTest)42 Vector (cyclops.data.Vector)39 ReactiveSeq (cyclops.reactive.ReactiveSeq)39 Executor (java.util.concurrent.Executor)38 Spouts (cyclops.reactive.Spouts)36 Arrays.asList (java.util.Arrays.asList)36 Executors (java.util.concurrent.Executors)35 StepVerifier (reactor.test.StepVerifier)34