/[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 393 by torben, Mon Oct 5 19:44:40 2009 UTC CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java revision 625 by torben, Mon Mar 8 10:00:55 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.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
8     *
9     *  example of how it can be used
10    
11  /* example of how it can be used   private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000);
12  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13                  class TestInvocation implements CircuitInvocation {                  class TestInvocation implements CircuitInvocation {
14                          String url;                          String url;
15                          public TestInvocation(String url) {                          public TestInvocation(String url) {
# Line 27  protected void doGet(HttpServletRequest Line 31  protected void doGet(HttpServletRequest
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 38  protected void doGet(HttpServletRequest Line 42  protected void doGet(HttpServletRequest
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();                  // Initial state is open - like a reset() was called initially
63                    closed.resetFailureCount();
64                    currentState = closed;
65          }          }
66                    
67                    
68      public Object invoke(CircuitInvocation invocation) throws Throwable      public Object invoke(CircuitInvocation invocation) throws Exception
69      {      {
70          Object result = null;          Object result = null;
71          try          try
# Line 68  public class CircuitBreaker{ Line 74  public class CircuitBreaker{
74              result = invocation.proceed();              result = invocation.proceed();
75              getState().postInvoke(this);              getState().postInvoke(this);
76          }          }
77          catch(Throwable t)          catch(Exception e)
78          {          {
79              getState().onError(this, t);              getState().onError(this, e);
80              throw t;              throw e;
81          }          }
82          return result;          return result;
83      }      }
84            
85      public void tripBreaker() {      public void tripBreaker() {
86          synchronized(this) {          synchronized(this) {
87                  currentState = open;                  if (currentState != open) { // TODO:Is this conditional necessary ??
88                  open.trip();                          open.trip();
89                            currentState = open;
90                                    
91                  logger.warning("Circuitbreaker tripBreaker - " + name);                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);
92          }                  }
93                }    
94      }      }
95            
96      public void attemptReset() {      public void attemptReset() {
97          synchronized(this) {          synchronized(this) {
98                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
99                                            currentState = halfOpen;
100                  logger.warning("Circuitbreaker attemptReset - " + name);                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
101                    }
102          }          }
103            
104      }      }
105            
106      public void reset() {      public void reset() {
107          synchronized(this) {          synchronized(this) {
108                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
109                                            closed.resetFailureCount();
110                  logger.warning("Circuitbreaker reset - " + name);                          currentState = closed;
111                            notifier.sendNotification(name, Notifier.Event.BreakerReset);
112                    }
113          }          }
114      }      }
115            
116        
117      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
118          synchronized(this) {          synchronized(this) {
119                  return currentState;                  return currentState;
120          }          }
121      }      }
122        
123        public boolean isClosed() {
124            return (getState() == closed);
125        }
126        
127        public boolean isOpen() {
128            return (getState() == open);
129        }
130        
131        public String getName() {
132            return name;
133        }
134        
135        public String getStateName() {
136            return getState().getName();
137        }
138        
139        public int getThreshold() {
140            return closed.getThreshold();
141        }
142        
143        public int getTimeout() {
144            return (int)open.getTimeout();
145        }
146        
147        public int getFailureCount() {
148            if (getState() == closed) {
149                    return closed.getFailureCount();
150            } else {
151                    return -1;
152            }
153        }
154        
155        public long getElapsed() {
156            if (getState() == open) {
157                    return open.getElapsed();
158            } else {
159                    return -1;
160            }
161        }
162        
163        public void setNotifier(Notifier notifier) {
164            this.notifier = notifier;
165        }
166        
167        public String getNotifierName() {
168            return notifier.getClass().getName();
169        }
170    
171  }  }

Legend:
Removed from v.393  
changed lines
  Added in v.625

  ViewVC Help
Powered by ViewVC 1.1.20