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

com.yammer.tenacity.core.TenacityObservableCommand Maven / Gradle / Ivy

There is a newer version: 1.2.0.dw9
Show newest version
package com.yammer.tenacity.core;

import com.netflix.hystrix.*;
import com.netflix.hystrix.metric.consumer.CumulativeCommandEventCounterStream;
import com.netflix.hystrix.metric.consumer.RollingCommandEventCounterStream;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory;
import com.yammer.tenacity.core.properties.TenacityPropertyKey;
import rx.Observable;

import java.util.function.Supplier;

public abstract class TenacityObservableCommand extends HystrixObservableCommand {
    protected TenacityObservableCommand(TenacityPropertyKey tenacityPropertyKey) {
        super(HystrixObservableCommand.Setter.withGroupKey(TenacityCommand.tenacityGroupKey())
                .andCommandKey(tenacityPropertyKey));
    }

    public static HystrixCommandGroupKey commandGroupKeyFrom(String key) {
        return HystrixCommandGroupKey.Factory.asKey(key);
    }

    public static HystrixCommandProperties getCommandProperties(TenacityPropertyKey key) {
        return HystrixPropertiesFactory.getCommandProperties(key, null);
    }

    public HystrixCommandProperties getCommandProperties() {
        return HystrixPropertiesFactory.getCommandProperties(getCommandKey(), null);
    }

    public static HystrixThreadPoolProperties getThreadpoolProperties(TenacityPropertyKey key) {
        return HystrixPropertiesFactory.getThreadPoolProperties(key, null);
    }

    public static  TenacityObservableCommand.Builder builder(TenacityPropertyKey tenacityPropertyKey) {
        return new TenacityObservableCommand.Builder<>(tenacityPropertyKey);
    }

    public HystrixThreadPoolProperties getThreadpoolProperties() {
        return HystrixPropertiesFactory.getThreadPoolProperties(getThreadPoolKey(), null);
    }

    public HystrixCommandMetrics getCommandMetrics() {
        return HystrixCommandMetrics.getInstance(getCommandKey());
    }

    public static HystrixCommandMetrics getCommandMetrics(TenacityPropertyKey key) {
        return HystrixCommandMetrics.getInstance(key);
    }

    public HystrixThreadPoolMetrics getThreadpoolMetrics() {
        return HystrixThreadPoolMetrics.getInstance(getThreadPoolKey());
    }

    public static HystrixThreadPoolMetrics getThreadpoolMetrics(TenacityPropertyKey key) {
        return HystrixThreadPoolMetrics.getInstance(key);
    }

    public HystrixCircuitBreaker getCircuitBreaker() {
        return HystrixCircuitBreaker.Factory.getInstance(getCommandKey());
    }

    public static HystrixCircuitBreaker getCircuitBreaker(TenacityPropertyKey key) {
        return HystrixCircuitBreaker.Factory.getInstance(key);
    }

    public CumulativeCommandEventCounterStream getCumulativeCommandEventCounterStream() {
        return CumulativeCommandEventCounterStream.getInstance(getCommandKey(), getCommandProperties());
    }

    public RollingCommandEventCounterStream getRollingCommandEventCounterStream() {
        return RollingCommandEventCounterStream.getInstance(getCommandKey(), getCommandProperties());
    }

    public static class Builder {
        protected final TenacityPropertyKey key;
        protected Supplier> run;
        protected Supplier> fallback;

        public Builder(TenacityPropertyKey key) {
            this.key = key;
        }

        public Builder run(Supplier> fun) {
            run = fun;
            return this;
        }

        public Builder fallback(Supplier> fun) {
            fallback = fun;
            return this;
        }

        public TenacityObservableCommand build() {
            if (run == null) {
                throw new IllegalStateException("Run must be supplied.");
            }
            if (fallback == null) {
                return new TenacityObservableCommand(key) {
                    @Override
                    protected Observable construct() {
                        return run.get();
                    }
                };
            } else {
                return new TenacityObservableCommand(key) {
                    @Override
                    protected Observable construct() {
                        return run.get();
                    }

                    @Override
                    protected Observable resumeWithFallback() {
                        return fallback.get();
                    }
                };
            }
        }

        public Observable observe() {
            return build().observe();
        }
    }

    @Override
    protected abstract Observable construct();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy