/[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/circuitbreaker/CircuitBreaker.java revision 1148 by torben, Fri Oct 1 11:40:30 2010 UTC miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java revision 2572 by torben, Tue Jun 9 09:28:14 2015 UTC
# Line 4  package dk.thoerup.circuitbreaker; Line 4  package dk.thoerup.circuitbreaker;
4  import java.util.concurrent.ExecutorService;  import java.util.concurrent.ExecutorService;
5  import java.util.concurrent.Executors;  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;  import dk.thoerup.circuitbreaker.notification.Notifier;
11  import dk.thoerup.circuitbreaker.notification.NullNotifier;  import dk.thoerup.circuitbreaker.notification.NullNotifier;
12    import dk.thoerup.circuitbreaker.statistics.NullStatistics;
13    import dk.thoerup.circuitbreaker.statistics.Statistics;
14    
15  /* 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
16   *   *
# Line 56  public class CircuitBreaker{ Line 61  public class CircuitBreaker{
61                    
62          private ExecutorService executor = null;                  private ExecutorService executor = null;        
63          private Notifier notifier = new NullNotifier();          private Notifier notifier = new NullNotifier();
64            private Statistics stats = new NullStatistics();
65                    
66          public CircuitBreaker(String name, int threshold, long timeoutMS) {          @Deprecated
67                  closed.setThreshold(threshold);          public CircuitBreaker(String name, int threshold, int timeoutMS) {
68                  open.setTimeout(timeoutMS);                  this(name, new StaticConfig(threshold, timeoutMS) );
69            }
70            
71            public CircuitBreaker(String name, BreakerConfig config) {
72                    closed.setThreshold(config);
73                    open.setTimeout(config);
74                                    
75                  this.name = name;                  this.name = name;
76                    
# Line 76  public class CircuitBreaker{ Line 87  public class CircuitBreaker{
87                    
88      public Object invoke(CircuitInvocation invocation) throws Exception      public Object invoke(CircuitInvocation invocation) throws Exception
89      {      {
90            stats.addStatistics(Event.Invocation);
91            
92          Object result = null;          Object result = null;
93          try          try
94          {          {
# Line 84  public class CircuitBreaker{ Line 97  public class CircuitBreaker{
97              getState().postInvoke(this);              getState().postInvoke(this);
98          }          }
99          catch(Exception e)          catch(Exception e)
100          {          {              
101                            if (e instanceof CircuitBreakerException) {
102                                    stats.addStatistics(Event.InvocationBlocked);
103                            } else {
104                                    stats.addStatistics(Event.InvocationFailure);
105                            }
106                    
107              getState().onError(this, e);              getState().onError(this, e);
108              throw e;              throw e;
109          }          }
# Line 92  public class CircuitBreaker{ Line 111  public class CircuitBreaker{
111      }      }
112            
113      public void tripBreaker() {      public void tripBreaker() {
114          commonTripBreaker(Notifier.Event.BreakerTripped);          commonTripBreaker(Event.BreakerTripped);
115      }      }
116            
117          //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events          //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
118      public void retripBreaker() {      public void retripBreaker() {
119          commonTripBreaker(Notifier.Event.BreakerRetripped);          commonTripBreaker(Event.BreakerRetripped);
120      }      }
121            
122      private void commonTripBreaker(Notifier.Event event) {      private void commonTripBreaker(Event event) {
123          synchronized(this) {          synchronized(this) {
124                  if (currentState != open) { // TODO:Is this conditional necessary ??                  if (currentState != open) { // TODO:Is this conditional necessary ??
125                          open.trip();                          open.trip();
126                          currentState = open;                          currentState = open;
127                                    
128                          notifier.sendNotification(name, event);                          notifier.sendNotification(this, event);
129                            stats.addStatistics(event);
130                  }                  }
131          }                }      
132      }      }
# Line 115  public class CircuitBreaker{ Line 135  public class CircuitBreaker{
135          synchronized(this) {          synchronized(this) {
136                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
137                          currentState = halfOpen;                          currentState = halfOpen;
138                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          notifier.sendNotification(this, Event.BreakerAttemptReset);
139                            stats.addStatistics(Event.BreakerAttemptReset);
140                  }                  }
141          }          }
142                    
# Line 125  public class CircuitBreaker{ Line 146  public class CircuitBreaker{
146          synchronized(this) {          synchronized(this) {
147                  if (currentState != closed) { // TODO: Is this conditional necessary ??                  if (currentState != closed) { // TODO: Is this conditional necessary ??
148                          internalReset();                          internalReset();
149                          notifier.sendNotification(name, Notifier.Event.BreakerReset);                          notifier.sendNotification(this, Event.BreakerReset);
150                            stats.addStatistics(Event.BreakerReset);
151                  }                  }
152          }          }
153      }      }
# Line 184  public class CircuitBreaker{ Line 206  public class CircuitBreaker{
206      }      }
207            
208      public void setNotifier(Notifier notifier) {      public void setNotifier(Notifier notifier) {
209          this.notifier = notifier;          synchronized(this) {
210                    this.notifier = notifier;
211            }
212        }
213        
214        public String getNotifierName() {
215            return NotiferHelper.getName(notifier);
216        }
217        
218        public void setStatistics(Statistics newStats) {
219            this.stats = newStats;
220      }      }
221            
222      public String getNotifierName() {      public Statistics getStatistics() {
223          return notifier.getClass().getName();          return this.stats;
224      }      }
225            
226      public synchronized ExecutorService getExecutor() {      public synchronized ExecutorService getExecutor() {

Legend:
Removed from v.1148  
changed lines
  Added in v.2572

  ViewVC Help
Powered by ViewVC 1.1.20