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

revision 393 by torben, Mon Oct 5 19:44:40 2009 UTC revision 424 by torben, Thu Oct 8 20:39:39 2009 UTC
# Line 2  package dk.thoerup.curcuitbreaker; Line 2  package dk.thoerup.curcuitbreaker;
2    
3  import java.util.logging.Logger;  import java.util.logging.Logger;
4    
5    import dk.thoerup.curcuitbreaker.notification.Notifier;
6    import dk.thoerup.curcuitbreaker.notification.NullNotifier;
7    
8    /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java
9     *
10     *  example of how it can be used
11    
12  /* example of how it can be used   private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000);
13  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14                  class TestInvocation implements CircuitInvocation {                  class TestInvocation implements CircuitInvocation {
15                          String url;                          String url;
16                          public TestInvocation(String url) {                          public TestInvocation(String url) {
# Line 43  public class CircuitBreaker{ Line 48  public class CircuitBreaker{
48                    
49          private CircuitBreakerState currentState;          private CircuitBreakerState currentState;
50                    
51          private OpenState open = new OpenState();          private final OpenState open = new OpenState();
52          private HalfOpenState halfOpen = new HalfOpenState();          private final HalfOpenState halfOpen = new HalfOpenState();
53          private ClosedState closed = new ClosedState();          private final ClosedState closed = new ClosedState();
54                    
55          private String name;          private String name;
56                    
57            private Notifier notifier = new NullNotifier();
58            
59          public CircuitBreaker(String name, int threshold, long timeoutMS) {          public CircuitBreaker(String name, int threshold, long timeoutMS) {
60                  closed.setThreshold(threshold);                  closed.setThreshold(threshold);
61                  open.setTimeout(timeoutMS);                  open.setTimeout(timeoutMS);
# Line 78  public class CircuitBreaker{ Line 85  public class CircuitBreaker{
85            
86      public void tripBreaker() {      public void tripBreaker() {
87          synchronized(this) {          synchronized(this) {
                 currentState = open;  
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      public void attemptReset() {      public void attemptReset() {
96          synchronized(this) {          synchronized(this) {
97                  currentState = halfOpen;                  currentState = halfOpen;
98                                    notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
                 logger.warning("Circuitbreaker attemptReset - " + name);  
99          }          }
100            
101      }      }
102            
103      public void reset() {      public void reset() {
104          synchronized(this) {          synchronized(this) {
105                  currentState = closed;                  currentState = closed;
106                                    notifier.sendNotification(name, Notifier.Event.BreakerReset);
                 logger.warning("Circuitbreaker reset - " + name);  
107          }          }
108      }      }
109            
110        
111      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
112          synchronized(this) {          synchronized(this) {
113                  return currentState;                  return currentState;
114          }          }
115      }      }
116        
117        public String getName() {
118            return name;
119        }
120        
121        public String getStateName() {
122            return getState().getName();
123        }
124        
125        public int getThreshold() {
126            return closed.getThreshold();
127        }
128        
129        public int getTimeout() {
130            return (int)open.getTimeout();
131        }
132        
133        public int getFailureCount() {
134            if (getState() == closed) {
135                    return closed.getFailureCount();
136            } else {
137                    return -1;
138            }
139        }
140        
141        public long getElapsed() {
142            if (getState() == open) {
143                    return open.getElapsed();
144            } else {
145                    return -1;
146            }
147        }
148        
149        public void setNotifier(Notifier notifier) {
150            this.notifier = notifier;
151        }
152        
153        public String getNotifierName() {
154            return notifier.getClass().getName();
155        }
156    
157  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20