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 * An interface describing a <a 021 * href="https://martinfowler.com/bliki/CircuitBreaker.html">Circuit Breaker</a> component. 022 * 023 * <p> 024 * A <em>circuit breaker</em> can be used to protect an application against unreliable 025 * services or unexpected load. It typically monitors a specific resource. As long as this 026 * resource works as expected, it stays in state <em>closed</em>, meaning that the 027 * resource can be used. If problems are encountered when using the resource, the circuit 028 * breaker can switch into state <em>open</em>; then access to this resource is 029 * prohibited. Depending on a concrete implementation, it is possible that the circuit 030 * breaker switches back to state <em>closed</em> when the resource becomes available 031 * again. 032 * </p> 033 * <p> 034 * This interface defines a generic protocol of a circuit breaker component. It should be 035 * sufficiently generic to be applied to multiple different use cases. 036 * </p> 037 * 038 * @param <T> the type of the value monitored by this circuit breaker 039 * @since 3.5 040 */ 041public interface CircuitBreaker<T> { 042 043 /** 044 * Checks the state of this circuit breaker and changes it if necessary. The return 045 * value indicates whether the circuit breaker is now in state <em>closed</em>; a value 046 * of <strong>true</strong> typically means that the current operation can continue. 047 * 048 * @return <strong>true</strong> if the circuit breaker is now closed; 049 * <strong>false</strong> otherwise. 050 */ 051 boolean checkState(); 052 053 /** 054 * Closes this circuit breaker. Its state is changed to closed. If this circuit 055 * breaker is already closed, this method has no effect. 056 */ 057 void close(); 058 059 /** 060 * Increments the monitored value and performs a check of the current state of this 061 * circuit breaker. This method works like {@link #checkState()}, but the monitored 062 * value is incremented before the state check is performed. 063 * 064 * @param increment value to increment in the monitored value of the circuit breaker 065 * @return <strong>true</strong> if the circuit breaker is now closed; 066 * <strong>false</strong> otherwise 067 */ 068 boolean incrementAndCheckState(T increment); 069 070 /** 071 * Tests the current closed state of this circuit breaker. A return value of 072 * <strong>true</strong> means that the circuit breaker is currently closed. This 073 * means that everything is okay with the monitored subsystem. 074 * 075 * @return the current closed state of this circuit breaker. 076 */ 077 boolean isClosed(); 078 079 /** 080 * Tests the current open state of this circuit breaker. A return value of 081 * <strong>true</strong> means that the circuit breaker is currently open indicating a 082 * problem in the monitored subsystem. 083 * 084 * @return the current open state of this circuit breaker. 085 */ 086 boolean isOpen(); 087 088 /** 089 * Opens this circuit breaker. Its state is changed to open. Depending on a concrete 090 * implementation, it may close itself again if the monitored subsystem becomes 091 * available. If this circuit breaker is already open, this method has no effect. 092 */ 093 void open(); 094}