/[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 450 - (hide annotations) (download)
Tue Oct 20 10:26:50 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/curcuitbreaker/ClosedState.java
File size: 1114 byte(s)
Narrow thrown from Throwable to Exception,

Added a notifier that prints to system.out
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     private void resetFailureCount() {
35     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 }

  ViewVC Help
Powered by ViewVC 1.1.20