/[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 1306 by torben, Tue Apr 19 15:22:09 2011 UTC
# Line 1  Line 1 
1  package dk.thoerup.curcuitbreaker;  package dk.thoerup.circuitbreaker;
2    
3    
4  import dk.thoerup.curcuitbreaker.notification.Notifier;  import java.util.concurrent.ExecutorService;
5  import dk.thoerup.curcuitbreaker.notification.NullNotifier;  import java.util.concurrent.Executors;
6    
7    import dk.thoerup.circuitbreaker.config.BreakerConfig;
8    import dk.thoerup.circuitbreaker.config.StaticConfig;
9    import dk.thoerup.circuitbreaker.notification.NotiferHelper;
10    import dk.thoerup.circuitbreaker.notification.Notifier;
11    import dk.thoerup.circuitbreaker.notification.NullNotifier;
12    
13  /* 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
14   *   *
# Line 51  public class CircuitBreaker{ Line 57  public class CircuitBreaker{
57                    
58          private String name;          private String name;
59                    
60            private ExecutorService executor = null;        
61          private Notifier notifier = new NullNotifier();          private Notifier notifier = new NullNotifier();
62                    
63          public CircuitBreaker(String name, int threshold, long timeoutMS) {          public CircuitBreaker(String name, int threshold, int timeoutMS) {
64                  closed.setThreshold(threshold);                  this(name, new StaticConfig(threshold, timeoutMS) );
65                  open.setTimeout(timeoutMS);          }
66            
67            public CircuitBreaker(String name, BreakerConfig config) {
68                    closed.setThreshold(config);
69                    open.setTimeout(config);
70                                    
71                  this.name = name;                  this.name = name;
72                            
73                  reset();                  //set correct intial state
74                    internalReset();
75            }
76            
77            public synchronized void shutdown() {
78                    if (executor != null) {
79                            executor.shutdown();
80                    }
81          }          }
82                    
83                    
# Line 81  public class CircuitBreaker{ Line 99  public class CircuitBreaker{
99      }      }
100            
101      public void tripBreaker() {      public void tripBreaker() {
102          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerTripped);
103        }
104        
105            //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
106        public void retripBreaker() {
107            commonTripBreaker(Notifier.Event.BreakerRetripped);
108        }
109        
110        private void commonTripBreaker(Notifier.Event event) {
111            synchronized(this) {
112                  if (currentState != open) { // TODO:Is this conditional necessary ??                  if (currentState != open) { // TODO:Is this conditional necessary ??
113                          open.trip();                          open.trip();
114                          currentState = open;                          currentState = open;
115                                    
116                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);                          notifier.sendNotification(this, event);
117                  }                  }
118          }              }      
119      }      }
120        
121      public void attemptReset() {      public void attemptReset() {
122          synchronized(this) {          synchronized(this) {
123                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
124                          currentState = halfOpen;                          currentState = halfOpen;
125                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset);
126                  }                  }
127          }          }
128                    
# Line 104  public class CircuitBreaker{ Line 131  public class CircuitBreaker{
131      public void reset() {      public void reset() {
132          synchronized(this) {          synchronized(this) {
133                  if (currentState != closed) { // TODO: Is this conditional necessary ??                  if (currentState != closed) { // TODO: Is this conditional necessary ??
134                          currentState = closed;                          internalReset();
135                          notifier.sendNotification(name, Notifier.Event.BreakerReset);                          notifier.sendNotification(this, Notifier.Event.BreakerReset);
136                  }                  }
137          }          }
138      }      }
139            
140        //This one actually sets the correct closed/reset state
141        private void internalReset() {
142                    closed.resetFailureCount();
143                    currentState = closed;          
144        }
145        
146            
147      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
148          synchronized(this) {          synchronized(this) {
# Line 161  public class CircuitBreaker{ Line 194  public class CircuitBreaker{
194          this.notifier = notifier;          this.notifier = notifier;
195      }      }
196            
197      public String getNotifierName() {      public String getNotifierName() {
198          return notifier.getClass().getName();          return NotiferHelper.getName(notifier);
199        }
200        
201        public synchronized ExecutorService getExecutor() {
202            
203            if (executor == null) {
204                    executor = Executors.newFixedThreadPool(1);
205            }
206            
207            return executor;
208            
209      }      }
210    
211  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20