/[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 627 by torben, Mon Mar 8 10:12:59 2010 UTC revision 1314 by torben, Tue Apr 19 17:12:27 2011 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.config.BreakerConfig;
8    import dk.thoerup.circuitbreaker.config.StaticConfig;
9    import dk.thoerup.circuitbreaker.notification.NotiferHelper;
10  import dk.thoerup.circuitbreaker.notification.Notifier;  import dk.thoerup.circuitbreaker.notification.Notifier;
11  import dk.thoerup.circuitbreaker.notification.NullNotifier;  import dk.thoerup.circuitbreaker.notification.NullNotifier;
12    
# Line 51  public class CircuitBreaker{ Line 57  public class CircuitBreaker{
57                    
58          private String name;          private String name;
59                    
60            private ExecutorService executor = null;        
61          private Notifier notifier = new NullNotifier();          private Notifier notifier = new NullNotifier();
62                    
63          public CircuitBreaker(String name, int threshold, long timeoutMS) {          /*public CircuitBreaker(String name, int threshold, int timeoutMS) {
64                  closed.setThreshold(threshold);                  this(name, new StaticConfig(threshold, timeoutMS) );
65                  open.setTimeout(timeoutMS);          }*/
66            
67            public CircuitBreaker(String name, BreakerConfig config) {
68                    closed.setThreshold(config);
69                    open.setTimeout(config);
70                                    
71                  this.name = name;                  this.name = name;
72                    
# Line 63  public class CircuitBreaker{ Line 74  public class CircuitBreaker{
74                  internalReset();                  internalReset();
75          }          }
76                    
77            public synchronized void shutdown() {
78                    if (executor != null) {
79                            executor.shutdown();
80                    }
81            }
82            
83                    
84      public Object invoke(CircuitInvocation invocation) throws Exception      public Object invoke(CircuitInvocation invocation) throws Exception
85      {      {
# Line 82  public class CircuitBreaker{ Line 99  public class CircuitBreaker{
99      }      }
100            
101      public void tripBreaker() {      public void tripBreaker() {
102          synchronized(this) {          commonTripBreaker(Notifier.Event.BreakerTripped);
103        }
104        
105            //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
106        public void retripBreaker() {
107            commonTripBreaker(Notifier.Event.BreakerRetripped);
108        }
109        
110        private void commonTripBreaker(Notifier.Event event) {
111            synchronized(this) {
112                  if (currentState != open) { // TODO:Is this conditional necessary ??                  if (currentState != open) { // TODO:Is this conditional necessary ??
113                          open.trip();                          open.trip();
114                          currentState = open;                          currentState = open;
115                                    
116                          notifier.sendNotification(name, Notifier.Event.BreakerTripped);                          notifier.sendNotification(this, event);
117                  }                  }
118          }              }      
119      }      }
120        
121      public void attemptReset() {      public void attemptReset() {
122          synchronized(this) {          synchronized(this) {
123                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??                  if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
124                          currentState = halfOpen;                          currentState = halfOpen;
125                          notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);                          notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset);
126                  }                  }
127          }          }
128                    
# Line 106  public class CircuitBreaker{ Line 132  public class CircuitBreaker{
132          synchronized(this) {          synchronized(this) {
133                  if (currentState != closed) { // TODO: Is this conditional necessary ??                  if (currentState != closed) { // TODO: Is this conditional necessary ??
134                          internalReset();                          internalReset();
135                          notifier.sendNotification(name, Notifier.Event.BreakerReset);                          notifier.sendNotification(this, Notifier.Event.BreakerReset);
136                  }                  }
137          }          }
138      }      }
# Line 168  public class CircuitBreaker{ Line 194  public class CircuitBreaker{
194          this.notifier = notifier;          this.notifier = notifier;
195      }      }
196            
197      public String getNotifierName() {      public String getNotifierName() {
198          return notifier.getClass().getName();          return NotiferHelper.getName(notifier);
199        }
200        
201        public synchronized ExecutorService getExecutor() {
202            
203            if (executor == null) {
204                    executor = Executors.newFixedThreadPool(1);
205            }
206            
207            return executor;
208            
209      }      }
210    
211  }  }

Legend:
Removed from v.627  
changed lines
  Added in v.1314

  ViewVC Help
Powered by ViewVC 1.1.20