1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
|
import java.util.logging.Logger; |
|
3 |
|
|
4 |
import dk.thoerup.curcuitbreaker.notification.Notifier; |
import dk.thoerup.circuitbreaker.notification.Notifier; |
5 |
import dk.thoerup.curcuitbreaker.notification.NullNotifier; |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
6 |
|
|
7 |
/* 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 |
8 |
* |
* |
31 |
try { |
try { |
32 |
String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); |
String s = (String) cb.invoke(new TestInvocation("http://rafiki/test")); |
33 |
response.getWriter().print(s); |
response.getWriter().print(s); |
34 |
} catch (Throwable e) { |
} catch (Exception e) { |
35 |
logger.warning( e.getMessage() ); |
logger.warning( e.getMessage() ); |
36 |
response.sendError(500); |
response.sendError(500); |
37 |
return; |
return; |
42 |
|
|
43 |
|
|
44 |
public class CircuitBreaker{ |
public class CircuitBreaker{ |
|
Logger logger = Logger.getLogger(CircuitBreaker.class.getName()); |
|
|
|
|
45 |
|
|
46 |
private CircuitBreakerState currentState; |
private volatile CircuitBreakerState currentState; |
47 |
|
|
48 |
private OpenState open = new OpenState(); |
private final OpenState open = new OpenState(); |
49 |
private HalfOpenState halfOpen = new HalfOpenState(); |
private final HalfOpenState halfOpen = new HalfOpenState(); |
50 |
private ClosedState closed = new ClosedState(); |
private final ClosedState closed = new ClosedState(); |
51 |
|
|
52 |
private String name; |
private String name; |
53 |
|
|
58 |
open.setTimeout(timeoutMS); |
open.setTimeout(timeoutMS); |
59 |
|
|
60 |
this.name = name; |
this.name = name; |
61 |
|
|
62 |
reset(); |
// Initial state is closed - like a reset() was called initially |
63 |
|
closed.resetFailureCount(); |
64 |
|
currentState = closed; |
65 |
} |
} |
66 |
|
|
67 |
|
|
68 |
public Object invoke(CircuitInvocation invocation) throws Throwable |
public Object invoke(CircuitInvocation invocation) throws Exception |
69 |
{ |
{ |
70 |
Object result = null; |
Object result = null; |
71 |
try |
try |
74 |
result = invocation.proceed(); |
result = invocation.proceed(); |
75 |
getState().postInvoke(this); |
getState().postInvoke(this); |
76 |
} |
} |
77 |
catch(Throwable t) |
catch(Exception e) |
78 |
{ |
{ |
79 |
getState().onError(this, t); |
getState().onError(this, e); |
80 |
throw t; |
throw e; |
81 |
} |
} |
82 |
return result; |
return result; |
83 |
} |
} |
84 |
|
|
85 |
public void tripBreaker() { |
public void tripBreaker() { |
86 |
synchronized(this) { |
synchronized(this) { |
87 |
open.trip(); |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
88 |
currentState = open; |
open.trip(); |
89 |
|
currentState = open; |
90 |
|
|
91 |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
92 |
|
} |
93 |
} |
} |
94 |
} |
} |
95 |
|
|
96 |
public void attemptReset() { |
public void attemptReset() { |
97 |
synchronized(this) { |
synchronized(this) { |
98 |
currentState = halfOpen; |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
99 |
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
currentState = halfOpen; |
100 |
|
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
101 |
|
} |
102 |
} |
} |
103 |
|
|
104 |
} |
} |
105 |
|
|
106 |
public void reset() { |
public void reset() { |
107 |
synchronized(this) { |
synchronized(this) { |
108 |
currentState = closed; |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
109 |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
closed.resetFailureCount(); |
110 |
|
currentState = closed; |
111 |
|
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
112 |
|
} |
113 |
} |
} |
114 |
} |
} |
115 |
|
|
120 |
} |
} |
121 |
} |
} |
122 |
|
|
123 |
|
public boolean isClosed() { |
124 |
|
return (getState() == closed); |
125 |
|
} |
126 |
|
|
127 |
|
public boolean isOpen() { |
128 |
|
return (getState() == open); |
129 |
|
} |
130 |
|
|
131 |
public String getName() { |
public String getName() { |
132 |
return name; |
return name; |
133 |
} |
} |
140 |
return closed.getThreshold(); |
return closed.getThreshold(); |
141 |
} |
} |
142 |
|
|
143 |
|
public int getTimeout() { |
144 |
|
return (int)open.getTimeout(); |
145 |
|
} |
146 |
|
|
147 |
public int getFailureCount() { |
public int getFailureCount() { |
148 |
if (getState() == closed) { |
if (getState() == closed) { |
149 |
return closed.getFailureCount(); |
return closed.getFailureCount(); |
165 |
} |
} |
166 |
|
|
167 |
public String getNotifierName() { |
public String getNotifierName() { |
168 |
return notifier.toString(); |
return notifier.getClass().getName(); |
169 |
} |
} |
170 |
|
|
171 |
} |
} |