1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.curcuitbreaker; |
2 |
|
|
3 |
import java.util.logging.Logger; |
|
4 |
|
import dk.thoerup.curcuitbreaker.notification.Notifier; |
5 |
|
import dk.thoerup.curcuitbreaker.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 |
* |
* |
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 |
|
|
54 |
|
private Notifier notifier = new NullNotifier(); |
55 |
|
|
56 |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
57 |
closed.setThreshold(threshold); |
closed.setThreshold(threshold); |
58 |
open.setTimeout(timeoutMS); |
open.setTimeout(timeoutMS); |
82 |
|
|
83 |
public void tripBreaker() { |
public void tripBreaker() { |
84 |
synchronized(this) { |
synchronized(this) { |
85 |
currentState = open; |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
86 |
open.trip(); |
open.trip(); |
87 |
|
currentState = open; |
88 |
|
|
89 |
logger.warning("Circuitbreaker tripBreaker - " + name); |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
90 |
} |
} |
91 |
|
} |
92 |
} |
} |
93 |
|
|
94 |
public void attemptReset() { |
public void attemptReset() { |
95 |
synchronized(this) { |
synchronized(this) { |
96 |
currentState = halfOpen; |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
97 |
|
currentState = halfOpen; |
98 |
logger.warning("Circuitbreaker attemptReset - " + name); |
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
99 |
|
} |
100 |
} |
} |
101 |
|
|
102 |
} |
} |
103 |
|
|
104 |
public void reset() { |
public void reset() { |
105 |
synchronized(this) { |
synchronized(this) { |
106 |
currentState = closed; |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
107 |
|
currentState = closed; |
108 |
logger.warning("Circuitbreaker reset - " + name); |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
109 |
|
} |
110 |
} |
} |
111 |
} |
} |
112 |
|
|
113 |
|
|
114 |
private CircuitBreakerState getState() { |
private CircuitBreakerState getState() { |
115 |
synchronized(this) { |
synchronized(this) { |
116 |
return currentState; |
return currentState; |
117 |
} |
} |
118 |
} |
} |
119 |
|
|
120 |
|
public String getName() { |
121 |
|
return name; |
122 |
|
} |
123 |
|
|
124 |
|
public String getStateName() { |
125 |
|
return getState().getName(); |
126 |
|
} |
127 |
|
|
128 |
|
public int getThreshold() { |
129 |
|
return closed.getThreshold(); |
130 |
|
} |
131 |
|
|
132 |
|
public int getTimeout() { |
133 |
|
return (int)open.getTimeout(); |
134 |
|
} |
135 |
|
|
136 |
|
public int getFailureCount() { |
137 |
|
if (getState() == closed) { |
138 |
|
return closed.getFailureCount(); |
139 |
|
} else { |
140 |
|
return -1; |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
public long getElapsed() { |
145 |
|
if (getState() == open) { |
146 |
|
return open.getElapsed(); |
147 |
|
} else { |
148 |
|
return -1; |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
public void setNotifier(Notifier notifier) { |
153 |
|
this.notifier = notifier; |
154 |
|
} |
155 |
|
|
156 |
|
public String getNotifierName() { |
157 |
|
return notifier.getClass().getName(); |
158 |
|
} |
159 |
|
|
160 |
} |
} |