com.netflix.hystrix.HystrixCachedObservable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hystrix-core Show documentation
Show all versions of hystrix-core Show documentation
hystrix-core developed by Netflix
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();
}
}