View Javadoc
1   package net.secodo.jcircuitbreaker.breakhandler;
2   
3   import java.util.concurrent.Callable;
4   
5   import net.secodo.jcircuitbreaker.breaker.ContextAwareCircuitBreaker;
6   import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext;
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   * Handles break situation. Can result in different actions to be performed like trying to retry the task or return
16   * static fallback value instead.
17   *
18   * <p>It's main goal is to provide a fallback value for a method in case the CircuitBreaker decided not to execute the
19   * real method. If providing a fallback
20   * value is not possible break handler should throw {@link BreakHandlerException}
21   *
22   * @param <R> the return type of onBreak method. This must be the same return type as the of of executed Task
23   */
24  public interface BreakHandler<R> {
25    /**
26     * Handles situation when break occurs. Returns the fallback value in case the break happens.
27     *
28     * @param circuitBreaker   a reference to the {@link ContextAwareCircuitBreaker} which called this break handler
29     * @param task             the task which execution was prevented by break strategy and resulted in calling this
30     *                         break handler
31     * @param breakStrategy    the strategy which prevented the execution of task and resulted in calling this break
32     *                         handler
33     * @param executionContext contains current execution data (specific to current execution)
34     * @return fallback value which replaces the value returned by method call
35     * @throws TaskExecutionException  in case there was exception while executing the task by this break handler (but
36     *                                 only in case this handler decided to execute the task - there is no possibility
37     *                                 to throw TaskExecutionException in case there was no attempt to execute a task)
38     * @throws CircuitBreakerException in case the break handler tried to execute some task on circuitBreaker but the
39     *                                 circuitBreaker fallen with unexpected exception
40     *
41     * @throws BreakHandlerException   in case this break handler is not able to provide the the fallback value
42     */
43    R onBreak(ContextAwareCircuitBreaker<R> circuitBreaker, Task<R> task, BreakStrategy<R> breakStrategy,
44              ExecutionContext<R> executionContext) throws TaskExecutionException, CircuitBreakerException,
45                                                           BreakHandlerException;
46  
47  }