1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
3 |
import java.util.logging.Logger; |
|
4 |
|
import java.util.concurrent.ExecutorService; |
5 |
|
import java.util.concurrent.Executors; |
6 |
|
|
7 |
|
import dk.thoerup.circuitbreaker.config.BreakerConfig; |
8 |
|
import dk.thoerup.circuitbreaker.config.StaticConfig; |
9 |
|
import dk.thoerup.circuitbreaker.notification.NotiferHelper; |
10 |
|
import dk.thoerup.circuitbreaker.notification.Notifier; |
11 |
|
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
12 |
|
|
13 |
/* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java |
/* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java |
14 |
* |
* |
37 |
try { |
try { |
38 |
String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); |
String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); |
39 |
response.getWriter().print(s); |
response.getWriter().print(s); |
40 |
} catch (Throwable e) { |
} catch (Exception e) { |
41 |
logger.warning( e.getMessage() ); |
logger.warning( e.getMessage() ); |
42 |
response.sendError(500); |
response.sendError(500); |
43 |
return; |
return; |
48 |
|
|
49 |
|
|
50 |
public class CircuitBreaker{ |
public class CircuitBreaker{ |
|
Logger logger = Logger.getLogger(CircuitBreaker.class.getName()); |
|
|
|
|
51 |
|
|
52 |
private CircuitBreakerState currentState; |
private volatile CircuitBreakerState currentState; |
53 |
|
|
54 |
private OpenState open = new OpenState(); |
private final OpenState open = new OpenState(); |
55 |
private HalfOpenState halfOpen = new HalfOpenState(); |
private final HalfOpenState halfOpen = new HalfOpenState(); |
56 |
private ClosedState closed = new ClosedState(); |
private final ClosedState closed = new ClosedState(); |
57 |
|
|
58 |
private String name; |
private String name; |
59 |
|
|
60 |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
private ExecutorService executor = null; |
61 |
closed.setThreshold(threshold); |
private Notifier notifier = new NullNotifier(); |
62 |
open.setTimeout(timeoutMS); |
|
63 |
|
@Deprecated |
64 |
|
public CircuitBreaker(String name, int threshold, int timeoutMS) { |
65 |
|
this(name, new StaticConfig(threshold, timeoutMS) ); |
66 |
|
} |
67 |
|
|
68 |
|
public CircuitBreaker(String name, BreakerConfig config) { |
69 |
|
closed.setThreshold(config); |
70 |
|
open.setTimeout(config); |
71 |
|
|
72 |
this.name = name; |
this.name = name; |
73 |
|
|
74 |
reset(); |
//set correct intial state |
75 |
|
internalReset(); |
76 |
|
} |
77 |
|
|
78 |
|
public synchronized void shutdown() { |
79 |
|
if (executor != null) { |
80 |
|
executor.shutdown(); |
81 |
|
} |
82 |
} |
} |
83 |
|
|
84 |
|
|
85 |
public Object invoke(CircuitInvocation invocation) throws Throwable |
public Object invoke(CircuitInvocation invocation) throws Exception |
86 |
{ |
{ |
87 |
Object result = null; |
Object result = null; |
88 |
try |
try |
91 |
result = invocation.proceed(); |
result = invocation.proceed(); |
92 |
getState().postInvoke(this); |
getState().postInvoke(this); |
93 |
} |
} |
94 |
catch(Throwable t) |
catch(Exception e) |
95 |
{ |
{ |
96 |
getState().onError(this, t); |
getState().onError(this, e); |
97 |
throw t; |
throw e; |
98 |
} |
} |
99 |
return result; |
return result; |
100 |
} |
} |
101 |
|
|
102 |
public void tripBreaker() { |
public void tripBreaker() { |
103 |
synchronized(this) { |
commonTripBreaker(Notifier.Event.BreakerTripped); |
104 |
open.trip(); |
} |
|
currentState = open; |
|
|
|
|
|
|
|
|
logger.warning("Circuitbreaker tripBreaker - " + name); |
|
|
} |
|
105 |
|
|
106 |
|
//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 |
|
commonTripBreaker(Notifier.Event.BreakerRetripped); |
109 |
} |
} |
110 |
|
|
111 |
|
private void commonTripBreaker(Notifier.Event event) { |
112 |
|
synchronized(this) { |
113 |
|
if (currentState != open) { // TODO:Is this conditional necessary ?? |
114 |
|
open.trip(); |
115 |
|
currentState = open; |
116 |
|
|
117 |
|
notifier.sendNotification(this, event); |
118 |
|
} |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
public void attemptReset() { |
public void attemptReset() { |
123 |
synchronized(this) { |
synchronized(this) { |
124 |
currentState = halfOpen; |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
125 |
|
currentState = halfOpen; |
126 |
logger.warning("Circuitbreaker attemptReset - " + name); |
notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); |
127 |
|
} |
128 |
} |
} |
129 |
|
|
130 |
} |
} |
131 |
|
|
132 |
public void reset() { |
public void reset() { |
133 |
synchronized(this) { |
synchronized(this) { |
134 |
currentState = closed; |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
135 |
|
internalReset(); |
136 |
logger.warning("Circuitbreaker reset - " + name); |
notifier.sendNotification(this, Notifier.Event.BreakerReset); |
137 |
|
} |
138 |
} |
} |
139 |
} |
} |
140 |
|
|
141 |
|
//This one actually sets the correct closed/reset state |
142 |
|
private void internalReset() { |
143 |
|
closed.resetFailureCount(); |
144 |
|
currentState = closed; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
private CircuitBreakerState getState() { |
private CircuitBreakerState getState() { |
149 |
synchronized(this) { |
synchronized(this) { |
151 |
} |
} |
152 |
} |
} |
153 |
|
|
154 |
|
public boolean isClosed() { |
155 |
|
return (getState() == closed); |
156 |
|
} |
157 |
|
|
158 |
|
public boolean isOpen() { |
159 |
|
return (getState() == open); |
160 |
|
} |
161 |
|
|
162 |
public String getName() { |
public String getName() { |
163 |
return name; |
return name; |
164 |
} |
} |
171 |
return closed.getThreshold(); |
return closed.getThreshold(); |
172 |
} |
} |
173 |
|
|
174 |
|
public int getTimeout() { |
175 |
|
return (int)open.getTimeout(); |
176 |
|
} |
177 |
|
|
178 |
public int getFailureCount() { |
public int getFailureCount() { |
179 |
if (getState() == closed) { |
if (getState() == closed) { |
180 |
return closed.getFailureCount(); |
return closed.getFailureCount(); |
190 |
return -1; |
return -1; |
191 |
} |
} |
192 |
} |
} |
193 |
|
|
194 |
|
public void setNotifier(Notifier notifier) { |
195 |
|
this.notifier = notifier; |
196 |
|
} |
197 |
|
|
198 |
|
public String getNotifierName() { |
199 |
|
return NotiferHelper.getName(notifier); |
200 |
|
} |
201 |
|
|
202 |
|
public synchronized ExecutorService getExecutor() { |
203 |
|
|
204 |
|
if (executor == null) { |
205 |
|
executor = Executors.newFixedThreadPool(1); |
206 |
|
} |
207 |
|
|
208 |
|
return executor; |
209 |
|
|
210 |
|
} |
211 |
|
|
212 |
} |
} |