Search in sources :

Example 11 with Subscriber

use of org.reactivestreams.Subscriber in project RxJava by ReactiveX.

the class ParallelJoinTest method overflowSlowpathDelayError.

@Test
public void overflowSlowpathDelayError() {
    @SuppressWarnings("unchecked") final Subscriber<? super Integer>[] subs = new Subscriber[1];
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>(1) {

        @Override
        public void onNext(Integer t) {
            super.onNext(t);
            if (t == 1) {
                subs[0].onNext(2);
                subs[0].onNext(3);
            }
        }
    };
    new ParallelFlowable<Integer>() {

        @Override
        public void subscribe(Subscriber<? super Integer>[] subscribers) {
            subs[0] = subscribers[0];
            subscribers[0].onSubscribe(new BooleanSubscription());
            subscribers[0].onNext(1);
        }

        @Override
        public int parallelism() {
            return 1;
        }
    }.sequentialDelayError(1).subscribe(ts);
    ts.request(1);
    ts.assertFailure(MissingBackpressureException.class, 1, 2);
}
Also used : BooleanSubscription(io.reactivex.internal.subscriptions.BooleanSubscription) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Subscriber(org.reactivestreams.Subscriber) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Test(org.junit.Test)

Example 12 with Subscriber

use of org.reactivestreams.Subscriber in project Offer by woshiyizhijiao.

the class RxJavaActivity method flowable.

/**
 * flowable用法
 *
 * @param v
 */
public void flowable(View v) {
    Flowable<Integer> flowable = Flowable.create(new FlowableOnSubscribe<Integer>() {

        @Override
        public void subscribe(FlowableEmitter<Integer> e) throws Exception {
            e.onNext(1);
            LogUtils.e("Flowable : 1");
            e.onNext(2);
            LogUtils.e("Flowable : 2");
            e.onNext(3);
            LogUtils.e("Flowable : 3");
            e.onComplete();
        }
    }, BackpressureStrategy.ERROR);
    // Drop就是直接把存不下的事件丢弃,Latest就是只保留最新的事件
    Subscriber subscriber = new Subscriber<Integer>() {

        @Override
        public void onSubscribe(Subscription s) {
            LogUtils.e("onSubscribe");
            // 不加这行代码会报错 MissingBackpressureException
            s.request(Long.MAX_VALUE);
        }

        @Override
        public void onNext(Integer object) {
            LogUtils.e("onNext : " + object);
        }

        @Override
        public void onError(Throwable t) {
            LogUtils.e("onError");
            t.printStackTrace();
        }

        @Override
        public void onComplete() {
            LogUtils.e("onComplete");
        }
    };
    flowable.subscribe(subscriber);
}
Also used : Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription)

Example 13 with Subscriber

use of org.reactivestreams.Subscriber in project quasar by puniverse.

the class TestHelper method createDummyFailedPublisher.

public static <T> Publisher<T> createDummyFailedPublisher() {
    return new Publisher<T>() {

        @Override
        public void subscribe(Subscriber<? super T> s) {
            s.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                }

                @Override
                public void cancel() {
                }
            });
            s.onError(new RuntimeException("Can't subscribe subscriber: " + s + ", because of reasons."));
        }
    };
}
Also used : Subscriber(org.reactivestreams.Subscriber) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription)

Example 14 with Subscriber

use of org.reactivestreams.Subscriber in project ratpack by ratpack.

