All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.hystrix.HystrixCachedObservable Maven / Gradle / Ivy

There is a newer version: 1.5.18
Show newest version
package com.netflix.hystrix;

import rx.Observable;
import rx.Subscription;
import rx.functions.Action0;
import rx.subjects.ReplaySubject;

public class HystrixCachedObservable {
    protected final Subscription originalSubscription;
    protected final Observable cachedObservable;
    private volatile int outstandingSubscriptions = 0;

    protected HystrixCachedObservable(final Observable originalObservable) {
        ReplaySubject replaySubject = ReplaySubject.create();
        this.originalSubscription = originalObservable
                .subscribe(replaySubject);

        this.cachedObservable = replaySubject
                .doOnUnsubscribe(new Action0() {
                    @Override
                    public void call() {
                        outstandingSubscriptions--;
                        if (outstandingSubscriptions == 0) {
                            originalSubscription.unsubscribe();
                        }
                    }
                })
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        outstandingSubscriptions++;
                    }
                });
    }

    public static  HystrixCachedObservable from(Observable o, AbstractCommand originalCommand) {
        return new HystrixCommandResponseFromCache(o, originalCommand);
    }

    public static  HystrixCachedObservable from(Observable o) {
        return new HystrixCachedObservable(o);
    }

    public Observable toObservable() {
        return cachedObservable;
    }

    public void unsubscribe() {
        originalSubscription.unsubscribe();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy