1 |
package dk.thoerup.curcuitbreaker; |
2 |
|
3 |
|
4 |
import dk.thoerup.curcuitbreaker.notification.Notifier; |
5 |
import dk.thoerup.curcuitbreaker.notification.NullNotifier; |
6 |
|
7 |
/* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java |
8 |
* |
9 |
* example of how it can be used |
10 |
|
11 |
private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000); |
12 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
13 |
class TestInvocation implements CircuitInvocation { |
14 |
String url; |
15 |
public TestInvocation(String url) { |
16 |
this.url = url; |
17 |
} |
18 |
public Object proceed() throws Exception{ |
19 |
|
20 |
URL u = new URL(url); |
21 |
URLConnection c = u.openConnection(); |
22 |
c.connect(); |
23 |
InputStream in = c.getInputStream(); |
24 |
in.close(); |
25 |
|
26 |
|
27 |
return "hello"; |
28 |
} |
29 |
|
30 |
} |
31 |
try { |
32 |
String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); |
33 |
response.getWriter().print(s); |
34 |
} catch (Throwable e) { |
35 |
logger.warning( e.getMessage() ); |
36 |
response.sendError(500); |
37 |
return; |
38 |
} |
39 |
|
40 |
} |
41 |
*/ |
42 |
|
43 |
|
44 |
public class CircuitBreaker{ |
45 |
|
46 |
private volatile CircuitBreakerState currentState; |
47 |
|
48 |
private final OpenState open = new OpenState(); |
49 |
private final HalfOpenState halfOpen = new HalfOpenState(); |
50 |
private final ClosedState closed = new ClosedState(); |
51 |
|
52 |
private String name; |
53 |
|
54 |
private Notifier notifier = new NullNotifier(); |
55 |
|
56 |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
57 |
closed.setThreshold(threshold); |
58 |
open.setTimeout(timeoutMS); |
59 |
|
60 |
this.name = name; |
61 |
|
62 |
reset(); |
63 |
} |
64 |
|
65 |
|
66 |
public Object invoke(CircuitInvocation invocation) throws Throwable |
67 |
{ |
68 |
Object result = null; |
69 |
try |
70 |
{ |
71 |
getState().preInvoke(this); |
72 |
result = invocation.proceed(); |
73 |
getState().postInvoke(this); |
74 |
} |
75 |
catch(Throwable t) |
76 |
{ |
77 |
getState().onError(this, t); |
78 |
throw t; |
79 |
} |
80 |
return result; |
81 |
} |
82 |
|
83 |
public void tripBreaker() { |
84 |
synchronized(this) { |
85 |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
86 |
open.trip(); |
87 |
currentState = open; |
88 |
|
89 |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
90 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
public void attemptReset() { |
95 |
synchronized(this) { |
96 |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
97 |
currentState = halfOpen; |
98 |
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
99 |
} |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
public void reset() { |
105 |
synchronized(this) { |
106 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
107 |
currentState = closed; |
108 |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
109 |
} |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
private CircuitBreakerState getState() { |
115 |
synchronized(this) { |
116 |
return currentState; |
117 |
} |
118 |
} |
119 |
|
120 |
public boolean isClosed() { |
121 |
return (getState() == closed); |
122 |
} |
123 |
|
124 |
public boolean isOpen() { |
125 |
return (getState() == open); |
126 |
} |
127 |
|
128 |
public String getName() { |
129 |
return name; |
130 |
} |
131 |
|
132 |
public String getStateName() { |
133 |
return getState().getName(); |
134 |
} |
135 |
|
136 |
public int getThreshold() { |
137 |
return closed.getThreshold(); |
138 |
} |
139 |
|
140 |
public int getTimeout() { |
141 |
return (int)open.getTimeout(); |
142 |
} |
143 |
|
144 |
public int getFailureCount() { |
145 |
if (getState() == closed) { |
146 |
return closed.getFailureCount(); |
147 |
} else { |
148 |
return -1; |
149 |
} |
150 |
} |
151 |
|
152 |
public long getElapsed() { |
153 |
if (getState() == open) { |
154 |
return open.getElapsed(); |
155 |
} else { |
156 |
return -1; |
157 |
} |
158 |
} |
159 |
|
160 |
public void setNotifier(Notifier notifier) { |
161 |
this.notifier = notifier; |
162 |
} |
163 |
|
164 |
public String getNotifierName() { |
165 |
return notifier.getClass().getName(); |
166 |
} |
167 |
|
168 |
} |