the class ExecutionBoundPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super T> subscriber) {
    DefaultExecution execution = DefaultExecution.require();
    execution.delimitStream(subscriber::onError, continuation -> publisher.subscribe(new Subscriber<T>() {

        private Subscription subscription;

        private final AtomicBoolean cancelled = new AtomicBoolean();

        private final AtomicBoolean pendingCancelSignal = new AtomicBoolean(true);

        private boolean dispatch(Block block) {
            if (cancelled.get()) {
                return false;
            } else if (execution.isBound()) {
                try {
                    block.execute();
                    return true;
                } catch (Exception e) {
                    throw Exceptions.uncheck(e);
                }
            } else {
                return continuation.event(block);
            }
        }

        @Override
        public void onSubscribe(final Subscription subscription) {
            this.subscription = subscription;
            dispatch(() -> subscriber.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    dispatch(() -> subscription.request(n));
                }

                @Override
                public void cancel() {
                    if (cancelled.compareAndSet(false, true)) {
                        if (execution.isBound()) {
                            subscription.cancel();
                            continuation.complete(Block.noop());
                        } else {
                            pendingCancelSignal.set(true);
                            continuation.complete(() -> {
                                if (pendingCancelSignal.compareAndSet(true, false)) {
                                    subscription.cancel();
                                }
                            });
                        }
                    }
                }
            }));
        }

        @Override
        public void onNext(final T element) {
            boolean added = dispatch(() -> {
                if (cancelled.get()) {
                    dispose(element);
                } else {
                    subscriber.onNext(element);
                }
            });
            if (!added) {
                dispose(element);
                if (cancelled.get()) {
                    if (execution.isBound() && pendingCancelSignal.compareAndSet(true, false)) {
                        subscription.cancel();
                        continuation.complete(Block.noop());
                    }
                }
            }
        }

        private void dispose(T element) {
            try {
                disposer.execute(element);
            } catch (Exception e) {
                DefaultExecution.LOGGER.warn("Exception raised disposing stream item will be ignored - ", e);
            }
        }

        @Override
        public void onComplete() {
            continuation.complete(() -> {
                if (!cancelled.get()) {
                    subscriber.onComplete();
                }
            });
        }

        @Override
        public void onError(final Throwable cause) {
            if (!cancelled.get()) {
                continuation.complete(() -> subscriber.onError(cause));
            }
        }
    }));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscriber(org.reactivestreams.Subscriber) Block(ratpack.func.Block) Subscription(org.reactivestreams.Subscription)

Example 15 with Subscriber

use of org.reactivestreams.Subscriber in project ratpack by ratpack.

the class ByteBufComposingPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super CompositeByteBuf> subscriber) {
    subscriber.onSubscribe(new ManagedSubscription<CompositeByteBuf>(subscriber, ByteBuf::release) {

        private Subscription subscription;

        private CompositeByteBuf composite;

        private volatile State state;

        @Override
        protected void onRequest(long n) {
            if (subscription == null) {
                upstream.subscribe(new Subscriber<ByteBuf>() {

                    @Override
                    public void onSubscribe(Subscription s) {
                        subscription = s;
                        state = State.Fetching;
                        s.request(1);
                    }

                    @Override
                    public void onNext(ByteBuf t) {
                        if (state == State.Closed) {
                            t.release();
                            return;
                        }
                        if (composite == null) {
                            composite = alloc.compositeBuffer(maxNum);
                        }
                        composite.addComponent(true, t);
                        if (composite.numComponents() == maxNum || composite.readableBytes() >= watermark) {
                            state = State.Writing;
                            emitNext(composite);
                            composite = null;
                            maybeFetch();
                        } else {
                            subscription.request(1);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        state = State.Closed;
                        ReferenceCountUtil.release(composite);
                        emitError(t);
                    }

                    @Override
                    public void onComplete() {
                        state = State.Closed;
                        if (composite != null) {
                            emitNext(composite);
                        }
                        emitComplete();
                    }
                });
            } else {
                maybeFetch();
            }
        }

        private void maybeFetch() {
            if (getDemand() > 0 && state != State.Fetching) {
                state = State.Fetching;
                subscription.request(1);
            }
        }

        @Override
        protected void onCancel() {
            state = State.Closed;
            ReferenceCountUtil.release(composite);
            if (subscription != null) {
                subscription.cancel();
            }
        }
    });
}
Also used : CompositeByteBuf(io.netty.buffer.CompositeByteBuf) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

Subscriber (org.reactivestreams.Subscriber)26 Subscription (org.reactivestreams.Subscription)16 Publisher (org.reactivestreams.Publisher)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Test (org.junit.Test)5 ByteBuf (io.netty.buffer.ByteBuf)4 BooleanSubscription (io.reactivex.internal.subscriptions.BooleanSubscription)4 TestSubscriber (io.reactivex.subscribers.TestSubscriber)3 InetSocketAddress (java.net.InetSocketAddress)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Exchange (org.apache.camel.Exchange)3 Test (org.testng.annotations.Test)3 TransformablePublisher (ratpack.stream.TransformablePublisher)3 ReactiveSubscription (reactor.rx.subscription.ReactiveSubscription)3 Flowable (io.reactivex.Flowable)2 TestException (io.reactivex.exceptions.TestException)2 ExecutorService (java.util.concurrent.ExecutorService)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2