/[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 412 by torben, Wed Oct 7 16:42:10 2009 UTC CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java revision 864 by torben, Sun Jun 20 21:54:53 2010 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 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 32  import dk.thoerup.curcuitbreaker.notific Line 31  import dk.thoerup.curcuitbreaker.notific
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 43  import dk.thoerup.curcuitbreaker.notific Line 42  import dk.thoerup.curcuitbreaker.notific
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                    
# Line 61  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                    
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 75  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) {          synchronized(this) {
86                  open.trip();                  if (currentState != open) { // TODO:Is this conditional necessary ??
87                  currentState = open;                          open.trip();
88                            currentState = open;
89                                    
90                  notifier.sendNotification(name, Notifier.Event.BreakerTripped);                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);
91                    }
92          }              }    
93      }      }
94            
95            //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
96        public void retripBreaker() {
97            synchronized(this) {
98                    if (currentState != open) { // TODO:Is this conditional necessary ??
99                            open.trip();
100                            currentState = open;
101                    
102                            notifier.sendNotification(name, Notifier.Event.BreakerRetripped);
103                    }
104            }    
105        }
106    
107      public void attemptReset() {      public void attemptReset() {
108          synchronized(this) {          synchronized(this) {
109                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
110                  notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          currentState = halfOpen;
111                            notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
112                    }
113          }          }
114                    
115      }      }
116            
117      public void reset() {      public void reset() {
118          synchronized(this) {          synchronized(this) {
119                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
120                  notifier.sendNotification(name, Notifier.Event.BreakerReset);                          internalReset();
121                            notifier.sendNotification(name, Notifier.Event.BreakerReset);
122                    }
123          }          }
124      }      }
125            
126        //This one actually sets the correct closed/reset state
127        private void internalReset() {
128                    closed.resetFailureCount();
129                    currentState = closed;          
130        }
131        
132            
133      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
134          synchronized(this) {          synchronized(this) {
# Line 114  public class CircuitBreaker{ Line 136  public class CircuitBreaker{
136          }          }
137      }      }
138            
139        public boolean isClosed() {
140            return (getState() == closed);
141        }
142        
143        public boolean isOpen() {
144            return (getState() == open);
145        }
146        
147      public String getName() {      public String getName() {
148          return name;          return name;
149      }      }
# Line 126  public class CircuitBreaker{ Line 156  public class CircuitBreaker{
156          return closed.getThreshold();          return closed.getThreshold();
157      }      }
158            
159        public int getTimeout() {
160            return (int)open.getTimeout();
161        }
162        
163      public int getFailureCount() {      public int getFailureCount() {
164          if (getState() == closed) {          if (getState() == closed) {
165                  return closed.getFailureCount();                  return closed.getFailureCount();

Legend:
Removed from v.412  
changed lines
  Added in v.864

  ViewVC Help
Powered by ViewVC 1.1.20