5 |
/* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java |
/* Simple CircuitBreaker implementation - snipped from http://www.jroller.com/kenwdelong/entry/circuit_breaker_in_java |
6 |
* |
* |
7 |
* example of how it can be used |
* example of how it can be used |
8 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
|
9 |
|
private CircuitBreaker cb = new CircuitBreaker("test", 5, 10000); |
10 |
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
11 |
class TestInvocation implements CircuitInvocation { |
class TestInvocation implements CircuitInvocation { |
12 |
String url; |
String url; |
13 |
public TestInvocation(String url) { |
public TestInvocation(String url) { |
80 |
|
|
81 |
public void tripBreaker() { |
public void tripBreaker() { |
82 |
synchronized(this) { |
synchronized(this) { |
|
currentState = open; |
|
83 |
open.trip(); |
open.trip(); |
84 |
|
currentState = open; |
85 |
|
|
86 |
|
|
87 |
logger.warning("Circuitbreaker tripBreaker - " + name); |
logger.warning("Circuitbreaker tripBreaker - " + name); |
88 |
} |
} |
105 |
} |
} |
106 |
} |
} |
107 |
|
|
108 |
|
|
109 |
private CircuitBreakerState getState() { |
private CircuitBreakerState getState() { |
110 |
synchronized(this) { |
synchronized(this) { |
111 |
return currentState; |
return currentState; |
112 |
} |
} |
113 |
} |
} |
114 |
|
|
115 |
|
public String getName() { |
116 |
|
return name; |
117 |
|
} |
118 |
|
|
119 |
|
public String getStateName() { |
120 |
|
return getState().getName(); |
121 |
|
} |
122 |
|
|
123 |
|
public int getThreshold() { |
124 |
|
return closed.getThreshold(); |
125 |
|
} |
126 |
|
|
127 |
|
public int getFailureCount() { |
128 |
|
if (getState() == closed) { |
129 |
|
return closed.getFailureCount(); |
130 |
|
} else { |
131 |
|
return -1; |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
|
public long getElapsed() { |
136 |
|
if (getState() == open) { |
137 |
|
return open.getElapsed(); |
138 |
|
} else { |
139 |
|
return -1; |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
} |
} |