/[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 864 by torben, Sun Jun 20 21:54:53 2010 UTC revision 1161 by torben, Mon Oct 4 17:36:43 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.circuitbreaker;  package dk.thoerup.circuitbreaker;
2    
3    
4    import java.util.concurrent.ExecutorService;
5    import java.util.concurrent.Executors;
6    
7  import dk.thoerup.circuitbreaker.notification.Notifier;  import dk.thoerup.circuitbreaker.notification.Notifier;
8  import dk.thoerup.circuitbreaker.notification.NullNotifier;  import dk.thoerup.circuitbreaker.notification.NullNotifier;
9    
# Line 51  public class CircuitBreaker{ Line 54  public class CircuitBreaker{
54                    
55          private String name;          private String name;
56                    
57            private ExecutorService executor = null;        
58          private Notifier notifier = new NullNotifier();          private Notifier notifier = new NullNotifier();
59                    
60          public CircuitBreaker(String name, int threshold, long timeoutMS) {          public CircuitBreaker(String name, int threshold, long timeoutMS) {
# Line 63  public class CircuitBreaker{ Line 67  public class CircuitBreaker{
67                  internalReset();                  internalReset();
68          }          }
69                    
70            public synchronized void shutdown() {
71                    if (executor != null) {
72                            executor.shutdown();
73                    }
74            }
75            
76                    
77      public Object invoke(CircuitInvocation invocation) throws Exception      public Object invoke(CircuitInvocation invocation) throws Exception
78      {      {
# Line 82  public class CircuitBreaker{ Line 92  public class CircuitBreaker{
92      }      }
93            
94      public void tripBreaker() {      public void tripBreaker() {
95          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerTripped);
                 if (currentState != open) { // TODO:Is this conditional necessary ??  
                         open.trip();  
                         currentState = open;  
                   
                         notifier.sendNotification(name, Notifier.Event.BreakerTripped);  
                 }  
         }      
96      }      }
97            
98          //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events          //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
99      public void retripBreaker() {      public void retripBreaker() {
100          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerRetripped);
101        }
102        
103        private void commonTripBreaker(Notifier.Event event) {
104            synchronized(this) {
105                  if (currentState != open) { // TODO:Is this conditional necessary ??                  if (currentState != open) { // TODO:Is this conditional necessary ??
106                          open.trip();                          open.trip();
107                          currentState = open;                          currentState = open;
108                                    
109                          notifier.sendNotification(name, Notifier.Event.BreakerRetripped);                          notifier.sendNotification(this, event);
110                  }                  }
111          }              }      
112      }      }
113    
114      public void attemptReset() {      public void attemptReset() {
115          synchronized(this) {          synchronized(this) {
116                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
117                          currentState = halfOpen;                          currentState = halfOpen;
118                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset);
119                  }                  }
120          }          }
121                    
# Line 118  public class CircuitBreaker{ Line 125  public class CircuitBreaker{
125          synchronized(this) {          synchronized(this) {
126                  if (currentState != closed) { // TODO: Is this conditional necessary ??                  if (currentState != closed) { // TODO: Is this conditional necessary ??
127                          internalReset();                          internalReset();
128                          notifier.sendNotification(name, Notifier.Event.BreakerReset);                          notifier.sendNotification(this, Notifier.Event.BreakerReset);
129                  }                  }
130          }          }
131      }      }
# Line 183  public class CircuitBreaker{ Line 190  public class CircuitBreaker{
190      public String getNotifierName() {      public String getNotifierName() {
191          return notifier.getClass().getName();          return notifier.getClass().getName();
192      }      }
193        
194        public synchronized ExecutorService getExecutor() {
195            
196            if (executor == null) {
197                    executor = Executors.newFixedThreadPool(1);
198            }
199            
200            return executor;
201            
202        }
203    
204  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20