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.config.BreakerConfig; |
8 |
|
import dk.thoerup.circuitbreaker.config.StaticConfig; |
9 |
|
import dk.thoerup.circuitbreaker.notification.NotiferHelper; |
10 |
import dk.thoerup.circuitbreaker.notification.Notifier; |
import dk.thoerup.circuitbreaker.notification.Notifier; |
11 |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
12 |
|
|
57 |
|
|
58 |
private String name; |
private String name; |
59 |
|
|
60 |
|
private ExecutorService executor = null; |
61 |
private Notifier notifier = new NullNotifier(); |
private Notifier notifier = new NullNotifier(); |
62 |
|
|
63 |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
@Deprecated |
64 |
closed.setThreshold(threshold); |
public CircuitBreaker(String name, int threshold, int timeoutMS) { |
65 |
open.setTimeout(timeoutMS); |
this(name, new StaticConfig(threshold, timeoutMS) ); |
66 |
|
} |
67 |
|
|
68 |
|
public CircuitBreaker(String name, BreakerConfig config) { |
69 |
|
closed.setThreshold(config); |
70 |
|
open.setTimeout(config); |
71 |
|
|
72 |
this.name = name; |
this.name = name; |
73 |
|
|
74 |
// Initial state is open - like a reset() was called initially |
//set correct intial state |
75 |
closed.resetFailureCount(); |
internalReset(); |
76 |
currentState = closed; |
} |
77 |
|
|
78 |
|
public synchronized void shutdown() { |
79 |
|
if (executor != null) { |
80 |
|
executor.shutdown(); |
81 |
|
} |
82 |
} |
} |
83 |
|
|
84 |
|
|
100 |
} |
} |
101 |
|
|
102 |
public void tripBreaker() { |
public void tripBreaker() { |
103 |
synchronized(this) { |
commonTripBreaker(Notifier.Event.BreakerTripped); |
104 |
|
} |
105 |
|
|
106 |
|
//a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events |
107 |
|
public void retripBreaker() { |
108 |
|
commonTripBreaker(Notifier.Event.BreakerRetripped); |
109 |
|
} |
110 |
|
|
111 |
|
private void commonTripBreaker(Notifier.Event event) { |
112 |
|
synchronized(this) { |
113 |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
114 |
open.trip(); |
open.trip(); |
115 |
currentState = open; |
currentState = open; |
116 |
|
|
117 |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
notifier.sendNotification(this, event); |
118 |
} |
} |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
122 |
public void attemptReset() { |
public void attemptReset() { |
123 |
synchronized(this) { |
synchronized(this) { |
124 |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
125 |
currentState = halfOpen; |
currentState = halfOpen; |
126 |
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); |
127 |
} |
} |
128 |
} |
} |
129 |
|
|
132 |
public void reset() { |
public void reset() { |
133 |
synchronized(this) { |
synchronized(this) { |
134 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
135 |
closed.resetFailureCount(); |
internalReset(); |
136 |
currentState = closed; |
notifier.sendNotification(this, Notifier.Event.BreakerReset); |
|
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
|
137 |
} |
} |
138 |
} |
} |
139 |
} |
} |
140 |
|
|
141 |
|
//This one actually sets the correct closed/reset state |
142 |
|
private void internalReset() { |
143 |
|
closed.resetFailureCount(); |
144 |
|
currentState = closed; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
private CircuitBreakerState getState() { |
private CircuitBreakerState getState() { |
149 |
synchronized(this) { |
synchronized(this) { |
195 |
this.notifier = notifier; |
this.notifier = notifier; |
196 |
} |
} |
197 |
|
|
198 |
public String getNotifierName() { |
public String getNotifierName() { |
199 |
return notifier.getClass().getName(); |
return NotiferHelper.getName(notifier); |
200 |
|
} |
201 |
|
|
202 |
|
public synchronized ExecutorService getExecutor() { |
203 |
|
|
204 |
|
if (executor == null) { |
205 |
|
executor = Executors.newFixedThreadPool(1); |
206 |
|
} |
207 |
|
|
208 |
|
return executor; |
209 |
|
|
210 |
} |
} |
211 |
|
|
212 |
} |
} |