/[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 456 - (hide annotations) (download)
Tue Oct 20 20:42:36 2009 UTC (14 years, 7 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/curcuitbreaker/CircuitBreaker.java
File size: 3922 byte(s)
When doing a reset, make sure to also reset ClosedState's fail count
1 torben 393 package dk.thoerup.curcuitbreaker;
2    
3    
4 torben 409 import dk.thoerup.curcuitbreaker.notification.Notifier;
5     import dk.thoerup.curcuitbreaker.notification.NullNotifier;
6    
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    
62     reset();
63     }
64    
65    
66 torben 450 public Object invoke(CircuitInvocation invocation) throws Exception
67 torben 393 {
68     Object result = null;
69     try
70     {
71     getState().preInvoke(this);
72     result = invocation.proceed();
73     getState().postInvoke(this);
74     }
75 torben 450 catch(Exception e)
76 torben 393 {
77 torben 450 getState().onError(this, e);
78     throw e;
79 torben 393 }
80     return result;
81     }
82    
83     public void tripBreaker() {
84     synchronized(this) {
85 torben 426 if (currentState != open) { // TODO:Is this conditional necessary ??
86     open.trip();
87     currentState = open;
88 torben 393
89 torben 426 notifier.sendNotification(name, Notifier.Event.BreakerTripped);
90     }
91 torben 409 }
92 torben 393 }
93    
94     public void attemptReset() {
95     synchronized(this) {
96 torben 426 if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
97     currentState = halfOpen;
98     notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
99     }
100 torben 393 }
101 torben 409
102 torben 393 }
103    
104     public void reset() {
105     synchronized(this) {
106 torben 426 if (currentState != closed) { // TODO: Is this conditional necessary ??
107 torben 456 closed.resetFailureCount();
108 torben 426 currentState = closed;
109     notifier.sendNotification(name, Notifier.Event.BreakerReset);
110     }
111 torben 393 }
112     }
113    
114 torben 397
115 torben 393 private CircuitBreakerState getState() {
116     synchronized(this) {
117     return currentState;
118     }
119     }
120 torben 397
121 torben 447 public boolean isClosed() {
122     return (getState() == closed);
123     }
124    
125     public boolean isOpen() {
126     return (getState() == open);
127     }
128    
129 torben 397 public String getName() {
130     return name;
131     }
132    
133     public String getStateName() {
134     return getState().getName();
135     }
136    
137     public int getThreshold() {
138     return closed.getThreshold();
139     }
140    
141 torben 413 public int getTimeout() {
142     return (int)open.getTimeout();
143     }
144    
145 torben 397 public int getFailureCount() {
146     if (getState() == closed) {
147     return closed.getFailureCount();
148     } else {
149     return -1;
150     }
151     }
152    
153     public long getElapsed() {
154     if (getState() == open) {
155     return open.getElapsed();
156     } else {
157     return -1;
158     }
159     }
160 torben 409
161     public void setNotifier(Notifier notifier) {
162     this.notifier = notifier;
163     }
164    
165     public String getNotifierName() {
166 torben 412 return notifier.getClass().getName();
167 torben 409 }
168 torben 397
169 torben 393 }

  ViewVC Help
Powered by ViewVC 1.1.20