1 |
package dk.thoerup.curcuitbreaker; |
package dk.thoerup.curcuitbreaker; |
2 |
|
|
|
import java.util.logging.Logger; |
|
3 |
|
|
4 |
import dk.thoerup.curcuitbreaker.notification.Notifier; |
import dk.thoerup.curcuitbreaker.notification.Notifier; |
5 |
import dk.thoerup.curcuitbreaker.notification.NullNotifier; |
import dk.thoerup.curcuitbreaker.notification.NullNotifier; |
42 |
|
|
43 |
|
|
44 |
public class CircuitBreaker{ |
public class CircuitBreaker{ |
|
Logger logger = Logger.getLogger(CircuitBreaker.class.getName()); |
|
|
|
|
45 |
|
|
46 |
private CircuitBreakerState currentState; |
private volatile CircuitBreakerState currentState; |
47 |
|
|
48 |
private final OpenState open = new OpenState(); |
private final OpenState open = new OpenState(); |
49 |
private final HalfOpenState halfOpen = new HalfOpenState(); |
private final HalfOpenState halfOpen = new HalfOpenState(); |
63 |
} |
} |
64 |
|
|
65 |
|
|
66 |
public Object invoke(CircuitInvocation invocation) throws Throwable |
public Object invoke(CircuitInvocation invocation) throws Exception |
67 |
{ |
{ |
68 |
Object result = null; |
Object result = null; |
69 |
try |
try |
72 |
result = invocation.proceed(); |
result = invocation.proceed(); |
73 |
getState().postInvoke(this); |
getState().postInvoke(this); |
74 |
} |
} |
75 |
catch(Throwable t) |
catch(Exception e) |
76 |
{ |
{ |
77 |
getState().onError(this, t); |
getState().onError(this, e); |
78 |
throw t; |
throw e; |
79 |
} |
} |
80 |
return result; |
return result; |
81 |
} |
} |
117 |
} |
} |
118 |
} |
} |
119 |
|
|
120 |
|
public boolean isClosed() { |
121 |
|
return (getState() == closed); |
122 |
|
} |
123 |
|
|
124 |
|
public boolean isOpen() { |
125 |
|
return (getState() == open); |
126 |
|
} |
127 |
|
|
128 |
public String getName() { |
public String getName() { |
129 |
return name; |
return name; |
130 |
} |
} |