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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20