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.NotiferHelper; |
8 |
import dk.thoerup.circuitbreaker.notification.Notifier; |
import dk.thoerup.circuitbreaker.notification.Notifier; |
9 |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
import dk.thoerup.circuitbreaker.notification.NullNotifier; |
10 |
|
|
55 |
|
|
56 |
private String name; |
private String name; |
57 |
|
|
58 |
|
private ExecutorService executor = null; |
59 |
private Notifier notifier = new NullNotifier(); |
private Notifier notifier = new NullNotifier(); |
60 |
|
|
61 |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
public CircuitBreaker(String name, int threshold, long timeoutMS) { |
68 |
internalReset(); |
internalReset(); |
69 |
} |
} |
70 |
|
|
71 |
|
public synchronized void shutdown() { |
72 |
|
if (executor != null) { |
73 |
|
executor.shutdown(); |
74 |
|
} |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
public Object invoke(CircuitInvocation invocation) throws Exception |
public Object invoke(CircuitInvocation invocation) throws Exception |
79 |
{ |
{ |
93 |
} |
} |
94 |
|
|
95 |
public void tripBreaker() { |
public void tripBreaker() { |
96 |
synchronized(this) { |
commonTripBreaker(Notifier.Event.BreakerTripped); |
97 |
|
} |
98 |
|
|
99 |
|
//a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events |
100 |
|
public void retripBreaker() { |
101 |
|
commonTripBreaker(Notifier.Event.BreakerRetripped); |
102 |
|
} |
103 |
|
|
104 |
|
private void commonTripBreaker(Notifier.Event event) { |
105 |
|
synchronized(this) { |
106 |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
if (currentState != open) { // TODO:Is this conditional necessary ?? |
107 |
open.trip(); |
open.trip(); |
108 |
currentState = open; |
currentState = open; |
109 |
|
|
110 |
notifier.sendNotification(name, Notifier.Event.BreakerTripped); |
notifier.sendNotification(this, event); |
111 |
} |
} |
112 |
} |
} |
113 |
} |
} |
114 |
|
|
115 |
public void attemptReset() { |
public void attemptReset() { |
116 |
synchronized(this) { |
synchronized(this) { |
117 |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
if (currentState != halfOpen) { // TODO:Is this conditional necessary ?? |
118 |
currentState = halfOpen; |
currentState = halfOpen; |
119 |
notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset); |
notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset); |
120 |
} |
} |
121 |
} |
} |
122 |
|
|
126 |
synchronized(this) { |
synchronized(this) { |
127 |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
if (currentState != closed) { // TODO: Is this conditional necessary ?? |
128 |
internalReset(); |
internalReset(); |
129 |
notifier.sendNotification(name, Notifier.Event.BreakerReset); |
notifier.sendNotification(this, Notifier.Event.BreakerReset); |
130 |
} |
} |
131 |
} |
} |
132 |
} |
} |
188 |
this.notifier = notifier; |
this.notifier = notifier; |
189 |
} |
} |
190 |
|
|
191 |
public String getNotifierName() { |
public String getNotifierName() { |
192 |
return notifier.getClass().getName(); |
return NotiferHelper.getName(notifier); |
193 |
|
} |
194 |
|
|
195 |
|
public synchronized ExecutorService getExecutor() { |
196 |
|
|
197 |
|
if (executor == null) { |
198 |
|
executor = Executors.newFixedThreadPool(1); |
199 |
|
} |
200 |
|
|
201 |
|
return executor; |
202 |
|
|
203 |
} |
} |
204 |
|
|
205 |
} |
} |