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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 864 - (show annotations) (download)
Sun Jun 20 21:54:53 2010 UTC (13 years, 11 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/CircuitBreaker.java
File size: 4495 byte(s)
Differentiate against a normal trip and a retrip from half-open back to open
1 package dk.thoerup.circuitbreaker;
2
3
4 import dk.thoerup.circuitbreaker.notification.Notifier;
5 import dk.thoerup.circuitbreaker.notification.NullNotifier;
6
7 /* 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
11 private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000);
12 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13 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 } catch (Exception e) {
35 logger.warning( e.getMessage() );
36 response.sendError(500);
37 return;
38 }
39
40 }
41 */
42
43
44 public class CircuitBreaker{
45
46 private volatile CircuitBreakerState currentState;
47
48 private final OpenState open = new OpenState();
49 private final HalfOpenState halfOpen = new HalfOpenState();
50 private final ClosedState closed = new ClosedState();
51
52 private String name;
53
54 private Notifier notifier = new NullNotifier();
55
56 public CircuitBreaker(String name, int threshold, long timeoutMS) {
57 closed.setThreshold(threshold);
58 open.setTimeout(timeoutMS);
59
60 this.name = name;
61
62 //set correct intial state
63 internalReset();
64 }
65
66
67 public Object invoke(CircuitInvocation invocation) throws Exception
68 {
69 Object result = null;
70 try
71 {
72 getState().preInvoke(this);
73 result = invocation.proceed();
74 getState().postInvoke(this);
75 }
76 catch(Exception e)
77 {
78 getState().onError(this, e);
79 throw e;
80 }
81 return result;
82 }
83
84 public void tripBreaker() {
85 synchronized(this) {
86 if (currentState != open) { // TODO:Is this conditional necessary ??
87 open.trip();
88 currentState = open;
89
90 notifier.sendNotification(name, Notifier.Event.BreakerTripped);
91 }
92 }
93 }
94
95 //a re-trip should basically do the same as a normal trip, but it is here just to differentiate the two different events
96 public void retripBreaker() {
97 synchronized(this) {
98 if (currentState != open) { // TODO:Is this conditional necessary ??
99 open.trip();
100 currentState = open;
101
102 notifier.sendNotification(name, Notifier.Event.BreakerRetripped);
103 }
104 }
105 }
106
107 public void attemptReset() {
108 synchronized(this) {
109 if (currentState != halfOpen) { // TODO:Is this conditional necessary ??
110 currentState = halfOpen;
111 notifier.sendNotification(name, Notifier.Event.BreakerAttemptReset);
112 }
113 }
114
115 }
116
117 public void reset() {
118 synchronized(this) {
119 if (currentState != closed) { // TODO: Is this conditional necessary ??
120 internalReset();
121 notifier.sendNotification(name, Notifier.Event.BreakerReset);
122 }
123 }
124 }
125
126 //This one actually sets the correct closed/reset state
127 private void internalReset() {
128 closed.resetFailureCount();
129 currentState = closed;
130 }
131
132
133 private CircuitBreakerState getState() {
134 synchronized(this) {
135 return currentState;
136 }
137 }
138
139 public boolean isClosed() {
140 return (getState() == closed);
141 }
142
143 public boolean isOpen() {
144 return (getState() == open);
145 }
146
147 public String getName() {
148 return name;
149 }
150
151 public String getStateName() {
152 return getState().getName();
153 }
154
155 public int getThreshold() {
156 return closed.getThreshold();
157 }
158
159 public int getTimeout() {
160 return (int)open.getTimeout();
161 }
162
163 public int getFailureCount() {
164 if (getState() == closed) {
165 return closed.getFailureCount();
166 } else {
167 return -1;
168 }
169 }
170
171 public long getElapsed() {
172 if (getState() == open) {
173 return open.getElapsed();
174 } else {
175 return -1;
176 }
177 }
178
179 public void setNotifier(Notifier notifier) {
180 this.notifier = notifier;
181 }
182
183 public String getNotifierName() {
184 return notifier.getClass().getName();
185 }
186
187 }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20