/[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 1289 by torben, Mon Apr 11 16:05:10 2011 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 java.util.concurrent.ExecutorService;
5  import dk.thoerup.curcuitbreaker.notification.NullNotifier;  import java.util.concurrent.Executors;
6    
7    import dk.thoerup.circuitbreaker.notification.NotiferHelper;
8    import dk.thoerup.circuitbreaker.notification.Notifier;
9    import dk.thoerup.circuitbreaker.notification.NullNotifier;
10    
11  /* 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
12   *   *
# Line 32  import dk.thoerup.curcuitbreaker.notific Line 35  import dk.thoerup.curcuitbreaker.notific
35                  try {                  try {
36                          String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));                          String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));
37                          response.getWriter().print(s);                          response.getWriter().print(s);
38                  } catch (Throwable e) {                  } catch (Exception e) {
39                          logger.warning( e.getMessage() );                          logger.warning( e.getMessage() );
40                          response.sendError(500);                          response.sendError(500);
41                          return;                          return;
# Line 43  import dk.thoerup.curcuitbreaker.notific Line 46  import dk.thoerup.curcuitbreaker.notific
46    
47    
48  public class CircuitBreaker{  public class CircuitBreaker{
         Logger logger = Logger.getLogger(CircuitBreaker.class.getName());  
   
49                    
50          private CircuitBreakerState currentState;          private volatile CircuitBreakerState currentState;
51                    
52          private OpenState open = new OpenState();          private final OpenState open = new OpenState();
53          private HalfOpenState halfOpen = new HalfOpenState();          private final HalfOpenState halfOpen = new HalfOpenState();
54          private ClosedState closed = new ClosedState();          private final ClosedState closed = new ClosedState();
55                    
56          private String name;          private String name;
57                    
58            private ExecutorService executor = null;        
59          private Notifier notifier = new NullNotifier();          private Notifier notifier = new NullNotifier();
60                    
61          public CircuitBreaker(String name, int threshold, long timeoutMS) {          public CircuitBreaker(String name, int threshold, long timeoutMS) {
# Line 61  public class CircuitBreaker{ Line 63  public class CircuitBreaker{
63                  open.setTimeout(timeoutMS);                  open.setTimeout(timeoutMS);
64                                    
65                  this.name = name;                  this.name = name;
66                            
67                  reset();                  //set correct intial state
68                    internalReset();
69            }
70            
71            public synchronized void shutdown() {
72                    if (executor != null) {
73                            executor.shutdown();
74                    }
75          }          }
76                    
77                    
78      public Object invoke(CircuitInvocation invocation) throws Throwable      public Object invoke(CircuitInvocation invocation) throws Exception
79      {      {
80          Object result = null;          Object result = null;
81          try          try
# Line 75  public class CircuitBreaker{ Line 84  public class CircuitBreaker{
84              result = invocation.proceed();              result = invocation.proceed();
85              getState().postInvoke(this);              getState().postInvoke(this);
86          }          }
87          catch(Throwable t)          catch(Exception e)
88          {          {
89              getState().onError(this, t);              getState().onError(this, e);
90              throw t;              throw e;
91          }          }
92          return result;          return result;
93      }      }
94            
95      public void tripBreaker() {      public void tripBreaker() {
96          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerTripped);
97                  open.trip();      }
98                  currentState = open;      
99                            //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
100                  notifier.sendNotification(name, Notifier.Event.BreakerTripped);      public void retripBreaker() {
101          }              commonTripBreaker(Notifier.Event.BreakerRetripped);
102      }      }
103            
104        private void commonTripBreaker(Notifier.Event event) {
105            synchronized(this) {
106                    if (currentState != open) { // TODO:Is this conditional necessary ??
107                            open.trip();
108                            currentState = open;
109                    
110                            notifier.sendNotification(this, event);
111                    }
112            }      
113        }
114    
115      public void attemptReset() {      public void attemptReset() {
116          synchronized(this) {          synchronized(this) {
117                  currentState = halfOpen;                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
118                  notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          currentState = halfOpen;
119                            notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset);
120                    }
121          }          }
122                    
123      }      }
124            
125      public void reset() {      public void reset() {
126          synchronized(this) {          synchronized(this) {
127                  currentState = closed;                  if (currentState != closed) { // TODO: Is this conditional necessary ??
128                  notifier.sendNotification(name, Notifier.Event.BreakerReset);                          internalReset();
129                            notifier.sendNotification(this, Notifier.Event.BreakerReset);
130                    }
131          }          }
132      }      }
133            
134        //This one actually sets the correct closed/reset state
135        private void internalReset() {
136                    closed.resetFailureCount();
137                    currentState = closed;          
138        }
139        
140            
141      private CircuitBreakerState getState() {      private CircuitBreakerState getState() {
142          synchronized(this) {          synchronized(this) {
# Line 114  public class CircuitBreaker{ Line 144  public class CircuitBreaker{
144          }          }
145      }      }
146            
147        public boolean isClosed() {
148            return (getState() == closed);
149        }
150        
151        public boolean isOpen() {
152            return (getState() == open);
153        }
154        
155      public String getName() {      public String getName() {
156          return name;          return name;
157      }      }
# Line 126  public class CircuitBreaker{ Line 164  public class CircuitBreaker{
164          return closed.getThreshold();          return closed.getThreshold();
165      }      }
166            
167        public int getTimeout() {
168            return (int)open.getTimeout();
169        }
170        
171      public int getFailureCount() {      public int getFailureCount() {
172          if (getState() == closed) {          if (getState() == closed) {
173                  return closed.getFailureCount();                  return closed.getFailureCount();
# Line 146  public class CircuitBreaker{ Line 188  public class CircuitBreaker{
188          this.notifier = notifier;          this.notifier = notifier;
189      }      }
190            
191      public String getNotifierName() {      public String getNotifierName() {
192          return notifier.getClass().getName();          return NotiferHelper.getName(notifier);
193        }
194        
195        public synchronized ExecutorService getExecutor() {
196            
197            if (executor == null) {
198                    executor = Executors.newFixedThreadPool(1);
199            }
200            
201            return executor;
202            
203      }      }
204    
205  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20