Skip to content

Commit 6365d35

Browse files
author
guyplusplus
committed
Added demo app
1 parent 51fae4a commit 6365d35

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.geckotechnology.simpleCircuitBreaker;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.Date;
8+
import java.util.Properties;
9+
import java.util.Random;
10+
import java.util.Timer;
11+
import java.util.TimerTask;
12+
import java.util.concurrent.atomic.AtomicInteger;
13+
14+
import org.junit.Test;
15+
16+
public class DemoApp {
17+
18+
private static final Random rd = new Random(System.currentTimeMillis());
19+
private static final int NB_THREADS = 5;
20+
private static final float ACTUAL_FAILURE_RATIO = 70;
21+
private static final long WAIT_BETWEEN_LOOP_MS = 500;
22+
23+
private CircuitBreaker circuitBreaker;
24+
25+
public static void main(String[] args) {
26+
new Thread() {
27+
@Override
28+
public void run() {
29+
new DemoApp().test();
30+
}
31+
}.start();
32+
}
33+
34+
private void loadConfigFile() {
35+
try {
36+
Properties props = new Properties();
37+
InputStream is = CircuitBreakerConfigTest.class.getResourceAsStream("/demo.config");
38+
props.load(is);
39+
is.close();
40+
CircuitBreakerConfig config = new CircuitBreakerConfig(props);
41+
System.out.println("config:{" + config.toString() + "}");
42+
circuitBreaker = new CircuitBreaker(config);
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
System.exit(-1);
46+
}
47+
circuitBreaker.getBreakerStateEventManager().addBreakerStateEventListener(new BreakerStateEventListener() {
48+
@Override
49+
public void onCircuitBreakerStateChangeEvent(CircuitBreakerStateChangeEvent event) {
50+
System.out.println("CircuitBreaker state changed. " + event);
51+
}
52+
});
53+
}
54+
55+
public void test() {
56+
loadConfigFile();
57+
for(int i = 0; i<NB_THREADS; i++) {
58+
final AtomicInteger threadId = new AtomicInteger(i);
59+
new Thread() {
60+
@Override
61+
public void run() {
62+
while(true) {
63+
if(circuitBreaker.isClosedForThisCall()) {
64+
boolean willFail = (rd.nextInt(1000) > 10 * ACTUAL_FAILURE_RATIO);
65+
long duration = rd.nextInt(1500);
66+
if(willFail) {
67+
System.out.println("[" + threadId.get() + "] Breaker is closed. Simulating callFailed with duration: " + duration);
68+
TestUtils.sleep(duration);
69+
circuitBreaker.callFailed(duration);
70+
}
71+
else {
72+
System.out.println("[" + threadId.get() + "] Breaker is closed. Simulating callSucceeded with duration: " + duration);
73+
TestUtils.sleep(duration);
74+
circuitBreaker.callSucceeded(duration);
75+
}
76+
}
77+
else {
78+
System.out.println("[" + threadId.get() + "] Breaker is opened. Pass...");
79+
}
80+
TestUtils.sleep(WAIT_BETWEEN_LOOP_MS);
81+
}
82+
}
83+
}.start();
84+
}
85+
}
86+
87+
}

src/test/resources/demo.config

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=demo
2+
slidingWindowSize=30
3+
permittedNumberOfCallsInHalfOpenState=5
4+
slowCallDurationThreshold=528
5+
minimumNumberOfCalls=20
6+
failureRateThreshold=75.1
7+
slowCallRateThreshold=45
8+
waitDurationInOpenState=2000
9+
maxDurationOpenInHalfOpenState=1100

0 commit comments

Comments
 (0)