/[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

CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java revision 452 by torben, Tue Oct 20 10:47:36 2009 UTC CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java revision 871 by torben, Mon Jun 21 17:16:46 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.curcuitbreaker;  package dk.thoerup.circuitbreaker;
2    
3    
4  import dk.thoerup.curcuitbreaker.notification.Notifier;  import dk.thoerup.circuitbreaker.notification.Notifier;
5  import dk.thoerup.curcuitbreaker.notification.NullNotifier;  import dk.thoerup.circuitbreaker.notification.NullNotifier;
6    
7  /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java  /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java
8   *   *
# Line 58  public class CircuitBreaker{ Line 58  public class CircuitBreaker{
58                  open.setTimeout(timeoutMS);                  open.setTimeout(timeoutMS);
59                                    
60                  this.name = name;                  this.name = name;
61                            
62                  reset();                  //set correct intial state
63                    internalReset();
64          }          }
65                    
66                    
# Line 81  public class CircuitBreaker{ Line 82  public class CircuitBreaker{
82      }      }
83            
84      public void tripBreaker() {      public void tripBreaker() {
85          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerTripped);
86        }
87        
88            //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
89        public void retripBreaker() {
90            commonTripBreaker(Notifier.Event.BreakerRetripped);
91        }
92        
93        private void commonTripBreaker(Notifier.Event event) {
94            synchronized(this) {
95                  if (currentState != open) { // TODO:Is this conditional necessary ??                  if (currentState != open) { // TODO:Is this conditional necessary ??
96                          open.trip();                          open.trip();
97                          currentState = open;                          currentState = open;
98                                    
99                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);                          notifier.sendNotification(name, event);
100                  }                  }
101          }              }      
102      }      }
103        
104      public void attemptReset() {      public void attemptReset() {
105          synchronized(this) {          synchronized(this) {
106                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
# Line 104  public class CircuitBreaker{ Line 114  public class CircuitBreaker{
114      public void reset() {      public void reset() {
115          synchronized(this) {          synchronized(this) {
116                  if (currentState != closed) { // TODO: Is this conditional necessary ??                  if (currentState != closed) { // TODO: Is this conditional necessary ??
117                          currentState = closed;                          internalReset();
118                          notifier.sendNotification(name, Notifier.Event.BreakerReset);                          notifier.sendNotification(name, Notifier.Event.BreakerReset);
119                  }                  }
120          }          }
121      }      }
122            
123        //This one actually sets the correct closed/reset state
124        private void internalReset() {
125                    closed.resetFailureCount();
126                    currentState = closed;          
127        }
128        
129            
130      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
131          synchronized(this) {          synchronized(this) {

Legend:
Removed from v.452  
changed lines
  Added in v.871

  ViewVC Help
Powered by ViewVC 1.1.20