/[projects]/miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java
ViewVC logotype

Annotation of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/CircuitBreaker.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1314 - (hide annotations) (download)
Tue Apr 19 17:12:27 2011 UTC (13 years, 1 month ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java
File size: 5117 byte(s)
Switch entirely to BreakerConfig inits
1 torben 467 package dk.thoerup.circuitbreaker;
2 torben 393
3    
4 torben 1148 import java.util.concurrent.ExecutorService;
5     import java.util.concurrent.Executors;
6    
7 torben 1306 import dk.thoerup.circuitbreaker.config.BreakerConfig;
8     import dk.thoerup.circuitbreaker.config.StaticConfig;
9 torben 1289 import dk.thoerup.circuitbreaker.notification.NotiferHelper;
10 torben 467 import dk.thoerup.circuitbreaker.notification.Notifier;
11     import dk.thoerup.circuitbreaker.notification.NullNotifier;
12 torben 409
13 torben 394 /* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java
14     *
15     * example of how it can be used
16 torben 395
17     private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000);
18     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
19 torben 393 class TestInvocation implements CircuitInvocation {
20     String url;
21     public TestInvocation(String url) {
22     this.url = url;
23     }
24     public Object proceed() throws Exception{
25    
26     URL u = new URL(url);
27     URLConnection c = u.openConnection();
28     c.connect();
29     InputStream in = c.getInputStream();
30     in.close();
31    
32    
33     return "hello";
34     }
35    
36     }
37     try {
38     String s = (String) cb.invoke(new TestInvocation("http://rafiki/test"));
39     response.getWriter().print(s);
40 torben 452 } catch (Exception e) {
41 torben 393 logger.warning( e.getMessage() );
42     response.sendError(500);
43     return;
44     }
45    
46     }
47     */
48    
49    
50     public class CircuitBreaker{
51    
52 torben 443 private volatile CircuitBreakerState currentState;
53 torben 393
54 torben 424 private final OpenState open = new OpenState();
55     private final HalfOpenState halfOpen = new HalfOpenState();
56     private final ClosedState closed = new ClosedState();
57 torben 393
58     private String name;
59    
60 torben 1148 private ExecutorService executor = null;
61 torben 409 private Notifier notifier = new NullNotifier();
62    
63 torben 1314 /*public CircuitBreaker(String name, int threshold, int timeoutMS) {
64 torben 1306 this(name, new StaticConfig(threshold, timeoutMS) );
65 torben 1314 }*/
66 torben 1306
67     public CircuitBreaker(String name, BreakerConfig config) {
68     closed.setThreshold(config);
69     open.setTimeout(config);
70 torben 393
71     this.name = name;
72 torben 625
73 torben 627 //set correct intial state
74     internalReset();
75 torben 393 }
76    
77 torben 1148 public synchronized void shutdown() {
78     if (executor != null) {
79     executor.shutdown();
80     }
81     }
82 torben 393
83 torben 1148
84 torben 450 public Object invoke(CircuitInvocation invocation) throws Exception
85 torben 393 {
86     Object result = null;
87     try
88     {
89     getState().preInvoke(this);
90     result = invocation.proceed();
91     getState().postInvoke(this);
92     }
93 torben 450 catch(Exception e)
94 torben 393 {
95 torben 450 getState().onError(this, e);
96     throw e;
97 torben 393 }
98     return result;
99     }
100    
101     public void tripBreaker() {
102 torben 871 commonTripBreaker(Notifier.Event.BreakerTripped);
103 torben 393 }
104    
105 torben 864 //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 torben 871 commonTripBreaker(Notifier.Event.BreakerRetripped);
108     }
109    
110     private void commonTripBreaker(Notifier.Event event) {
111     synchronized(this) {
112 torben 864 if (currentState != open) { // TODO:Is this conditional necessary ??
113     open.trip();
114     currentState = open;
115    
116 torben 1161 notifier.sendNotification(this, event);
117 torben 864 }
118 torben 871 }
119 torben 864 }
120    
121 torben 393 public void attemptReset() {
122     synchronized(this) {
123 torben 426 if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
124     currentState = halfOpen;
125 torben 1161 notifier.sendNotification(this, Notifier.Event.BreakerAttemptReset);
126 torben 426 }
127 torben 393 }
128 torben 409
129 torben 393 }
130    
131     public void reset() {
132     synchronized(this) {
133 torben 426 if (currentState != closed) { // TODO: Is this conditional necessary ??
134 torben 627 internalReset();
135 torben 1161 notifier.sendNotification(this, Notifier.Event.BreakerReset);
136 torben 426 }
137 torben 393 }
138     }
139    
140 torben 627 //This one actually sets the correct closed/reset state
141     private void internalReset() {
142     closed.resetFailureCount();
143     currentState = closed;
144     }
145 torben 397
146 torben 627
147 torben 393 private CircuitBreakerState getState() {
148     synchronized(this) {
149     return currentState;
150     }
151     }
152 torben 397
153 torben 447 public boolean isClosed() {
154     return (getState() == closed);
155     }
156    
157     public boolean isOpen() {
158     return (getState() == open);
159     }
160    
161 torben 397 public String getName() {
162     return name;
163     }
164    
165     public String getStateName() {
166     return getState().getName();
167     }
168    
169     public int getThreshold() {
170     return closed.getThreshold();
171     }
172    
173 torben 413 public int getTimeout() {
174     return (int)open.getTimeout();
175     }
176    
177 torben 397 public int getFailureCount() {
178     if (getState() == closed) {
179     return closed.getFailureCount();
180     } else {
181     return -1;
182     }
183     }
184    
185     public long getElapsed() {
186     if (getState() == open) {
187     return open.getElapsed();
188     } else {
189     return -1;
190     }
191     }
192 torben 409
193     public void setNotifier(Notifier notifier) {
194     this.notifier = notifier;
195     }
196    
197 torben 1289 public String getNotifierName() {
198     return NotiferHelper.getName(notifier);
199 torben 409 }
200 torben 1148
201     public synchronized ExecutorService getExecutor() {
202    
203     if (executor == null) {
204     executor = Executors.newFixedThreadPool(1);
205     }
206    
207     return executor;
208    
209     }
210 torben 397
211 torben 393 }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20