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

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

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

CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java revision 395 by torben, Mon Oct 5 19:54:25 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  import java.util.logging.Logger;  
4    import dk.thoerup.circuitbreaker.notification.Notifier;
5    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 29  import java.util.logging.Logger; Line 31  import java.util.logging.Logger;
31                  try {                  try {
32                          String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));                          String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));
33                          response.getWriter().print(s);                          response.getWriter().print(s);
34                  } catch (Throwable e) {                  } catch (Exception e) {
35                          logger.warning( e.getMessage() );                          logger.warning( e.getMessage() );
36                          response.sendError(500);                          response.sendError(500);
37                          return;                          return;
# Line 40  import java.util.logging.Logger; Line 42  import java.util.logging.Logger;
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                    
54            private Notifier notifier = new NullNotifier();
55            
56          public CircuitBreaker(String name, int threshold, long timeoutMS) {          public CircuitBreaker(String name, int threshold, long timeoutMS) {
57                  closed.setThreshold(threshold);                  closed.setThreshold(threshold);
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                    
67      public Object invoke(CircuitInvocation invocation) throws Throwable      public Object invoke(CircuitInvocation invocation) throws Exception
68      {      {
69          Object result = null;          Object result = null;
70          try          try
# Line 70  public class CircuitBreaker{ Line 73  public class CircuitBreaker{
73              result = invocation.proceed();              result = invocation.proceed();
74              getState().postInvoke(this);              getState().postInvoke(this);
75          }          }
76          catch(Throwable t)          catch(Exception e)
77          {          {
78              getState().onError(this, t);              getState().onError(this, e);
79              throw t;              throw e;
80          }          }
81          return result;          return result;
82      }      }
83            
84      public void tripBreaker() {      public void tripBreaker() {
85          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerTripped);
86                  currentState = open;      }
                 open.trip();  
                   
                 logger.warning("Circuitbreaker tripBreaker - " + name);  
         }  
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 ??
96                            open.trip();
97                            currentState = open;
98                    
99                            notifier.sendNotification(name, event);
100                    }
101            }      
102        }
103    
104      public void attemptReset() {      public void attemptReset() {
105          synchronized(this) {          synchronized(this) {
106                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
107                                            currentState = halfOpen;
108                  logger.warning("Circuitbreaker attemptReset - " + name);                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
109                    }
110          }          }
111            
112      }      }
113            
114      public void reset() {      public void reset() {
115          synchronized(this) {          synchronized(this) {
116                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
117                                            internalReset();
118                  logger.warning("Circuitbreaker reset - " + name);                          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) {
132                  return currentState;                  return currentState;
133          }          }
134      }      }
135        
136        public boolean isClosed() {
137            return (getState() == closed);
138        }
139        
140        public boolean isOpen() {
141            return (getState() == open);
142        }
143        
144        public String getName() {
145            return name;
146        }
147        
148        public String getStateName() {
149            return getState().getName();
150        }
151        
152        public int getThreshold() {
153            return closed.getThreshold();
154        }
155        
156        public int getTimeout() {
157            return (int)open.getTimeout();
158        }
159        
160        public int getFailureCount() {
161            if (getState() == closed) {
162                    return closed.getFailureCount();
163            } else {
164                    return -1;
165            }
166        }
167        
168        public long getElapsed() {
169            if (getState() == open) {
170                    return open.getElapsed();
171            } else {
172                    return -1;
173            }
174        }
175        
176        public void setNotifier(Notifier notifier) {
177            this.notifier = notifier;
178        }
179        
180        public String getNotifierName() {
181            return notifier.getClass().getName();
182        }
183    
184  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20