001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.concurrent;
018
019/**
020 * <p>
021 * An interface describing a <a
022 * href="http://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a> component.
023 * </p>
024 * <p>
025 * A <em>circuit breaker</em> can be used to protect an application against unreliable
026 * services or unexpected load. It typically monitors a specific resource. As long as this
027 * resource works as expected, it stays in state <em>closed</em>, meaning that the
028 * resource can be used. If problems are encountered when using the resource, the circuit
029 * breaker can switch into state <em>open</em>; then access to this resource is
030 * prohibited. Depending on a concrete implementation, it is possible that the circuit
031 * breaker switches back to state <em>closed</em> when the resource becomes available
032 * again.
033 * </p>
034 * <p>
035 * This interface defines a generic protocol of a circuit breaker component. It should be
036 * sufficiently generic to be applied to multiple different use cases.
037 * </p>
038 *
039 * @param <T> the type of the value monitored by this circuit breaker
040 * @since 3.5
041 */
042public interface CircuitBreaker<T> {
043    /**
044     * Returns the current open state of this circuit breaker. A return value of
045     * <strong>true</strong> means that the circuit breaker is currently open indicating a
046     * problem in the monitored sub system.
047     *
048     * @return the current open state of this circuit breaker
049     */
050    boolean isOpen();
051
052    /**
053     * Returns the current closed state of this circuit breaker. A return value of
054     * <strong>true</strong> means that the circuit breaker is currently closed. This
055     * means that everything is okay with the monitored sub system.
056     *
057     * @return the current closed state of this circuit breaker
058     */
059    boolean isClosed();
060
061    /**
062     * Checks the state of this circuit breaker and changes it if necessary. The return
063     * value indicates whether the circuit breaker is now in state {@code CLOSED}; a value
064     * of <strong>true</strong> typically means that the current operation can continue.
065     *
066     * @return <strong>true</strong> if the circuit breaker is now closed;
067     * <strong>false</strong> otherwise
068     */
069    boolean checkState();
070
071    /**
072     * Closes this circuit breaker. Its state is changed to closed. If this circuit
073     * breaker is already closed, this method has no effect.
074     */
075    void close();
076
077    /**
078     * Opens this circuit breaker. Its state is changed to open. Depending on a concrete
079     * implementation, it may close itself again if the monitored sub system becomes
080     * available. If this circuit breaker is already open, this method has no effect.
081     */
082    void open();
083
084    /**
085     * Increments the monitored value and performs a check of the current state of this
086     * circuit breaker. This method works like {@link #checkState()}, but the monitored
087     * value is incremented before the state check is performed.
088     *
089     * @param increment value to increment in the monitored value of the circuit breaker
090     * @return <strong>true</strong> if the circuit breaker is now closed;
091     * <strong>false</strong> otherwise
092     */
093    boolean incrementAndCheckState(T increment);
094}