Java通过central config 开关实现双写控制

 抽象类:

import com.citi.risk.core.configuration.api.Configuration;

public abstract class AbstractXXXSwitcher<I> {

    @Autowised
    private Configuration configuration;

    private I executor_1;

    private I executor_2;

    protected AbstractClimateRiskSwitcher(I executor_1, I executor_2) {
        this.executor_1 = executor_1;
        this.executor_2 = executor_2;
    }

    protected <R> R read(Function<I, R> function) {
        R data;
        int mode = getAccessMode();
        if (mode == 0) {
            data = function.apply(executor_1);
        } else {
            data = function.apply(executor_2);
        }

        return data;
    }

    protected void readAndSet(Consumer<I> consumer) {
        int mode = getAccessMode();
        if (mode == 0) {
            consumer.accept(executor_1);
        } else {
            consumer.accept(executor_2);
        }
    }

    protected void write(Consumer<I> consumer) {
        int mode = getAccessMode();
        try {
            consumer.accept(executor_2);
        } catch (Exception e) {
            LOGGER.error("Exception thrown in write of MS", e);
        }
        if (mode == 0 || mode == 1) {
            consumer.accept(executor_1);
        }
    }

    protected <R> R writeAndGet(Function<I, R> function) {
        R data;
        int mode = getAccessMode();
        if (mode == 0) {
            try {
                function.apply(executor_2);
            } catch (Exception e) {
                LOGGER.error("Exception thrown in writeAndGet of MS", e);
            }
            data = function.apply(executor_1);
        } else if (mode == 1) {
            data = function.apply(executor_2);
            function.apply(executor_1);
        } else {
            data = function.apply(executor_2);
        }

        return data;
    }

    /**
     * 
     * 0: write to both executor_1 and executor_2, read from executor_1.
     * 1: write to both executor_1 and executor_2, read from executor_2.
     * 2: write to executor_2 only, read from executor_2.
     *
     * @return access mode.
     */
    private int getAccessMode() {
        return Integer.parseInt(configuration.getPrefixProperties("PREFIX").getProperty("PROPERTY", "0"));
    }
}

 最后获取开关的值可以配置在central config里,通过更改这个值就可以实现对双写的控制

configuration.getPrefixProperties("PREFIX").getProperty("PROPERTY", "0")

具体用法:

@Singleton
public class XXXHandlerProxy extends AbstractXXXSwitcher<XXXHandler> implements XXXHandler {

    @Inject
    public XXXHandlerProxy(Handler1 handler1 , Handler2 handler2) {
        super(handler1 , handler2);
    }

    @Override
    public Object foo(Object o) {
        return read(handler -> handler.method(o));
    }
}

XXXHandler 接口: 

public interface XXXHandler {

    Object foo(Object o);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micrle_007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值