1 package net.secodo.jcircuitbreaker.breakhandler.impl;
2
3 import java.lang.reflect.Constructor;
4
5 import java.util.Optional;
6 import java.util.concurrent.Callable;
7
8 import net.secodo.jcircuitbreaker.breaker.ContextAwareCircuitBreaker;
9 import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext;
10 import net.secodo.jcircuitbreaker.breakhandler.BreakHandler;
11 import net.secodo.jcircuitbreaker.breakhandler.BreakHandlerFactory;
12 import net.secodo.jcircuitbreaker.breakhandler.exception.BreakHandlerException;
13 import net.secodo.jcircuitbreaker.breakstrategy.BreakStrategy;
14 import net.secodo.jcircuitbreaker.task.Task;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class ExceptionThrowingHandler<R> implements BreakHandler<R> {
32 private final String exceptionMessage;
33 private final Class<? extends BreakHandlerException> exceptionClass;
34 private final Optional<ExceptionThrowingHandlerMessageCallback<R>> messageCallback;
35
36
37
38
39
40
41
42
43 public ExceptionThrowingHandler(String exceptionMessage) {
44 this.exceptionClass = BreakHandlerException.class;
45 this.exceptionMessage = exceptionMessage;
46 this.messageCallback = Optional.empty();
47 }
48
49
50
51
52
53
54
55
56 public ExceptionThrowingHandler(ExceptionThrowingHandlerMessageCallback<R> callback) {
57 this.messageCallback = Optional.ofNullable(callback);
58 this.exceptionClass = BreakHandlerException.class;
59 this.exceptionMessage = "";
60 }
61
62
63
64
65
66
67
68
69
70 public ExceptionThrowingHandler(Class<? extends BreakHandlerException> exceptionClass, String exceptionMessage) {
71 this.exceptionClass = exceptionClass;
72 this.exceptionMessage = exceptionMessage;
73 this.messageCallback = Optional.empty();
74 }
75
76
77
78
79
80
81
82
83
84 public ExceptionThrowingHandler(Class<? extends BreakHandlerException> exceptionClass,
85 ExceptionThrowingHandlerMessageCallback<R> callback) {
86 this.exceptionClass = exceptionClass;
87 this.exceptionMessage = "";
88 this.messageCallback = Optional.of(callback);
89 }
90
91 @Override
92 public R onBreak(ContextAwareCircuitBreaker<R> circuitBreaker, Task<R> task, BreakStrategy<R> breakStrategy,
93 ExecutionContext<R> executionContext) throws BreakHandlerException {
94 final BreakHandlerException exceptionToThrow;
95 String message = null;
96 try {
97 message = buildMessage(task, executionContext);
98
99 final Constructor<? extends BreakHandlerException> exceptionClassConstructor = exceptionClass.getConstructor(
100 String.class);
101
102 exceptionToThrow = exceptionClassConstructor.newInstance(message);
103
104 } catch (Exception ex) {
105 throw new BreakHandlerException(
106 "Unable to throw custom exception of class: " + exceptionClass + " and message: " + message,
107 ex);
108 }
109
110 throw exceptionToThrow;
111 }
112
113 protected String buildMessage(Task<R> task, ExecutionContext<R> executionContext) {
114 if (messageCallback.isPresent()) {
115 return messageCallback.get().buildMessage(task, executionContext);
116 } else {
117 return this.exceptionMessage;
118 }
119 }
120 }