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 |
* |
* |
58 |
open.setTimeout(timeoutMS); |
open.setTimeout(timeoutMS); |
59 |
|
|
60 |
this.name = name; |
this.name = name; |
61 |
|
|
62 |
reset(); |
//set correct intial state |
63 |
|
internalReset(); |
64 |
} |
} |
65 |
|
|
66 |
|
|
92 |
} |
} |
93 |
} |
} |
94 |
|
|
95 |
|
//a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events |
96 |
|
public void retripBreaker() { |
97 |
|
synchronized(this) { |
98 |
|
if (currentState != open) { // TODO:Is this conditional necessary ?? |
99 |
|
open.trip(); |
100 |
|
currentState = open; |
101 |
|
|
102 |
|
notifier.sendNotification(name, Notifier.Event.BreakerRetripped); |
103 |
|
} |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
public void attemptReset() { |
public void attemptReset() { |
108 |
synchronized(this) { |
synchronized(this) { |
109 |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
117 |
public void reset() { |
public void reset() { |
118 |
synchronized(this) { |
synchronized(this) { |
119 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
120 |
currentState = closed; |
internalReset(); |
121 |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
122 |
} |
} |
123 |
} |
} |
124 |
} |
} |
125 |
|
|
126 |
|
//This one actually sets the correct closed/reset state |
127 |
|
private void internalReset() { |
128 |
|
closed.resetFailureCount(); |
129 |
|
currentState = closed; |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
private CircuitBreakerState getState() { |
private CircuitBreakerState getState() { |
134 |
synchronized(this) { |
synchronized(this) { |