1 |
torben |
467 |
package dk.thoerup.circuitbreaker; |
2 |
torben |
393 |
|
3 |
|
|
|
4 |
torben |
1148 |
import java.util.concurrent.ExecutorService; |
5 |
|
|
import java.util.concurrent.Executors; |
6 |
|
|
|
7 |
torben |
1306 |
import dk.thoerup.circuitbreaker.config.BreakerConfig; |
8 |
|
|
import dk.thoerup.circuitbreaker.config.StaticConfig; |
9 |
torben |
1289 |
import dk.thoerup.circuitbreaker.notification.NotiferHelper; |
10 |
torben |
467 |
import dk.thoerup.circuitbreaker.notification.Notifier; |
11 |
|
|
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
12 |
torben |
409 |
|
13 |
torben |
394 |
/* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java |
14 |
|
|
* |
15 |
|
|
* example of how it can be used |
16 |
torben |
395 |
|
17 |
|
|
private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000); |
18 |
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
19 |
torben |
393 |
class TestInvocation implements CircuitInvocation { |
20 |
|
|
String url; |
21 |
|
|
public TestInvocation(String url) { |
22 |
|
|
this.url = url; |
23 |
|
|
} |
24 |
|
|
public Object proceed() throws Exception{ |
25 |
|
|
|
26 |
|
|
URL u = new URL(url); |
27 |
|
|
URLConnection c = u.openConnection(); |
28 |
|
|
c.connect(); |
29 |
|
|
InputStream in = c.getInputStream(); |
30 |
|
|
in.close(); |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
return "hello"; |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
} |
37 |
|
|
try { |
38 |
|
|
String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); |
39 |
|
|
response.getWriter().print(s); |
40 |
torben |
452 |
} catch (Exception e) { |
41 |
torben |
393 |
logger.warning( e.getMessage() ); |
42 |
|
|
response.sendError(500); |
43 |
|
|
return; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
} |
47 |
|
|
*/ |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
public class CircuitBreaker{ |
51 |
|
|
|
52 |
torben |
443 |
private volatile CircuitBreakerState currentState; |
53 |
torben |
393 |
|
54 |
torben |
424 |
private final OpenState open = new OpenState(); |
55 |
|
|
private final HalfOpenState halfOpen = new HalfOpenState(); |
56 |
|
|
private final ClosedState closed = new ClosedState(); |
57 |
torben |
393 |
|
58 |
|
|
private String name; |
59 |
|
|
|
60 |
torben |
1148 |
private ExecutorService executor = null; |
61 |
torben |
409 |
private Notifier notifier = new NullNotifier(); |
62 |
|
|
|
63 |
torben |
1376 |
@Deprecated |
64 |
|
|
public CircuitBreaker(String name, int threshold, int timeoutMS) { |
65 |
torben |
1306 |
this(name, new StaticConfig(threshold, timeoutMS) ); |
66 |
torben |
1376 |
} |
67 |
torben |
1306 |
|
68 |
|
|
public CircuitBreaker(String name, BreakerConfig config) { |
69 |
|
|
closed.setThreshold(config); |
70 |
|
|
open.setTimeout(config); |
71 |
torben |
393 |
|
72 |
|
|
this.name = name; |
73 |
torben |
625 |
|
74 |
torben |
627 |
//set correct intial state |
75 |
|
|
internalReset(); |
76 |
torben |
393 |
} |
77 |
|
|
|
78 |
torben |
1148 |
public synchronized void shutdown() { |
79 |
|
|
if (executor != null) { |
80 |
|
|
executor.shutdown(); |
81 |
|
|
} |
82 |
|
|
} |
83 |
torben |
393 |
|
84 |
torben |
1148 |
|
85 |
torben |
450 |
public Object invoke(CircuitInvocation invocation) throws Exception |
86 |
torben |
393 |
{ |
87 |
|
|
Object result = null; |
88 |
|
|
try |
89 |
|
|
{ |
90 |
|
|
getState().preInvoke(this); |
91 |
|
|
result = invocation.proceed(); |
92 |
|
|
getState().postInvoke(this); |
93 |
|
|
} |
94 |
torben |
450 |
catch(Exception e) |
95 |
torben |
393 |
{ |
96 |
torben |
450 |
getState().onError(this, e); |
97 |
|
|
throw e; |
98 |
torben |
393 |
} |
99 |
|
|
return result; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
public void tripBreaker() { |
103 |
torben |
871 |
commonTripBreaker(Notifier.Event.BreakerTripped); |
104 |
torben |
393 |
} |
105 |
|
|
|
106 |
torben |
864 |
//a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events |
107 |
|
|
public void retripBreaker() { |
108 |
torben |
871 |
commonTripBreaker(Notifier.Event.BreakerRetripped); |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
private void commonTripBreaker(Notifier.Event event) { |
112 |
|
|
synchronized(this) { |
113 |
torben |
864 |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
114 |
|
|
open.trip(); |
115 |
|
|
currentState = open; |
116 |
|
|
|
117 |
torben |
1161 |
notifier.sendNotification(this, event); |
118 |
torben |
864 |
} |
119 |
torben |
871 |
} |
120 |
torben |
864 |
} |
121 |
|
|
|
122 |
torben |
393 |
public void attemptReset() { |
123 |
|
|
synchronized(this) { |
124 |
torben |
426 |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
125 |
|
|
currentState = halfOpen; |
126 |
torben |
1161 |
notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); |
127 |
torben |
426 |
} |
128 |
torben |
393 |
} |
129 |
torben |
409 |
|
130 |
torben |
393 |
} |
131 |
|
|
|
132 |
|
|
public void reset() { |
133 |
|
|
synchronized(this) { |
134 |
torben |
426 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
135 |
torben |
627 |
internalReset(); |
136 |
torben |
1161 |
notifier.sendNotification(this, Notifier.Event.BreakerReset); |
137 |
torben |
426 |
} |
138 |
torben |
393 |
} |
139 |
|
|
} |
140 |
|
|
|
141 |
torben |
627 |
//This one actually sets the correct closed/reset state |
142 |
|
|
private void internalReset() { |
143 |
|
|
closed.resetFailureCount(); |
144 |
|
|
currentState = closed; |
145 |
|
|
} |
146 |
torben |
397 |
|
147 |
torben |
627 |
|
148 |
torben |
393 |
private CircuitBreakerState getState() { |
149 |
|
|
synchronized(this) { |
150 |
|
|
return currentState; |
151 |
|
|
} |
152 |
|
|
} |
153 |
torben |
397 |
|
154 |
torben |
447 |
public boolean isClosed() { |
155 |
|
|
return (getState() == closed); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
public boolean isOpen() { |
159 |
|
|
return (getState() == open); |
160 |
|
|
} |
161 |
|
|
|
162 |
torben |
397 |
public String getName() { |
163 |
|
|
return name; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
public String getStateName() { |
167 |
|
|
return getState().getName(); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
public int getThreshold() { |
171 |
|
|
return closed.getThreshold(); |
172 |
|
|
} |
173 |
|
|
|
174 |
torben |
413 |
public int getTimeout() { |
175 |
|
|
return (int)open.getTimeout(); |
176 |
|
|
} |
177 |
|
|
|
178 |
torben |
397 |
public int getFailureCount() { |
179 |
|
|
if (getState() == closed) { |
180 |
|
|
return closed.getFailureCount(); |
181 |
|
|
} else { |
182 |
|
|
return -1; |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
|
186 |
|
|
public long getElapsed() { |
187 |
|
|
if (getState() == open) { |
188 |
|
|
return open.getElapsed(); |
189 |
|
|
} else { |
190 |
|
|
return -1; |
191 |
|
|
} |
192 |
|
|
} |
193 |
torben |
409 |
|
194 |
|
|
public void setNotifier(Notifier notifier) { |
195 |
|
|
this.notifier = notifier; |
196 |
|
|
} |
197 |
|
|
|
198 |
torben |
1289 |
public String getNotifierName() { |
199 |
|
|
return NotiferHelper.getName(notifier); |
200 |
torben |
409 |
} |
201 |
torben |
1148 |
|
202 |
|
|
public synchronized ExecutorService getExecutor() { |
203 |
|
|
|
204 |
|
|
if (executor == null) { |
205 |
|
|
executor = Executors.newFixedThreadPool(1); |
206 |
|
|
} |
207 |
|
|
|
208 |
|
|
return executor; |
209 |
|
|
|
210 |
|
|
} |
211 |
torben |
397 |
|
212 |
torben |
393 |
} |