1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.circuitbreaker; |
2 |
|
|
3 |
import java.util.concurrent.atomic.AtomicLong; |
import java.util.concurrent.atomic.AtomicLong; |
4 |
|
|
5 |
|
import dk.thoerup.circuitbreaker.config.BreakerConfig; |
6 |
|
|
7 |
|
|
8 |
|
|
9 |
public final class OpenState implements CircuitBreakerState { |
public final class OpenState implements CircuitBreakerState { |
10 |
|
|
11 |
final private AtomicLong tripTime = new AtomicLong(0); |
final private AtomicLong tripTime = new AtomicLong(0); |
|
final private AtomicLong timeout = new AtomicLong(0); |
|
12 |
|
|
13 |
public void setTimeout(long timeout) { |
BreakerConfig config; |
14 |
this.timeout.set( timeout ); |
|
15 |
|
public void setTimeout(BreakerConfig config) { |
16 |
|
this.config = config; |
17 |
} |
} |
18 |
|
|
19 |
public void preInvoke(CircuitBreaker circuitBreaker) throws Exception |
|
20 |
|
public void preInvoke(CircuitBreaker<?> circuitBreaker) throws Exception |
21 |
{ |
{ |
22 |
long now = System.currentTimeMillis(); |
long now = System.currentTimeMillis(); |
23 |
long elapsed = now - tripTime.get(); |
long elapsed = now - tripTime.get(); |
24 |
|
|
25 |
if(elapsed > timeout.get()) |
if(elapsed > config.getTimeout()) |
26 |
{ |
{ |
27 |
circuitBreaker.attemptReset(); |
circuitBreaker.attemptReset(); |
28 |
} |
} |
32 |
} |
} |
33 |
} |
} |
34 |
|
|
35 |
public void postInvoke(CircuitBreaker circuitBreaker) throws Exception |
public void postInvoke(CircuitBreaker<?> circuitBreaker) throws Exception |
36 |
{ |
{ |
37 |
// NO OP |
// NO OP |
38 |
} |
} |
39 |
|
|
40 |
public void onError(CircuitBreaker circuitBreaker, Exception t) throws Exception |
public void onError(CircuitBreaker<?> circuitBreaker, Exception t) throws Exception |
41 |
{ |
{ |
42 |
// NO OP |
// NO OP |
43 |
} |
} |
53 |
} |
} |
54 |
|
|
55 |
public long getTimeout() { |
public long getTimeout() { |
56 |
return timeout.get(); |
return config.getTimeout(); |
57 |
} |
} |
58 |
|
|
59 |
public long getElapsed() { |
public long getElapsed() { |