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