1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
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; |
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 |
} |
} |
106 |
public void reset() { |
public void reset() { |
107 |
synchronized(this) { |
synchronized(this) { |
108 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
109 |
|
closed.resetFailureCount(); |
110 |
currentState = closed; |
currentState = closed; |
111 |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
112 |
} |
} |
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 |
} |
} |