/[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 409 by torben, Wed Oct 7 07:07:00 2009 UTC miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java revision 2569 by torben, Tue Jun 9 08:55:10 2015 UTC
# Line 1  Line 1 
1  package dk.thoerup.curcuitbreaker;  package dk.thoerup.circuitbreaker;
2    
 import java.util.logging.Logger;  
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    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 32  import dk.thoerup.curcuitbreaker.notific Line 39  import dk.thoerup.curcuitbreaker.notific
39                  try {                  try {
40                          String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));                          String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));
41                          response.getWriter().print(s);                          response.getWriter().print(s);
42                  } catch (Throwable e) {                  } catch (Exception e) {
43                          logger.warning( e.getMessage() );                          logger.warning( e.getMessage() );
44                          response.sendError(500);                          response.sendError(500);
45                          return;                          return;
# Line 43  import dk.thoerup.curcuitbreaker.notific Line 50  import dk.thoerup.curcuitbreaker.notific
50    
51    
52  public class CircuitBreaker{  public class CircuitBreaker{
         Logger logger = Logger.getLogger(CircuitBreaker.class.getName());  
   
53                    
54          private CircuitBreakerState currentState;          private volatile CircuitBreakerState currentState;
55                    
56          private OpenState open = new OpenState();          private final OpenState open = new OpenState();
57          private HalfOpenState halfOpen = new HalfOpenState();          private final HalfOpenState halfOpen = new HalfOpenState();
58          private ClosedState closed = new ClosedState();          private final ClosedState closed = new ClosedState();
59                    
60          private String name;          private String name;
61                    
62            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                            
77                  reset();                  //set correct intial state
78                    internalReset();
79          }          }
80                    
81            public synchronized void shutdown() {
82                    if (executor != null) {
83                            executor.shutdown();
84                    }
85            }
86                    
87      public Object invoke(CircuitInvocation invocation) throws Throwable          
88        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 75  public class CircuitBreaker{ Line 96  public class CircuitBreaker{
96              result = invocation.proceed();              result = invocation.proceed();
97              getState().postInvoke(this);              getState().postInvoke(this);
98          }          }
99          catch(Throwable t)          catch(Exception e)
100          {          {              
101              getState().onError(this, t);                          if (e instanceof CircuitBreakerException) {
102              throw t;                                  stats.addStatistics(Event.InvocationBlocked);
103                            } else {
104                                    stats.addStatistics(Event.InvocationFailure);
105                            }
106                    
107                getState().onError(this, e);
108                throw e;
109          }          }
110          return result;          return result;
111      }      }
112            
113      public void tripBreaker() {      public void tripBreaker() {
114          synchronized(this) {          commonTripBreaker(Event.BreakerTripped);
                 open.trip();  
                 currentState = open;  
                   
                 notifier.sendNotification(name, Notifier.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
118        public void retripBreaker() {
119            commonTripBreaker(Event.BreakerRetripped);
120        }
121        
122        private void commonTripBreaker(Event event) {
123            synchronized(this) {
124                    if (currentState != open) { // TODO:Is this conditional necessary ??
125                            open.trip();
126                            currentState = open;
127                    
128                            notifier.sendNotification(this, event);
129                            stats.addStatistics(event);
130                    }
131            }      
132        }
133    
134      public void attemptReset() {      public void attemptReset() {
135          synchronized(this) {          synchronized(this) {
136                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
137                  notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          currentState = halfOpen;
138                            notifier.sendNotification(this, Event.BreakerAttemptReset);
139                            stats.addStatistics(Event.BreakerAttemptReset);
140                    }
141          }          }
142                    
143      }      }
144            
145      public void reset() {      public void reset() {
146          synchronized(this) {          synchronized(this) {
147                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
148                  notifier.sendNotification(name, Notifier.Event.BreakerReset);                          internalReset();
149                            notifier.sendNotification(this, Event.BreakerReset);
150                            stats.addStatistics(Event.BreakerReset);
151                    }
152          }          }
153      }      }
154            
155        //This one actually sets the correct closed/reset state
156        private void internalReset() {
157                    closed.resetFailureCount();
158                    currentState = closed;          
159        }
160        
161            
162      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
163          synchronized(this) {          synchronized(this) {
# Line 114  public class CircuitBreaker{ Line 165  public class CircuitBreaker{
165          }          }
166      }      }
167            
168        public boolean isClosed() {
169            return (getState() == closed);
170        }
171        
172        public boolean isOpen() {
173            return (getState() == open);
174        }
175        
176      public String getName() {      public String getName() {
177          return name;          return name;
178      }      }
# Line 126  public class CircuitBreaker{ Line 185  public class CircuitBreaker{
185          return closed.getThreshold();          return closed.getThreshold();
186      }      }
187            
188        public int getTimeout() {
189            return (int)open.getTimeout();
190        }
191        
192      public int getFailureCount() {      public int getFailureCount() {
193          if (getState() == closed) {          if (getState() == closed) {
194                  return closed.getFailureCount();                  return closed.getFailureCount();
# Line 146  public class CircuitBreaker{ Line 209  public class CircuitBreaker{
209          this.notifier = notifier;          this.notifier = notifier;
210      }      }
211            
212      public String getNotifierName() {      public String getNotifierName() {
213          return notifier.toString();          return NotiferHelper.getName(notifier);
214        }
215        
216        public void setStatistics(Statistics newStats) {
217            this.stats = newStats;
218        }
219        
220        public Statistics getStatistics() {
221            return this.stats;
222        }
223        
224        public synchronized ExecutorService getExecutor() {
225            
226            if (executor == null) {
227                    executor = Executors.newFixedThreadPool(1);
228            }
229            
230            return executor;
231            
232      }      }
233    
234  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20