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

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

There is a newer version: 1.5.18
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy