/[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

revision 395 by torben, Mon Oct 5 19:54:25 2009 UTC revision 456 by torben, Tue Oct 20 20:42:36 2009 UTC
# Line 1  Line 1 
1  package dk.thoerup.curcuitbreaker;  package dk.thoerup.curcuitbreaker;
2    
3  import java.util.logging.Logger;  
4    import dk.thoerup.curcuitbreaker.notification.Notifier;
5    import dk.thoerup.curcuitbreaker.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);
# Line 61  public class CircuitBreaker{ Line 63  public class CircuitBreaker{
63          }          }
64                    
65                    
66      public Object invoke(CircuitInvocation invocation) throws Throwable      public Object invoke(CircuitInvocation invocation) throws Exception
67      {      {
68          Object result = null;          Object result = null;
69          try          try
# Line 70  public class CircuitBreaker{ Line 72  public class CircuitBreaker{
72              result = invocation.proceed();              result = invocation.proceed();
73              getState().postInvoke(this);              getState().postInvoke(this);
74          }          }
75          catch(Throwable t)          catch(Exception e)
76          {          {
77              getState().onError(this, t);              getState().onError(this, e);
78              throw t;              throw e;
79          }          }
80          return result;          return result;
81      }      }
82            
83      public void tripBreaker() {      public void tripBreaker() {
84          synchronized(this) {          synchronized(this) {
85                  currentState = open;                  if (currentState != open) { // TODO:Is this conditional necessary ??
86                  open.trip();                          open.trip();
87                            currentState = open;
88                                    
89                  logger.warning("Circuitbreaker tripBreaker - " + name);                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);
90          }                  }
91                }    
92      }      }
93            
94      public void attemptReset() {      public void attemptReset() {
95          synchronized(this) {          synchronized(this) {
96                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
97                                            currentState = halfOpen;
98                  logger.warning("Circuitbreaker attemptReset - " + name);                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
99                    }
100          }          }
101            
102      }      }
103            
104      public void reset() {      public void reset() {
105          synchronized(this) {          synchronized(this) {
106                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
107                                            closed.resetFailureCount();
108                  logger.warning("Circuitbreaker reset - " + name);                          currentState = closed;
109                            notifier.sendNotification(name, Notifier.Event.BreakerReset);
110                    }
111          }          }
112      }      }
113            
114        
115      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
116          synchronized(this) {          synchronized(this) {
117                  return currentState;                  return currentState;
118          }          }
119      }      }
120        
121        public boolean isClosed() {
122            return (getState() == closed);
123        }
124        
125        public boolean isOpen() {
126            return (getState() == open);
127        }
128        
129        public String getName() {
130            return name;
131        }
132        
133        public String getStateName() {
134            return getState().getName();
135        }
136        
137        public int getThreshold() {
138            return closed.getThreshold();
139        }
140        
141        public int getTimeout() {
142            return (int)open.getTimeout();
143        }
144        
145        public int getFailureCount() {
146            if (getState() == closed) {
147                    return closed.getFailureCount();
148            } else {
149                    return -1;
150            }
151        }
152        
153        public long getElapsed() {
154            if (getState() == open) {
155                    return open.getElapsed();
156            } else {
157                    return -1;
158            }
159        }
160        
161        public void setNotifier(Notifier notifier) {
162            this.notifier = notifier;
163        }
164        
165        public String getNotifierName() {
166            return notifier.getClass().getName();
167        }
168    
169  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20