1 |
torben |
393 |
package dk.thoerup.curcuitbreaker; |
2 |
|
|
|
3 |
|
|
import java.util.concurrent.atomic.*; |
4 |
|
|
|
5 |
|
|
|
6 |
|
|
|
7 |
torben |
424 |
public final class ClosedState implements CircuitBreakerState { |
8 |
torben |
393 |
|
9 |
torben |
424 |
final AtomicInteger failureCount = new AtomicInteger(0); |
10 |
|
|
final AtomicInteger failureThreshold = new AtomicInteger(0); |
11 |
torben |
393 |
|
12 |
|
|
public void setThreshold(int threshold) { |
13 |
|
|
failureThreshold.set( threshold ); |
14 |
|
|
} |
15 |
|
|
|
16 |
torben |
450 |
public void preInvoke(CircuitBreaker circuitBreaker) throws Exception |
17 |
torben |
393 |
{ |
18 |
|
|
// NO OP |
19 |
|
|
} |
20 |
|
|
|
21 |
torben |
450 |
public void postInvoke(CircuitBreaker circuitBreaker) throws Exception |
22 |
torben |
393 |
{ |
23 |
|
|
resetFailureCount(); |
24 |
|
|
} |
25 |
|
|
|
26 |
torben |
450 |
public void onError(CircuitBreaker circuitBreaker, Exception t) throws Exception |
27 |
torben |
393 |
{ |
28 |
|
|
int currentCount = failureCount.incrementAndGet(); |
29 |
|
|
int threshold = failureThreshold.get(); |
30 |
|
|
if(currentCount >= threshold) |
31 |
|
|
circuitBreaker.tripBreaker(); |
32 |
|
|
} |
33 |
|
|
|
34 |
torben |
456 |
public void resetFailureCount() { |
35 |
torben |
393 |
failureCount.set(0); |
36 |
|
|
} |
37 |
torben |
397 |
|
38 |
|
|
public String getName() { |
39 |
|
|
return "Closed"; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
public int getFailureCount() { |
43 |
|
|
return failureCount.get(); |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
public int getThreshold() { |
47 |
|
|
return failureThreshold.get(); |
48 |
|
|
} |
49 |
torben |
393 |
} |