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