Package rx.subscriptions

Examples of rx.subscriptions.CompositeSubscription


    @Override
    public Subscriber<? super ObservableConnection<HttpClientResponse<O>, HttpClientRequest<I>>> call(
            final Subscriber<? super HttpClientResponse<O>> child) {
        final long startTimeMillis = Clock.newStartTimeMillis();
        eventsSubject.onEvent(HttpClientMetricsEvent.REQUEST_SUBMITTED);
        final CompositeSubscription cs = new CompositeSubscription();
        child.add(cs);// Unsubscribe when the child unsubscribes.

        Subscriber<ObservableConnection<HttpClientResponse<O>, HttpClientRequest<I>>> toReturn =
                new Subscriber<ObservableConnection<HttpClientResponse<O>, HttpClientRequest<I>>>() {

                    @Override
                    public void onCompleted() {
                        // Ignore onComplete of connection.
                    }

                    @Override
                    public void onError(Throwable e) {
                        child.onError(e);
                    }

                    @Override
                    public void onNext(final ObservableConnection<HttpClientResponse<O>, HttpClientRequest<I>> connection) {

                        // Why don't we close the connection on unsubscribe?
                        // See issue: https://github.com/ReactiveX/RxNetty/issues/225
                        cs.add(connection.getInput()
                                         .doOnNext(new Action1<HttpClientResponse<O>>() {
                                             @Override
                                             public void call(final HttpClientResponse<O> response) {
                                                 response.updateNoContentSubscriptionTimeoutIfNotScheduled(
                                                         responseSubscriptionTimeoutMs,
View Full Code Here


    }
   
    @Override
    public Subscriber<? super T> call(Subscriber<? super R> child) {
        final SerializedSubscriber<R> s = new SerializedSubscriber<R>(child);
        final CompositeSubscription csub = new CompositeSubscription();
        child.add(csub);
       
        return new SourceSubscriber<T, R>(s, csub, onNext, onError, onCompleted);
    }
View Full Code Here

    }
   
    @Override
    public Subscriber<? super T> call(Subscriber<? super R> child) {
        final SerializedSubscriber<R> s = new SerializedSubscriber<R>(child);
        final CompositeSubscription csub = new CompositeSubscription();
        child.add(csub);
    
        return new SourceSubscriber<T, U, R>(s, csub, collectionSelector, resultSelector);
    }
View Full Code Here

    @Override
    public void call(final Subscriber<? super T> child) {
        final Scheduler.Worker inner = scheduler.createWorker();
        child.add(inner);

        final CompositeSubscription sourceSubscriptions = new CompositeSubscription();
        child.add(sourceSubscriptions);

        final PublishSubject<Notification<?>> terminals = PublishSubject.create();


        final Action0 subscribeToSource = new Action0() {
            @Override
            public void call() {
                Subscriber<T> terminalDelegatingSubscriber = new Subscriber<T>() {
                    @Override
                    public void onCompleted() {
                        unsubscribe();
                        terminals.onNext(Notification.createOnCompleted());
                    }

                    @Override
                    public void onError(Throwable e) {
                        unsubscribe();
                        terminals.onNext(Notification.createOnError(e));
                    }

                    @Override
                    public void onNext(T v) {
                        child.onNext(v);
                    }
                };
                // new subscription each time so if it unsubscribes itself it does not prevent retries
                // by unsubscribing the child subscription
                sourceSubscriptions.add(terminalDelegatingSubscriber);
                source.unsafeSubscribe(terminalDelegatingSubscriber);
            }
        };

        final Observable<?> restarts = f.call(
View Full Code Here

        if (getExecutorService() == null) {
            // Invoke as async JAX-RS client request.
            return Observable.create(new Observable.OnSubscribe<T>() {
                @Override
                public void call(final Subscriber<? super T> subscriber) {
                    final CompositeSubscription parent = new CompositeSubscription();
                    subscriber.add(parent);

                    // return a Subscription that wraps the Future to make sure it can be cancelled
                    parent.add(Subscriptions.from(getBuilder().async().method(name, entity, new InvocationCallback<Response>() {
                        @Override
                        public void completed(final Response response) {
                            if (!subscriber.isUnsubscribed()) {
                                try {
                                    final T entity = responseType.isAssignableFrom(response.getClass())
View Full Code Here

        if (getExecutorService() == null) {
            // Invoke as async JAX-RS client request.
            return Observable.create(new Observable.OnSubscribe<T>() {
                @Override
                public void call(final Subscriber<? super T> subscriber) {
                    final CompositeSubscription parent = new CompositeSubscription();
                    subscriber.add(parent);

                    // return a Subscription that wraps the Future to make sure it can be cancelled
                    parent.add(Subscriptions.from(getBuilder().async().method(name, entity, new InvocationCallback<Response>() {
                        @Override
                        public void completed(final Response response) {
                            if (!subscriber.isUnsubscribed()) {
                                try {
                                    @SuppressWarnings("unchecked")
View Full Code Here

        }

        @Override
        public Subscriber<? super R> call(final Subscriber<? super R> child) {
            final CompositeSubscription s = new CompositeSubscription();
            // if the child unsubscribes we unsubscribe our parent as well
            child.add(s);

            /*
             * Define the action to perform on timeout outside of the TimerListener to it can capture the HystrixRequestContext
             * of the calling thread which doesn't exist on the Timer thread.
             */
            final HystrixContextRunnable timeoutRunnable = new HystrixContextRunnable(originalCommand.concurrencyStrategy, new Runnable() {

                @Override
                public void run() {
                    child.onError(new HystrixTimeoutException());
                }
            });

            TimerListener listener = new TimerListener() {

                @Override
                public void tick() {
                    // if we can go from NOT_EXECUTED to TIMED_OUT then we do the timeout codepath
                    // otherwise it means we lost a race and the run() execution completed
                    if (originalCommand.isCommandTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.TIMED_OUT)) {
                        // do fallback logic
                        // report timeout failure
                        originalCommand.metrics.markTimeout(System.currentTimeMillis() - originalCommand.invocationStartTime);

                        // we record execution time because we are returning before
                        originalCommand.recordTotalExecutionTime(originalCommand.invocationStartTime);

                        // shut down the original request
                        s.unsubscribe();

                        timeoutRunnable.run();
                    }

                }

                @Override
                public int getIntervalTimeInMilliseconds() {
                    return originalCommand.properties.executionIsolationThreadTimeoutInMilliseconds().get();
                }
            };

            Reference<TimerListener> _tl = null;
            if (isNonBlocking) {
                /*
                 * Scheduling a separate timer to do timeouts is more expensive
                 * so we'll only do it if we're being used in a non-blocking manner.
                 */
                _tl = HystrixTimer.getInstance().addTimerListener(listener);
            } else {
                /*
                 * Otherwise we just set the hook that queue().get() can trigger if a timeout occurs.
                 *
                 * This allows the blocking and non-blocking approaches to be coded basically the same way
                 * though it is admittedly awkward if we were just blocking (the use of Reference annoys me for example)
                 */
                _tl = new SoftReference<TimerListener>(listener);
            }
            final Reference<TimerListener> tl = _tl;

            // set externally so execute/queue can see this
            originalCommand.timeoutTimer.set(tl);

            /**
             * If this subscriber receives values it means the parent succeeded/completed
             */
            Subscriber<R> parent = new Subscriber<R>() {

                @Override
                public void onCompleted() {
                    if (isNotTimedOut()) {
                        // stop timer and pass notification through
                        tl.clear();
                        child.onCompleted();
                    }
                }

                @Override
                public void onError(Throwable e) {
                    if (isNotTimedOut()) {
                        // stop timer and pass notification through
                        tl.clear();
                        child.onError(e);
                    }
                }

                @Override
                public void onNext(R v) {
                    if (isNotTimedOut()) {
                        child.onNext(v);
                    }
                }

                private boolean isNotTimedOut() {
                    // if already marked COMPLETED (by onNext) or succeeds in setting to COMPLETED
                    return originalCommand.isCommandTimedOut.get() == TimedOutStatus.COMPLETED ||
                            originalCommand.isCommandTimedOut.compareAndSet(TimedOutStatus.NOT_EXECUTED, TimedOutStatus.COMPLETED);
                }

            };

            // if s is unsubscribed we want to unsubscribe the parent
            s.add(parent);

            return parent;
        }
View Full Code Here

TOP

Related Classes of rx.subscriptions.CompositeSubscription

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.