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