/[projects]/miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/ClosedState.java
ViewVC logotype

Annotation of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/ClosedState.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1306 - (hide annotations) (download)
Tue Apr 19 15:22:09 2011 UTC (13 years, 1 month ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/ClosedState.java
File size: 1136 byte(s)
Switch to BreakerConfig method this way it is possible to control the breaker dynamically
1 torben 467 package dk.thoerup.circuitbreaker;
2 torben 393
3 torben 1306 import java.util.concurrent.atomic.AtomicInteger;
4 torben 393
5 torben 1306 import dk.thoerup.circuitbreaker.config.BreakerConfig;
6 torben 393
7    
8 torben 1306
9 torben 424 public final class ClosedState implements CircuitBreakerState {
10 torben 393
11 torben 424 final AtomicInteger failureCount = new AtomicInteger(0);
12 torben 393
13 torben 1306
14     BreakerConfig config;
15    
16     public void setThreshold(BreakerConfig config) {
17     this.config = config;
18 torben 393 }
19    
20 torben 450 public void preInvoke(CircuitBreaker circuitBreaker) throws Exception
21 torben 393 {
22     // NO OP
23     }
24    
25 torben 450 public void postInvoke(CircuitBreaker circuitBreaker) throws Exception
26 torben 393 {
27     resetFailureCount();
28     }
29    
30 torben 450 public void onError(CircuitBreaker circuitBreaker, Exception t) throws Exception
31 torben 393 {
32     int currentCount = failureCount.incrementAndGet();
33 torben 1306 int threshold = config.getTreshold();
34 torben 393 if(currentCount >= threshold)
35     circuitBreaker.tripBreaker();
36     }
37    
38 torben 456 public void resetFailureCount() {
39 torben 393 failureCount.set(0);
40     }
41 torben 397
42     public String getName() {
43     return "Closed";
44     }
45    
46     public int getFailureCount() {
47     return failureCount.get();
48     }
49    
50     public int getThreshold() {
51 torben 1306 return config.getTreshold();
52 torben 397 }
53 torben 393 }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20