1 |
torben |
393 |
package dk.thoerup.curcuitbreaker; |
2 |
|
|
|
3 |
|
|
import java.util.concurrent.atomic.AtomicLong; |
4 |
|
|
|
5 |
|
|
|
6 |
|
|
public class OpenState implements CircuitBreakerState { |
7 |
|
|
|
8 |
|
|
private AtomicLong tripTime = new AtomicLong(0); |
9 |
|
|
private AtomicLong timeout = new AtomicLong(0); |
10 |
|
|
|
11 |
|
|
public void setTimeout(long timeout) { |
12 |
|
|
this.timeout.set( timeout ); |
13 |
|
|
} |
14 |
|
|
|
15 |
|
|
public void preInvoke(CircuitBreaker circuitBreaker) throws Throwable |
16 |
|
|
{ |
17 |
|
|
long now = System.currentTimeMillis(); |
18 |
|
|
long elapsed = now - tripTime.get(); |
19 |
|
|
|
20 |
|
|
if(elapsed > timeout.get()) |
21 |
|
|
{ |
22 |
|
|
circuitBreaker.attemptReset(); |
23 |
|
|
} |
24 |
|
|
else |
25 |
|
|
{ |
26 |
|
|
throw new CircuitBreakerException("Circuit Breaker is open; calls are failing fast"); |
27 |
|
|
} |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
public void postInvoke(CircuitBreaker circuitBreaker) throws Throwable |
31 |
|
|
{ |
32 |
|
|
// NO OP |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
public void onError(CircuitBreaker circuitBreaker, Throwable t) throws Throwable |
36 |
|
|
{ |
37 |
|
|
// NO OP |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
void trip() |
41 |
|
|
{ |
42 |
|
|
long now = System.currentTimeMillis(); |
43 |
|
|
tripTime.set(now); |
44 |
|
|
} |
45 |
|
|
|
46 |
torben |
397 |
public String getName() { |
47 |
|
|
return "Open"; |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
public long getTimeout() { |
51 |
|
|
return timeout.get(); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
public long getElapsed() { |
55 |
|
|
long now = System.currentTimeMillis(); |
56 |
|
|
long elapsed = now - tripTime.get(); |
57 |
|
|
return elapsed; |
58 |
|
|
} |
59 |
|
|
|
60 |
torben |
393 |
} |