Search in sources :

Example 1 with RibbonResponse

use of com.netflix.ribbon.RibbonResponse in project ribbon by Netflix.

the class HttpMetaRequest method convertToRibbonResponse.

private Observable<RibbonResponse<Observable<T>>> convertToRibbonResponse(final HystrixObservableCommandChain<T> commandChain, final Observable<ResultCommandPair<T>> hystrixNotificationObservable) {
    return Observable.create(new OnSubscribe<RibbonResponse<Observable<T>>>() {

        @Override
        public void call(final Subscriber<? super RibbonResponse<Observable<T>>> t1) {
            final Subject<T, T> subject = ReplaySubject.create();
            hystrixNotificationObservable.materialize().subscribe(new Action1<Notification<ResultCommandPair<T>>>() {

                AtomicBoolean first = new AtomicBoolean(true);

                @Override
                public void call(Notification<ResultCommandPair<T>> notification) {
                    if (first.compareAndSet(true, false)) {
                        HystrixObservableCommand<T> command = notification.isOnError() ? commandChain.getLastCommand() : notification.getValue().getCommand();
                        t1.onNext(new ResponseWithSubject<T>(subject, command));
                        t1.onCompleted();
                    }
                    if (notification.isOnNext()) {
                        subject.onNext(notification.getValue().getResult());
                    } else if (notification.isOnCompleted()) {
                        subject.onCompleted();
                    } else {
                        // onError
                        subject.onError(notification.getThrowable());
                    }
                }
            });
        }
    });
}
Also used : Action1(rx.functions.Action1) Observable(rx.Observable) Subject(rx.subjects.Subject) ReplaySubject(rx.subjects.ReplaySubject) Notification(rx.Notification) RibbonResponse(com.netflix.ribbon.RibbonResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ResultCommandPair(com.netflix.ribbon.hystrix.ResultCommandPair)

Aggregations

RibbonResponse (com.netflix.ribbon.RibbonResponse)1 ResultCommandPair (com.netflix.ribbon.hystrix.ResultCommandPair)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Notification (rx.Notification)1 Observable (rx.Observable)1 Action1 (rx.functions.Action1)1 ReplaySubject (rx.subjects.ReplaySubject)1 Subject (rx.subjects.Subject)1