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

com.github.phantomthief.failover.impl.RecoverableCheckFailoverBuilder Maven / Gradle / Ivy

There is a newer version: 0.1.32
Show newest version
package com.github.phantomthief.failover.impl;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.slf4j.LoggerFactory.getLogger;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;

import org.slf4j.Logger;

@Deprecated
public final class RecoverableCheckFailoverBuilder {

    private static final int DEFAULT_FAIL_COUNT = 10;
    private static final long DEFAULT_FAIL_DURATION = MINUTES.toMillis(1);
    private static final long DEFAULT_RECOVERY_CHECK_DURATION = SECONDS.toMillis(5);
    private static final Logger logger = getLogger(RecoverableCheckFailover.class);
    private int failCount;
    private long failDuration;
    private long recoveryCheckDuration;
    private boolean returnOriginalWhileAllFailed;
    private Predicate checker;

    @CheckReturnValue
    @Nonnull
    public RecoverableCheckFailoverBuilder setFailCount(int failCount) {
        this.failCount = failCount;
        return this;
    }

    @SuppressWarnings("unchecked")
    @CheckReturnValue
    @Nonnull
    public  RecoverableCheckFailoverBuilder setChecker(Predicate checker) {
        RecoverableCheckFailoverBuilder thisBuilder = (RecoverableCheckFailoverBuilder) this;
        thisBuilder.checker = thisBuilder.catching((Predicate) checker);
        return thisBuilder;
    }

    @CheckReturnValue
    @Nonnull
    public RecoverableCheckFailoverBuilder setRecoveryCheckDuration(long recoveryCheckDuration,
            TimeUnit unit) {
        this.recoveryCheckDuration = unit.toMillis(recoveryCheckDuration);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public RecoverableCheckFailoverBuilder setFailDuration(long failDuration, TimeUnit unit) {
        this.failDuration = unit.toMillis(failDuration);
        return this;
    }

    @CheckReturnValue
    @Nonnull
    public RecoverableCheckFailoverBuilder
            setReturnOriginalWhileAllFailed(boolean returnOriginalWhileAllFailed) {
        this.returnOriginalWhileAllFailed = returnOriginalWhileAllFailed;
        return this;
    }

    @SuppressWarnings("unchecked")
    @Nonnull
    public  RecoverableCheckFailover build(List original) {
        RecoverableCheckFailoverBuilder thisBuilder = (RecoverableCheckFailoverBuilder) this;
        thisBuilder.ensure();
        return new RecoverableCheckFailover<>((List) original, thisBuilder.checker, failCount,
                failDuration, recoveryCheckDuration, returnOriginalWhileAllFailed);
    }

    private void ensure() {
        checkNotNull(checker);

        if (failCount <= 0) {
            failCount = DEFAULT_FAIL_COUNT;
        }
        if (failDuration <= 0) {
            failDuration = DEFAULT_FAIL_DURATION;
        }
        if (recoveryCheckDuration <= 0) {
            recoveryCheckDuration = DEFAULT_RECOVERY_CHECK_DURATION;
        }
    }

    private Predicate catching(Predicate predicate) {
        return t -> {
            try {
                return predicate.test(t);
            } catch (Throwable e) {
                logger.error("Ops. fail to test:{}", t, e);
                return false;
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy