diff --git a/callback/src/main/java/com/iluwatar/callback/App.java b/callback/src/main/java/com/iluwatar/callback/App.java index 842f01dcd..1b92d98de 100644 --- a/callback/src/main/java/com/iluwatar/callback/App.java +++ b/callback/src/main/java/com/iluwatar/callback/App.java @@ -23,16 +23,14 @@ package com.iluwatar.callback; -import org.slf4j.Logger; - import static org.slf4j.LoggerFactory.getLogger; +import org.slf4j.Logger; + /** - * - * Callback pattern is more native for functional languages where functions are - * treated as first-class citizens. Prior to Java 8 callbacks can be simulated - * using simple (alike command) interfaces. - * + * Callback pattern is more native for functional languages where functions are treated as + * first-class citizens. Prior to Java 8 callbacks can be simulated using simple (alike command) + * interfaces. */ public final class App { @@ -42,7 +40,7 @@ public final class App { } /** - * Program entry point + * Program entry point. */ public static void main(final String[] args) { Task task = new SimpleTask(); diff --git a/callback/src/main/java/com/iluwatar/callback/Callback.java b/callback/src/main/java/com/iluwatar/callback/Callback.java index 0158dcda0..ad98e27de 100644 --- a/callback/src/main/java/com/iluwatar/callback/Callback.java +++ b/callback/src/main/java/com/iluwatar/callback/Callback.java @@ -24,9 +24,7 @@ package com.iluwatar.callback; /** - * - * Callback interface - * + * Callback interface. */ public interface Callback { diff --git a/callback/src/main/java/com/iluwatar/callback/LambdasApp.java b/callback/src/main/java/com/iluwatar/callback/LambdasApp.java index 18715e3b7..f87d04969 100644 --- a/callback/src/main/java/com/iluwatar/callback/LambdasApp.java +++ b/callback/src/main/java/com/iluwatar/callback/LambdasApp.java @@ -23,24 +23,23 @@ package com.iluwatar.callback; -import org.slf4j.Logger; - import static org.slf4j.LoggerFactory.getLogger; +import org.slf4j.Logger; + /** - * - * This example generates the exact same output as {@link App} however the - * callback has been defined as a Lambdas expression. - * + * This example generates the exact same output as {@link App} however the callback has been defined + * as a Lambdas expression. */ public final class LambdasApp { private static final Logger LOGGER = getLogger(LambdasApp.class); - private LambdasApp() { } + private LambdasApp() { + } /** - * Program entry point + * Program entry point. */ public static void main(final String[] args) { Task task = new SimpleTask(); diff --git a/callback/src/main/java/com/iluwatar/callback/SimpleTask.java b/callback/src/main/java/com/iluwatar/callback/SimpleTask.java index 21162833a..f12448d96 100644 --- a/callback/src/main/java/com/iluwatar/callback/SimpleTask.java +++ b/callback/src/main/java/com/iluwatar/callback/SimpleTask.java @@ -23,14 +23,12 @@ package com.iluwatar.callback; -import org.slf4j.Logger; - import static org.slf4j.LoggerFactory.getLogger; +import org.slf4j.Logger; + /** - * - * Implementation of task that need to be executed - * + * Implementation of task that need to be executed. */ public final class SimpleTask extends Task { @@ -39,6 +37,6 @@ public final class SimpleTask extends Task { @Override public void execute() { LOGGER.info("Perform some important activity and after call the" - + " callback method."); + + " callback method."); } } diff --git a/callback/src/main/java/com/iluwatar/callback/Task.java b/callback/src/main/java/com/iluwatar/callback/Task.java index 15cd99e9a..09c8f67af 100644 --- a/callback/src/main/java/com/iluwatar/callback/Task.java +++ b/callback/src/main/java/com/iluwatar/callback/Task.java @@ -24,14 +24,12 @@ package com.iluwatar.callback; /** - * - * Template-method class for callback hook execution - * + * Template-method class for callback hook execution. */ public abstract class Task { /** - * Execute with callback + * Execute with callback. */ final void executeWith(final Callback callback) { execute(); diff --git a/chain/src/main/java/com/iluwatar/chain/App.java b/chain/src/main/java/com/iluwatar/chain/App.java index c45d68276..d6b7ebe2e 100644 --- a/chain/src/main/java/com/iluwatar/chain/App.java +++ b/chain/src/main/java/com/iluwatar/chain/App.java @@ -24,23 +24,21 @@ package com.iluwatar.chain; /** - * * The Chain of Responsibility pattern is a design pattern consisting of command objects and a * series of processing objects. Each processing object contains logic that defines the types of * command objects that it can handle; the rest are passed to the next processing object in the * chain. A mechanism also exists for adding new processing objects to the end of this chain. - *
- * In this example we organize the request handlers ({@link RequestHandler}) into a chain where each - * handler has a chance to act on the request on its turn. Here the king ({@link OrcKing}) makes - * requests and the military orcs ({@link OrcCommander}, {@link OrcOfficer}, {@link OrcSoldier}) - * form the handler chain. - * + * + *
In this example we organize the request handlers ({@link RequestHandler}) into a chain where + * each handler has a chance to act on the request on its turn. Here the king ({@link OrcKing}) + * makes requests and the military orcs ({@link OrcCommander}, {@link OrcOfficer}, {@link + * OrcSoldier}) form the handler chain. */ public class App { /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { diff --git a/chain/src/main/java/com/iluwatar/chain/OrcCommander.java b/chain/src/main/java/com/iluwatar/chain/OrcCommander.java index 4770eafcb..a0dab903a 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcCommander.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcCommander.java @@ -24,9 +24,7 @@ package com.iluwatar.chain; /** - * - * OrcCommander - * + * OrcCommander. */ public class OrcCommander extends RequestHandler { diff --git a/chain/src/main/java/com/iluwatar/chain/OrcKing.java b/chain/src/main/java/com/iluwatar/chain/OrcKing.java index 39243d0fd..93c01da81 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcKing.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcKing.java @@ -24,9 +24,7 @@ package com.iluwatar.chain; /** - * * OrcKing makes requests that are handled by the chain. - * */ public class OrcKing { diff --git a/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java b/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java index 6cf78b11d..abf1afe8b 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcOfficer.java @@ -24,9 +24,7 @@ package com.iluwatar.chain; /** - * - * OrcOfficer - * + * OrcOfficer. */ public class OrcOfficer extends RequestHandler { diff --git a/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java b/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java index 686840d95..f1291a0a7 100644 --- a/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java +++ b/chain/src/main/java/com/iluwatar/chain/OrcSoldier.java @@ -24,9 +24,7 @@ package com.iluwatar.chain; /** - * - * OrcSoldier - * + * OrcSoldier. */ public class OrcSoldier extends RequestHandler { diff --git a/chain/src/main/java/com/iluwatar/chain/Request.java b/chain/src/main/java/com/iluwatar/chain/Request.java index c8962aafa..621303d93 100644 --- a/chain/src/main/java/com/iluwatar/chain/Request.java +++ b/chain/src/main/java/com/iluwatar/chain/Request.java @@ -26,24 +26,24 @@ package com.iluwatar.chain; import java.util.Objects; /** - * Request + * Request. */ public class Request { /** * The type of this request, used by each item in the chain to see if they should or can handle - * this particular request + * this particular request. */ private final RequestType requestType; /** - * A description of the request + * A description of the request. */ private final String requestDescription; /** * Indicates if the request is handled or not. A request can only switch state from unhandled to - * handled, there's no way to 'unhandle' a request + * handled, there's no way to 'unhandle' a request. */ private boolean handled; @@ -59,7 +59,7 @@ public class Request { } /** - * Get a description of the request + * Get a description of the request. * * @return A human readable description of the request */ @@ -69,7 +69,7 @@ public class Request { /** * Get the type of this request, used by each person in the chain of command to see if they should - * or can handle this particular request + * or can handle this particular request. * * @return The request type */ @@ -78,14 +78,14 @@ public class Request { } /** - * Mark the request as handled + * Mark the request as handled. */ public void markHandled() { this.handled = true; } /** - * Indicates if this request is handled or not + * Indicates if this request is handled or not. * * @return true when the request is handled, false if not */ diff --git a/chain/src/main/java/com/iluwatar/chain/RequestHandler.java b/chain/src/main/java/com/iluwatar/chain/RequestHandler.java index aed5c0611..7923f03a6 100644 --- a/chain/src/main/java/com/iluwatar/chain/RequestHandler.java +++ b/chain/src/main/java/com/iluwatar/chain/RequestHandler.java @@ -27,9 +27,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * - * RequestHandler - * + * RequestHandler. */ public abstract class RequestHandler { @@ -42,7 +40,7 @@ public abstract class RequestHandler { } /** - * Request handler + * Request handler. */ public void handleRequest(Request req) { if (next != null) { diff --git a/chain/src/main/java/com/iluwatar/chain/RequestType.java b/chain/src/main/java/com/iluwatar/chain/RequestType.java index 4e377a2b6..b63cf9402 100644 --- a/chain/src/main/java/com/iluwatar/chain/RequestType.java +++ b/chain/src/main/java/com/iluwatar/chain/RequestType.java @@ -24,9 +24,7 @@ package com.iluwatar.chain; /** - * - * RequestType enumeration - * + * RequestType enumeration. */ public enum RequestType { diff --git a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/App.java b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/App.java index 054158e9b..c3465d801 100644 --- a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/App.java +++ b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/App.java @@ -28,42 +28,39 @@ import org.slf4j.LoggerFactory; /** *
- * 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 *
*- * 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. *
*- * This works as follows: The {@link CircuitBreaker} object can be in one of three - * states: Open, Closed and Half-Open, 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: + * Open, Closed and Half-Open, 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. *
*/ 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()); } diff --git a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/CircuitBreaker.java b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/CircuitBreaker.java index 005a000df..18268b1ce 100644 --- a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/CircuitBreaker.java +++ b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/CircuitBreaker.java @@ -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); diff --git a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/DelayedService.java b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/DelayedService.java index b291c3fe5..13861923b 100644 --- a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/DelayedService.java +++ b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/DelayedService.java @@ -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) { diff --git a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/MonitoringService.java b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/MonitoringService.java index f46843766..e91367175 100644 --- a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/MonitoringService.java +++ b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/MonitoringService.java @@ -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) { diff --git a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/State.java b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/State.java index 92fe0f254..95fdb08d6 100644 --- a/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/State.java +++ b/circuit-breaker/src/main/java/com/iluwatar/circuitbreaker/State.java @@ -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 } \ No newline at end of file