Resolves checkstyle errors for callback, chain, circuit-breaker (#1060)
* Reduces checkstyle errors in callback * Reduces checkstyle errors in chain * Reduces checkstyle errors in circuit-breaker
This commit is contained in:
committed by
Ilkka Seppälä
parent
efc17fcc70
commit
31f27a720b
@ -28,42 +28,39 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* The intention of the Circuit Builder pattern is to handle remote failures
|
||||
* robustly, which is to mean that if a service is dependant on n number of
|
||||
* other services, and m of them fail, we should be able to recover from that
|
||||
* failure by ensuring that the user can still use the services that are actually
|
||||
* functional, and resources are not tied up by uselessly by the services which
|
||||
* are not working. However, we should also be able to detect when any of the m
|
||||
* failing services become operational again, so that we can use it
|
||||
* The intention of the Circuit Builder pattern is to handle remote failures robustly, which is to
|
||||
* mean that if a service is dependant on n number of other services, and m of them fail, we should
|
||||
* be able to recover from that failure by ensuring that the user can still use the services that
|
||||
* are actually functional, and resources are not tied up by uselessly by the services which are not
|
||||
* working. However, we should also be able to detect when any of the m failing services become
|
||||
* operational again, so that we can use it
|
||||
* </p>
|
||||
* <p>
|
||||
* In this example, the circuit breaker pattern is demonstrated by using two services:
|
||||
* {@link MonitoringService} and {@link DelayedService}. The monitoring service
|
||||
* is responsible for calling two services: a local service and a remote service {@link DelayedService}
|
||||
* , and by using the circuit breaker construction we ensure that if the call to
|
||||
* remote service is going to fail, we are going to save our resources and not make the
|
||||
* function call at all, by wrapping our call to the remote service in the circuit
|
||||
* breaker object.
|
||||
* In this example, the circuit breaker pattern is demonstrated by using two services: {@link
|
||||
* MonitoringService} and {@link DelayedService}. The monitoring service is responsible for calling
|
||||
* two services: a local service and a remote service {@link DelayedService} , and by using the
|
||||
* circuit breaker construction we ensure that if the call to remote service is going to fail, we
|
||||
* are going to save our resources and not make the function call at all, by wrapping our call to
|
||||
* the remote service in the circuit breaker object.
|
||||
* </p>
|
||||
* <p>
|
||||
* This works as follows: The {@link CircuitBreaker} object can be in one of three
|
||||
* states: <b>Open</b>, <b>Closed</b> and <b>Half-Open</b>, which represents the real
|
||||
* world circuits. If the state is closed (initial), we assume everything is alright
|
||||
* and perform the function call. However, every time the call fails, we note it
|
||||
* and once it crosses a threshold, we set the state to Open, preventing any further
|
||||
* calls to the remote server. Then, after a certain retry period (during which we
|
||||
* expect thee service to recover), we make another call to the remote server and
|
||||
* this state is called the Half-Open state, where it stays till the service is down,
|
||||
* and once it recovers, it goes back to the closed state and the cycle continues.
|
||||
* This works as follows: The {@link CircuitBreaker} object can be in one of three states:
|
||||
* <b>Open</b>, <b>Closed</b> and <b>Half-Open</b>, which represents the real world circuits. If the
|
||||
* state is closed (initial), we assume everything is alright and perform the function call.
|
||||
* However, every time the call fails, we note it and once it crosses a threshold, we set the state
|
||||
* to Open, preventing any further calls to the remote server. Then, after a certain retry period
|
||||
* (during which we expect thee service to recover), we make another call to the remote server and
|
||||
* this state is called the Half-Open state, where it stays till the service is down, and once it
|
||||
* recovers, it goes back to the closed state and the cycle continues.
|
||||
* </p>
|
||||
*/
|
||||
public class App {
|
||||
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
@SuppressWarnings("squid:S2189")
|
||||
@ -71,14 +68,14 @@ public class App {
|
||||
//Create an object of monitoring service which makes both local and remote calls
|
||||
var obj = new MonitoringService();
|
||||
//Set the circuit Breaker parameters
|
||||
var circuitBreaker = new CircuitBreaker(3000, 1, 2000 * 1000 * 1000);
|
||||
var circuitBreaker = new CircuitBreaker(3000, 1, 2000 * 1000 * 1000);
|
||||
var serverStartTime = System.nanoTime();
|
||||
while (true) {
|
||||
LOGGER.info(obj.localResourceResponse());
|
||||
LOGGER.info(obj.remoteResourceResponse(circuitBreaker, serverStartTime));
|
||||
LOGGER.info(circuitBreaker.getState());
|
||||
try {
|
||||
Thread.sleep(5 * 1000);
|
||||
Thread.sleep(5 * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
|
@ -24,7 +24,7 @@
|
||||
package com.iluwatar.circuitbreaker;
|
||||
|
||||
/**
|
||||
* The circuit breaker class with all configurations
|
||||
* The circuit breaker class with all configurations.
|
||||
*/
|
||||
public class CircuitBreaker {
|
||||
private final long timeout;
|
||||
@ -36,27 +36,31 @@ public class CircuitBreaker {
|
||||
private final long futureTime = 1000 * 1000 * 1000 * 1000;
|
||||
|
||||
/**
|
||||
* Constructor to create an instance of Circuit Breaker
|
||||
* @param timeout Timeout for the API request. Not necessary for this simple example
|
||||
* @param failureThreshold Number of failures we receive from the depended service before changing state to 'OPEN'
|
||||
* @param retryTimePeriod Time period after which a new request is made to remote service for status check.
|
||||
* Constructor to create an instance of Circuit Breaker.
|
||||
*
|
||||
* @param timeout Timeout for the API request. Not necessary for this simple example
|
||||
* @param failureThreshold Number of failures we receive from the depended service before changing
|
||||
* state to 'OPEN'
|
||||
* @param retryTimePeriod Time period after which a new request is made to remote service for
|
||||
* status check.
|
||||
*/
|
||||
CircuitBreaker(long timeout, int failureThreshold, long retryTimePeriod) {
|
||||
// We start in a closed state hoping that everything is fine
|
||||
this.state = State.CLOSED;
|
||||
this.failureThreshold = failureThreshold;
|
||||
// Timeout for the API request. Used to break the calls made to remote resource if it exceeds the limit
|
||||
// Timeout for the API request.
|
||||
// Used to break the calls made to remote resource if it exceeds the limit
|
||||
this.timeout = timeout;
|
||||
this.retryTimePeriod = retryTimePeriod;
|
||||
//An absurd amount of time in future which basically indicates the last failure never happened
|
||||
this.lastFailureTime = System.nanoTime() + futureTime;
|
||||
this.failureCount = 0;
|
||||
}
|
||||
|
||||
|
||||
//Reset everything to defaults
|
||||
private void reset() {
|
||||
this.failureCount = 0;
|
||||
this.lastFailureTime = System.nanoTime() + futureTime;
|
||||
this.lastFailureTime = System.nanoTime() + futureTime;
|
||||
this.state = State.CLOSED;
|
||||
}
|
||||
|
||||
@ -64,7 +68,7 @@ public class CircuitBreaker {
|
||||
failureCount = failureCount + 1;
|
||||
this.lastFailureTime = System.nanoTime();
|
||||
}
|
||||
|
||||
|
||||
protected void setState() {
|
||||
if (failureCount > failureThreshold) { //Then something is wrong with remote service
|
||||
if ((System.nanoTime() - lastFailureTime) > retryTimePeriod) {
|
||||
@ -79,23 +83,28 @@ public class CircuitBreaker {
|
||||
state = State.CLOSED;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getState() {
|
||||
return state.name();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Break the circuit beforehand if it is known service is down
|
||||
* Or connect the circuit manually if service comes online before expected
|
||||
* Break the circuit beforehand if it is known service is down Or connect the circuit manually if
|
||||
* service comes online before expected.
|
||||
*
|
||||
* @param state State at which circuit is in
|
||||
*/
|
||||
public void setStateForBypass(State state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param serviceToCall The name of the service in String. Can be changed to data URLs in case of web applications
|
||||
* @param serverStartTime Time at which actual server was started which makes calls to this service
|
||||
* Executes service call.
|
||||
*
|
||||
* @param serviceToCall The name of the service in String. Can be changed to data URLs in case
|
||||
* of web applications
|
||||
* @param serverStartTime Time at which actual server was started which makes calls to this
|
||||
* service
|
||||
* @return Value from the remote resource, stale response or a custom exception
|
||||
*/
|
||||
public String call(String serviceToCall, long serverStartTime) throws Exception {
|
||||
@ -104,7 +113,7 @@ public class CircuitBreaker {
|
||||
// return cached response if no the circuit is in OPEN state
|
||||
return "This is stale response from API";
|
||||
} else {
|
||||
// Make the API request if the circuit is not OPEN
|
||||
// Make the API request if the circuit is not OPEN
|
||||
if (serviceToCall.equals("delayedService")) {
|
||||
var delayedService = new DelayedService(20);
|
||||
var response = delayedService.response(serverStartTime);
|
||||
|
@ -24,14 +24,15 @@
|
||||
package com.iluwatar.circuitbreaker;
|
||||
|
||||
/**
|
||||
* This simulates the remote service
|
||||
* It responds only after a certain timeout period (default set to 20 seconds)
|
||||
*/
|
||||
* This simulates the remote service It responds only after a certain timeout period (default set to
|
||||
* 20 seconds).
|
||||
*/
|
||||
public class DelayedService {
|
||||
private final int delay;
|
||||
|
||||
/**
|
||||
* Constructor to create an instance of DelayedService, which is down for first few seconds
|
||||
* Constructor to create an instance of DelayedService, which is down for first few seconds.
|
||||
*
|
||||
* @param delay the delay after which service would behave properly, in seconds
|
||||
*/
|
||||
public DelayedService(int delay) {
|
||||
@ -43,7 +44,10 @@ public class DelayedService {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param serverStartTime Time at which actual server was started which makes calls to this service
|
||||
* Responds based on delay, current time and server start time if the service is down / working.
|
||||
*
|
||||
* @param serverStartTime Time at which actual server was started which makes calls to this
|
||||
* service
|
||||
* @return The state of the service
|
||||
*/
|
||||
public String response(long serverStartTime) {
|
||||
|
@ -24,8 +24,8 @@
|
||||
package com.iluwatar.circuitbreaker;
|
||||
|
||||
/**
|
||||
* The service class which makes local and remote calls
|
||||
* Uses {@link CircuitBreaker} object to ensure remote calls don't use up resources
|
||||
* The service class which makes local and remote calls Uses {@link CircuitBreaker} object to ensure
|
||||
* remote calls don't use up resources.
|
||||
*/
|
||||
public class MonitoringService {
|
||||
|
||||
@ -35,9 +35,11 @@ public class MonitoringService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get result from remote server
|
||||
* @param circuitBreaker The circuitBreaker object with all parameters
|
||||
* @param serverStartTime Time at which actual server was started which makes calls to this service
|
||||
* Try to get result from remote server.
|
||||
*
|
||||
* @param circuitBreaker The circuitBreaker object with all parameters
|
||||
* @param serverStartTime Time at which actual server was started which makes calls to this
|
||||
* service
|
||||
* @return result from the remote response or exception raised by it.
|
||||
*/
|
||||
public String remoteResourceResponse(CircuitBreaker circuitBreaker, long serverStartTime) {
|
||||
|
@ -24,10 +24,10 @@
|
||||
package com.iluwatar.circuitbreaker;
|
||||
|
||||
/**
|
||||
* Enumeration for states the circuit breaker could be in
|
||||
* Enumeration for states the circuit breaker could be in.
|
||||
*/
|
||||
public enum State {
|
||||
CLOSED,
|
||||
OPEN,
|
||||
HALF_OPEN
|
||||
CLOSED,
|
||||
OPEN,
|
||||
HALF_OPEN
|
||||
}
|
Reference in New Issue
Block a user