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

Diff of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 409 by torben, Wed Oct 7 07:07:00 2009 UTC revision 447 by torben, Mon Oct 19 13:57:42 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.curcuitbreaker;  package dk.thoerup.curcuitbreaker;
2    
 import java.util.logging.Logger;  
3    
4  import dk.thoerup.curcuitbreaker.notification.Notifier;  import dk.thoerup.curcuitbreaker.notification.Notifier;
5  import dk.thoerup.curcuitbreaker.notification.NullNotifier;  import dk.thoerup.curcuitbreaker.notification.NullNotifier;
# Line 43  import dk.thoerup.curcuitbreaker.notific Line 42  import dk.thoerup.curcuitbreaker.notific
42    
43    
44  public class CircuitBreaker{  public class CircuitBreaker{
         Logger logger = Logger.getLogger(CircuitBreaker.class.getName());  
   
45                    
46          private CircuitBreakerState currentState;          private volatile CircuitBreakerState currentState;
47                    
48          private OpenState open = new OpenState();          private final OpenState open = new OpenState();
49          private HalfOpenState halfOpen = new HalfOpenState();          private final HalfOpenState halfOpen = new HalfOpenState();
50          private ClosedState closed = new ClosedState();          private final ClosedState closed = new ClosedState();
51                    
52          private String name;          private String name;
53                    
# Line 85  public class CircuitBreaker{ Line 82  public class CircuitBreaker{
82            
83      public void tripBreaker() {      public void tripBreaker() {
84          synchronized(this) {          synchronized(this) {
85                  open.trip();                  if (currentState != open) { // TODO:Is this conditional necessary ??
86                  currentState = open;                          open.trip();
87                            currentState = open;
88                                    
89                  notifier.sendNotification(name, Notifier.Event.BreakerTripped);                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);
90                    }
91          }              }    
92      }      }
93            
94      public void attemptReset() {      public void attemptReset() {
95          synchronized(this) {          synchronized(this) {
96                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
97                  notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          currentState = halfOpen;
98                            notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
99                    }
100          }          }
101                    
102      }      }
103            
104      public void reset() {      public void reset() {
105          synchronized(this) {          synchronized(this) {
106                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
107                  notifier.sendNotification(name, Notifier.Event.BreakerReset);                          currentState = closed;
108                            notifier.sendNotification(name, Notifier.Event.BreakerReset);
109                    }
110          }          }
111      }      }
112            
# Line 114  public class CircuitBreaker{ Line 117  public class CircuitBreaker{
117          }          }
118      }      }
119            
120        public boolean isClosed() {
121            return (getState() == closed);
122        }
123        
124        public boolean isOpen() {
125            return (getState() == open);
126        }
127        
128      public String getName() {      public String getName() {
129          return name;          return name;
130      }      }
# Line 126  public class CircuitBreaker{ Line 137  public class CircuitBreaker{
137          return closed.getThreshold();          return closed.getThreshold();
138      }      }
139            
140        public int getTimeout() {
141            return (int)open.getTimeout();
142        }
143        
144      public int getFailureCount() {      public int getFailureCount() {
145          if (getState() == closed) {          if (getState() == closed) {
146                  return closed.getFailureCount();                  return closed.getFailureCount();
# Line 147  public class CircuitBreaker{ Line 162  public class CircuitBreaker{
162      }      }
163            
164      public String getNotifierName() {      public String getNotifierName() {
165          return notifier.toString();          return notifier.getClass().getName();
166      }      }
167    
168  }  }

Legend:
Removed from v.409  
changed lines
  Added in v.447

  ViewVC Help
Powered by ViewVC 1.1.20