|
| 1 | +// Copyright (c) 2025 Synadia Communications Inc. All Rights Reserved. |
| 2 | +// See LICENSE and NOTICE file for details. |
| 3 | + |
| 4 | +package io.synadia.examples; |
| 5 | + |
| 6 | +import io.nats.client.Connection; |
| 7 | +import io.nats.client.Nats; |
| 8 | +import io.nats.client.Options; |
| 9 | +import io.synadia.chaos.ChaosArguments; |
| 10 | +import io.synadia.chaos.ChaosRunner; |
| 11 | + |
| 12 | +import java.io.BufferedReader; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.InputStreamReader; |
| 16 | +import java.net.URL; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static io.synadia.chaos.ChaosUtils.out; |
| 21 | + |
| 22 | +public class ChaosRunnerSpecificPortExample { |
| 23 | + private static final int SPECIFIC_PORT = 4222; |
| 24 | + private static final int SERVER_COUNT = 3; // 1, 3, 5 |
| 25 | + private static final long DELAY = 3000; // the delay to bring a server down |
| 26 | + private static final long INITIAL_DELAY = 3000; // the delay to bring a server down the first time |
| 27 | + private static final long DOWN_TIME = 3000; // how long before bringing the server up |
| 28 | + private static final int HEALTH_CHECK_DELAY = 1000; |
| 29 | + |
| 30 | + private static final int NUM_CONNECTIONS = 5; |
| 31 | + |
| 32 | + public static void main(String[] args) throws Exception { |
| 33 | + ChaosArguments arguments = new ChaosArguments() |
| 34 | + .servers(SERVER_COUNT) |
| 35 | + .specificPort(SPECIFIC_PORT) |
| 36 | + .workDirectory("C:\\temp\\chaos-runner") |
| 37 | + .serverNamePrefix("cr-example-server") |
| 38 | + .clusterName("cr-example-cluster") |
| 39 | + .delay(DELAY) |
| 40 | + .initialDelay(INITIAL_DELAY) |
| 41 | + .downTime(DOWN_TIME) |
| 42 | + // this is done last so anything on the command line |
| 43 | + // is used over the hard coded items. |
| 44 | + .args(args); |
| 45 | + |
| 46 | + ChaosRunner runner = ChaosRunner.start(arguments); |
| 47 | + |
| 48 | + // just give the servers a little time to be ready be first connect |
| 49 | + Thread.sleep(1000); |
| 50 | + |
| 51 | + String[] urls = runner.getConnectionUrls(); |
| 52 | + out("Connection Urls"); |
| 53 | + for (String url : urls) { |
| 54 | + out(" ", url); |
| 55 | + } |
| 56 | + |
| 57 | + @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") |
| 58 | + List<Connection> connections = new ArrayList<>(urls.length); |
| 59 | + for (int i = 0; i < NUM_CONNECTIONS; i++) { |
| 60 | + String connectionName = "Conn" + (i + 1); |
| 61 | + Options options = Options.builder().servers(urls) |
| 62 | + .connectionListener(new ChaosConnectionListener(connectionName)) |
| 63 | + .errorListener(new ChaosErrorListener(connectionName)) |
| 64 | + .build(); |
| 65 | + |
| 66 | + Connection connection = Nats.connect(options); |
| 67 | + connections.add(connection); |
| 68 | + } |
| 69 | + |
| 70 | + int[] ports = runner.getConnectionPorts(); |
| 71 | + int[] monitorPorts = runner.getMonitorPorts(); |
| 72 | + boolean hasMonitor = monitorPorts[0] > 0; |
| 73 | + |
| 74 | + String[] hzs = new String[ports.length]; |
| 75 | + while (true) { |
| 76 | + Thread.sleep(HEALTH_CHECK_DELAY); |
| 77 | + if (hasMonitor) { |
| 78 | + boolean changed = false; |
| 79 | + for (int i = 0; i < monitorPorts.length; i++) { |
| 80 | + String hz = readHealthz(monitorPorts[i]); |
| 81 | + if (!hz.equals(hzs[i])) { |
| 82 | + changed = true; |
| 83 | + hzs[i] = hz; |
| 84 | + } |
| 85 | + } |
| 86 | + if (changed) { |
| 87 | + out("HealthZ"); |
| 88 | + for (int i = 0; i < monitorPorts.length; i++) { |
| 89 | + int port = ports[i]; |
| 90 | + int mport = monitorPorts[i]; |
| 91 | + out(" ", port + "/" + mport, hzs[i]); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static String readHealthz(int port) { |
| 99 | + return readEndpoint(port, "healthz"); |
| 100 | + } |
| 101 | + |
| 102 | + private static String readEndpoint(int port, String endpoint) { |
| 103 | + String sUrl = "http://localhost:" + port + "/" + endpoint; |
| 104 | + try { |
| 105 | + URL url = new URL(sUrl); |
| 106 | + InputStream inputStream = url.openStream(); |
| 107 | + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); |
| 108 | + |
| 109 | + boolean first = true; |
| 110 | + String line; |
| 111 | + StringBuilder content = new StringBuilder(); |
| 112 | + while ((line = reader.readLine()) != null) { |
| 113 | + if (first) { |
| 114 | + first = false; |
| 115 | + } |
| 116 | + else { |
| 117 | + content.append(System.lineSeparator()); |
| 118 | + } |
| 119 | + content.append(line); |
| 120 | + } |
| 121 | + reader.close(); |
| 122 | + return content.toString().trim(); |
| 123 | + } |
| 124 | + catch (IOException e) { |
| 125 | + return e.getMessage(); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments