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

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20