1 package net.secodo.jcircuitbreaker.breaker; 2 3 import java.util.concurrent.Callable; 4 5 import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext; 6 import net.secodo.jcircuitbreaker.breakhandler.BreakHandler; 7 import net.secodo.jcircuitbreaker.breakhandler.exception.BreakHandlerException; 8 import net.secodo.jcircuitbreaker.breakstrategy.BreakStrategy; 9 import net.secodo.jcircuitbreaker.exception.CircuitBreakerException; 10 import net.secodo.jcircuitbreaker.exception.TaskExecutionException; 11 import net.secodo.jcircuitbreaker.task.Task; 12 13 14 /** 15 * This interface defines the method which allows to execute a task within given {@link ExecutionContext}. 16 * 17 * <p>This means that implementing classes are required to pass given {@link ExecutionContext} to both 18 * <i>break strategy</i> and <i>break handler</i> since they should run with the given context and not 19 * with the new (or other) context. 20 * 21 * @param <R> the return value that the breaker returns (if breaker wants to return any value) 22 */ 23 public interface ContextAwareCircuitBreaker<R> { 24 /** 25 * Executes given task within given execution context. Implementers of this method are required to use given 26 * {@link ExecutionContext} for the whole run of the method. This especially mean that this executionContext needs to 27 * be passed to given <i>break strategy</i> and <i>break handler</i>, so that both the strategy and handler are 28 * working within given context. 29 * 30 * @param task encapsulate java <i>target-method</i> which should be executed by this circuit breaker 31 * @param breakStrategy the strategy which should define whether task should be executed or break handler should 32 * handle fallback situation 33 * @param breakHandler handles fallback situation 34 * @param executionContext already existing context which should be used for execution and be passed to both 35 * <i>break strategy</i> and <i>break handler</i> 36 * @return value returned by executing the Task or fallback value returned by <i>break handler</i> 37 * @throws TaskExecutionException in case the task was executed but resulted in exception 38 * @throws BreakHandlerException in case <i>break handler</i> was executed but was not able to provide fallback value 39 * @throws CircuitBreakerException if unexpected problem occurred while processing the task within CircuitBreaker 40 */ 41 R executeInContext(Task<R> task, BreakStrategy<R> breakStrategy, BreakHandler<R> breakHandler, 42 ExecutionContext<R> executionContext) throws TaskExecutionException, BreakHandlerException, 43 CircuitBreakerException; 44 45 }