|
| 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 | +} |
0 commit comments