Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package org.testifyproject.failsafe;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.testifyproject.failsafe.function.BiPredicate;
import org.testifyproject.failsafe.function.CheckedRunnable;
import org.testifyproject.failsafe.function.Predicate;
import org.testifyproject.failsafe.internal.CircuitBreakerStats;
import org.testifyproject.failsafe.internal.CircuitState;
import org.testifyproject.failsafe.internal.ClosedState;
import org.testifyproject.failsafe.internal.HalfOpenState;
import org.testifyproject.failsafe.internal.OpenState;
import org.testifyproject.failsafe.internal.util.Assert;
import org.testifyproject.failsafe.util.Duration;
import org.testifyproject.failsafe.util.Ratio;
/**
* A circuit breaker that temporarily halts execution when configurable thresholds are exceeded.
*
* @author Jonathan Halterman
*/
public class CircuitBreaker {
/** Writes guarded by "this" */
private final AtomicReference state = new AtomicReference();
private final AtomicInteger currentExecutions = new AtomicInteger();
private final CircuitBreakerStats stats = new CircuitBreakerStats() {
@Override
public int getCurrentExecutions() {
return currentExecutions.get();
}
};
private Duration delay = Duration.NONE;
private Duration timeout;
private Ratio failureThreshold;
private Ratio successThreshold;
/** Indicates whether failures are checked by a configured failure condition */
private boolean failuresChecked;
private List> failureConditions;
CheckedRunnable onOpen;
CheckedRunnable onHalfOpen;
CheckedRunnable onClose;
/**
* Creates a Circuit that opens after a single failure, closes after a single success, and has no delay by default.
*/
public CircuitBreaker() {
failureConditions = new ArrayList>();
state.set(new ClosedState(this));
}
/**
* The state of the circuit.
*/
public enum State {
/* The circuit is closed and fully functional, allowing executions to occur. */
CLOSED,
/* The circuit is opened and not allowing executions to occur. */
OPEN,
/* The circuit is temporarily allowing executions to occur. */
HALF_OPEN;
}
/**
* Returns whether the circuit allows execution, possibly triggering a state transition.
*/
public boolean allowsExecution() {
return state.get().allowsExecution(stats);
}
/**
* Closes the circuit.
*/
public void close() {
transitionTo(State.CLOSED, onClose);
}
/**
* Specifies that a failure should be recorded if the {@code completionPredicate} matches the completion result.
*
* @throws NullPointerException if {@code completionPredicate} is null
*/
@SuppressWarnings("unchecked")
public CircuitBreaker failIf(BiPredicate completionPredicate) {
Assert.notNull(completionPredicate, "completionPredicate");
failuresChecked = true;
failureConditions.add((BiPredicate