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

Contents of /CircuitBreaker/src/dk/thoerup/circuitbreaker/OpenState.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1306 - (show annotations) (download)
Tue Apr 19 15:22:09 2011 UTC (13 years, 1 month ago) by torben
File size: 1373 byte(s)
Switch to BreakerConfig method this way it is possible to control the breaker dynamically
1 package dk.thoerup.circuitbreaker;
2
3 import java.util.concurrent.atomic.AtomicLong;
4
5 import dk.thoerup.circuitbreaker.config.BreakerConfig;
6
7
8 public final class OpenState implements CircuitBreakerState {
9
10 final private AtomicLong tripTime = new AtomicLong(0);
11
12 BreakerConfig config;
13
14 public void setTimeout(BreakerConfig config) {
15 this.config = config;
16 }
17
18 public void preInvoke(CircuitBreaker circuitBreaker) throws Exception
19 {
20 long now = System.currentTimeMillis();
21 long elapsed = now - tripTime.get();
22
23 if(elapsed > config.getTimeout())
24 {
25 circuitBreaker.attemptReset();
26 }
27 else
28 {
29 throw new CircuitBreakerException("Circuit Breaker is open; calls are failing fast");
30 }
31 }
32
33 public void postInvoke(CircuitBreaker circuitBreaker) throws Exception
34 {
35 // NO OP
36 }
37
38 public void onError(CircuitBreaker circuitBreaker, Exception t) throws Exception
39 {
40 // NO OP
41 }
42
43 void trip()
44 {
45 long now = System.currentTimeMillis();
46 tripTime.set(now);
47 }
48
49 public String getName() {
50 return "Open";
51 }
52
53 public long getTimeout() {
54 return config.getTimeout();
55 }
56
57 public long getElapsed() {
58 long now = System.currentTimeMillis();
59 long elapsed = now - tripTime.get();
60 return elapsed;
61 }
62
63 }

Properties

Name Value
svn:mergeinfo

  ViewVC Help
Powered by ViewVC 1.1.20