1 |
package dk.thoerup.circuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
3 |
|
|
4 |
|
import java.util.concurrent.ExecutorService; |
5 |
|
import java.util.concurrent.Executors; |
6 |
|
|
7 |
import dk.thoerup.circuitbreaker.notification.Notifier; |
import dk.thoerup.circuitbreaker.notification.Notifier; |
8 |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
9 |
|
|
54 |
|
|
55 |
private String name; |
private String name; |
56 |
|
|
57 |
|
private ExecutorService executor = null; |
58 |
private Notifier notifier = new NullNotifier(); |
private Notifier notifier = new NullNotifier(); |
59 |
|
|
60 |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
63 |
|
|
64 |
this.name = name; |
this.name = name; |
65 |
|
|
66 |
// Initial state is open - like a reset() was called initially |
//set correct intial state |
67 |
closed.resetFailureCount(); |
internalReset(); |
68 |
currentState = closed; |
} |
69 |
|
|
70 |
|
public synchronized void shutdown() { |
71 |
|
if (executor != null) { |
72 |
|
executor.shutdown(); |
73 |
|
} |
74 |
} |
} |
75 |
|
|
76 |
|
|
92 |
} |
} |
93 |
|
|
94 |
public void tripBreaker() { |
public void tripBreaker() { |
95 |
synchronized(this) { |
commonTripBreaker(Notifier.Event.BreakerTripped); |
96 |
|
} |
97 |
|
|
98 |
|
//a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events |
99 |
|
public void retripBreaker() { |
100 |
|
commonTripBreaker(Notifier.Event.BreakerRetripped); |
101 |
|
} |
102 |
|
|
103 |
|
private void commonTripBreaker(Notifier.Event event) { |
104 |
|
synchronized(this) { |
105 |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
106 |
open.trip(); |
open.trip(); |
107 |
currentState = open; |
currentState = open; |
108 |
|
|
109 |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
notifier.sendNotification(this, event); |
110 |
} |
} |
111 |
} |
} |
112 |
} |
} |
113 |
|
|
114 |
public void attemptReset() { |
public void attemptReset() { |
115 |
synchronized(this) { |
synchronized(this) { |
116 |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
117 |
currentState = halfOpen; |
currentState = halfOpen; |
118 |
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
124 |
public void reset() { |
public void reset() { |
125 |
synchronized(this) { |
synchronized(this) { |
126 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
127 |
closed.resetFailureCount(); |
internalReset(); |
128 |
currentState = closed; |
notifier.sendNotification(this, Notifier.Event.BreakerReset); |
|
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
|
129 |
} |
} |
130 |
} |
} |
131 |
} |
} |
132 |
|
|
133 |
|
//This one actually sets the correct closed/reset state |
134 |
|
private void internalReset() { |
135 |
|
closed.resetFailureCount(); |
136 |
|
currentState = closed; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
private CircuitBreakerState getState() { |
private CircuitBreakerState getState() { |
141 |
synchronized(this) { |
synchronized(this) { |
190 |
public String getNotifierName() { |
public String getNotifierName() { |
191 |
return notifier.getClass().getName(); |
return notifier.getClass().getName(); |
192 |
} |
} |
193 |
|
|
194 |
|
public synchronized ExecutorService getExecutor() { |
195 |
|
|
196 |
|
if (executor == null) { |
197 |
|
executor = Executors.newFixedThreadPool(1); |
198 |
|
} |
199 |
|
|
200 |
|
return executor; |
201 |
|
|
202 |
|
} |
203 |
|
|
204 |
} |
} |