com.netflix.hystrix.HystrixCommandResponseFromCache 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.functions.Action0;
import rx.functions.Action1;
import java.util.concurrent.atomic.AtomicBoolean;
public class HystrixCommandResponseFromCache extends HystrixCachedObservable {
private final AbstractCommand originalCommand;
/* package-private */ HystrixCommandResponseFromCache(Observable originalObservable, final AbstractCommand originalCommand) {
super(originalObservable);
this.originalCommand = originalCommand;
}
public Observable toObservableWithStateCopiedInto(final AbstractCommand commandToCopyStateInto) {
final AtomicBoolean completionLogicRun = new AtomicBoolean(false);
return cachedObservable
.doOnError(new Action1() {
@Override
public void call(Throwable throwable) {
if (completionLogicRun.compareAndSet(false, true)) {
commandCompleted(commandToCopyStateInto);
}
}
})
.doOnCompleted(new Action0() {
@Override
public void call() {
if (completionLogicRun.compareAndSet(false, true)) {
commandCompleted(commandToCopyStateInto);
}
}
})
.doOnUnsubscribe(new Action0() {
@Override
public void call() {
if (completionLogicRun.compareAndSet(false, true)) {
commandUnsubscribed(commandToCopyStateInto);
}
}
});
}
private void commandCompleted(final AbstractCommand commandToCopyStateInto) {
commandToCopyStateInto.executionResult = originalCommand.executionResult;
}
private void commandUnsubscribed(final AbstractCommand commandToCopyStateInto) {
commandToCopyStateInto.executionResult = commandToCopyStateInto.executionResult.addEvent(HystrixEventType.CANCELLED);
commandToCopyStateInto.executionResult = commandToCopyStateInto.executionResult.setExecutionLatency(-1);
}
}