1 |
package dk.thoerup.curcuitbreaker; |
2 |
|
3 |
import java.util.concurrent.atomic.*; |
4 |
|
5 |
|
6 |
|
7 |
public class ClosedState implements CircuitBreakerState { |
8 |
|
9 |
AtomicInteger failureCount = new AtomicInteger(0); |
10 |
AtomicInteger failureThreshold = new AtomicInteger(0); |
11 |
|
12 |
public void setThreshold(int threshold) { |
13 |
failureThreshold.set( threshold ); |
14 |
} |
15 |
|
16 |
public void preInvoke(CircuitBreaker circuitBreaker) throws Throwable |
17 |
{ |
18 |
// NO OP |
19 |
} |
20 |
|
21 |
public void postInvoke(CircuitBreaker circuitBreaker) throws Throwable |
22 |
{ |
23 |
resetFailureCount(); |
24 |
} |
25 |
|
26 |
public void onError(CircuitBreaker circuitBreaker, Throwable t) throws Throwable |
27 |
{ |
28 |
int currentCount = failureCount.incrementAndGet(); |
29 |
int threshold = failureThreshold.get(); |
30 |
if(currentCount >= threshold) |
31 |
circuitBreaker.tripBreaker(); |
32 |
} |
33 |
|
34 |
private void resetFailureCount() { |
35 |
failureCount.set(0); |
36 |
} |
37 |
|
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 |
} |