66import io .nats .client .Connection ;
77import io .nats .client .Nats ;
88import io .nats .client .Options ;
9- import io .nats .client .api .ServerInfo ;
109import io .synadia .chaos .ChaosArguments ;
1110import io .synadia .chaos .ChaosRunner ;
1211
2120import static io .synadia .chaos .ChaosUtils .report ;
2221
2322public class ChaosRunnerExample {
24- private static final int NUM_CONNECTIONS = 2 ;
25-
26- private static final int SERVER_COUNT = 5 ; // 1, 3, 5
23+ private static final int SERVER_COUNT = 3 ; // 1, 3, 5
2724 private static final long DELAY = 5000 ; // the delay to bring a server down
2825 private static final long INITIAL_DELAY = 10000 ; // the delay to bring a server down the first time
2926 private static final long DOWN_TIME = 5000 ; // how long before bringing the server up
3027 private static final int HEALTH_CHECK_DELAY = 3000 ;
3128
29+ private static final int NUM_CONNECTIONS = 5 ;
30+
3231 public static void main (String [] args ) throws Exception {
3332 ChaosArguments arguments = new ChaosArguments ()
3433 .servers (SERVER_COUNT )
34+ .workDirectory ("C:\\ temp\\ chaos-runner" )
3535 .serverNamePrefix ("cr-example-server" )
3636 .clusterName ("cr-example-cluster" )
3737 .delay (DELAY )
3838 .initialDelay (INITIAL_DELAY )
39- .downTime (DOWN_TIME );
39+ .downTime (DOWN_TIME )
40+ // this is done last so anything on the command line
41+ // is used over the hard coded items.
42+ .args (args );
4043
4144 ChaosRunner runner = ChaosRunner .start (arguments );
4245
@@ -49,30 +52,33 @@ public static void main(String[] args) throws Exception {
4952 report (" " , url );
5053 }
5154
55+ @ SuppressWarnings ("MismatchedQueryAndUpdateOfCollection" )
5256 List <Connection > connections = new ArrayList <>(urls .length );
5357 for (int i = 0 ; i < NUM_CONNECTIONS ; i ++) {
54- String cn = "CONN/ " + i ;
58+ String connectionName = "Conn " + ( i + 1 ) ;
5559 Options options = Options .builder ().servers (urls )
56- .connectionListener (new ChaosConnectionListener (cn ))
57- .errorListener (new ChaosErrorListener (cn ))
60+ .connectionListener (new ChaosConnectionListener (connectionName ))
61+ .errorListener (new ChaosErrorListener (connectionName ))
5862 .build ();
5963
6064 Connection connection = Nats .connect (options );
6165 connections .add (connection );
62- ServerInfo si = connection .getServerInfo ();
63- report ("Initial connection" , cn , si .getPort (), si .getCluster ());
6466 }
6567
6668 int [] ports = runner .getConnectionPorts ();
6769 int [] monitorPorts = runner .getMonitorPorts ();
70+ boolean hasMonitor = monitorPorts [0 ] > 0 ;
6871
72+ String [] reports = new String [ports .length ];
6973 while (true ) {
7074 Thread .sleep (HEALTH_CHECK_DELAY );
71- report ("HealthZ" );
72- for (int i = 0 ; i < monitorPorts .length ; i ++) {
73- int port = ports [i ];
74- int mport = monitorPorts [i ];
75- report (" " , port + "/" + mport , readHealthz (mport ));
75+ if (hasMonitor ) {
76+ report ("HealthZ" );
77+ for (int i = 0 ; i < monitorPorts .length ; i ++) {
78+ int port = ports [i ];
79+ int mport = monitorPorts [i ];
80+ report (" " , port + "/" + mport , readHealthz (mport ));
81+ }
7682 }
7783 }
7884 }
0 commit comments