@@ -58,23 +58,23 @@ public class CloudWatchScenario {
5858 static CloudWatchActions cwActions = new CloudWatchActions ();
5959
6060 private static final Logger logger = LoggerFactory .getLogger (CloudWatchScenario .class );
61+ static Scanner scanner = new Scanner (System .in );
62+ public static void main (String [] args ) throws Throwable {
6163
62- public static void main (String [] args ) throws IOException {
63- Scanner scanner = new Scanner (System .in );
6464 final String usage = """
6565
66- Usage:
67- <myDate> <costDateWeek> <dashboardName> <dashboardJson> <dashboardAdd> <settings> <metricImage> \s
66+ Usage:
67+ <myDate> <costDateWeek> <dashboardName> <dashboardJson> <dashboardAdd> <settings> <metricImage> \s
6868
69- Where:
70- myDate - The start date to use to get metric statistics. (For example, 2023-01-11T18:35:24.00Z.)\s
71- costDateWeek - The start date to use to get AWS/Billinget statistics. (For example, 2023-01-11T18:35:24.00Z.)\s
72- dashboardName - The name of the dashboard to create.\s
73- dashboardJson - The location of a JSON file to use to create a dashboard. (See Readme file .)\s
74- dashboardAdd - The location of a JSON file to use to update a dashboard. (See Readme file .)\s
75- settings - The location of a JSON file from which various values are read. (See Readme file .)\s
76- metricImage - The location of a BMP file that is used to create a graph.\s
77- """ ;
69+ Where:
70+ myDate - The start date to use to get metric statistics. (For example, 2023-01-11T18:35:24.00Z.)\s
71+ costDateWeek - The start date to use to get AWS/Billinget statistics. (For example, 2023-01-11T18:35:24.00Z.)\s
72+ dashboardName - The name of the dashboard to create.\s
73+ dashboardJson - The location of a JSON file to use to create a dashboard. (See jsonWidgets.json in Github .)\s
74+ dashboardAdd - The location of a JSON file to use to update a dashboard. (See CloudDashboard.json in Github .)\s
75+ settings - The location of a JSON file from which various values are read. (See settings.json in Github .)\s
76+ metricImage - The location of a BMP file that is used to create a graph.\s
77+ """ ;
7878
7979 if (args .length != 7 ) {
8080 logger .info (usage );
@@ -88,9 +88,6 @@ public static void main(String[] args) throws IOException {
8888 String settings = args [5 ];
8989 String metricImage = args [6 ];
9090
91- Double dataPoint = Double .parseDouble ("10.0" );
92- Scanner sc = new Scanner (System .in );
93-
9491 logger .info (DASHES );
9592 logger .info ("Welcome to the Amazon CloudWatch Basics scenario." );
9693 logger .info ("""
@@ -104,24 +101,33 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
104101 alarms and automatically respond to changes in your environment,
105102 enabling you to quickly identify and address issues before they impact your
106103 applications or services.
107-
104+
108105 With CloudWatch, you can gain visibility into your entire infrastructure, from the cloud
109106 to the edge, and use this information to make informed decisions and optimize your
110107 resource utilization.
111-
108+
112109 This scenario guides you through how to perform Amazon CloudWatch tasks by using the
113110 AWS SDK for Java v2. Let's get started...
114111 """ );
115112 waitForInputToContinue (scanner );
113+
114+ try {
115+ runScenario (myDate , costDateWeek , dashboardName , dashboardJson , dashboardAdd , settings , metricImage );
116+ } catch (RuntimeException e ) {
117+ e .printStackTrace ();
118+ }
116119 logger .info (DASHES );
120+ }
117121
122+ private static void runScenario (String myDate , String costDateWeek , String dashboardName , String dashboardJson , String dashboardAdd , String settings , String metricImage ) throws Throwable {
123+ Double dataPoint = Double .parseDouble ("10.0" );
118124 logger .info (DASHES );
119125 logger .info ("""
120126 1. List at least five available unique namespaces from Amazon CloudWatch.
121127 Select one from the list.
122128 """ );
123- String selectedNamespace = "" ;
124- String selectedMetrics = "" ;
129+ String selectedNamespace ;
130+ String selectedMetrics ;
125131 int num ;
126132 try {
127133 CompletableFuture <ArrayList <String >> future = cwActions .listNameSpacesAsync ();
@@ -131,7 +137,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
131137 logger .info (" " + index + ". {}" , list .get (z ));
132138 }
133139
134- num = Integer .parseInt (sc .nextLine ());
140+ num = Integer .parseInt (scanner .nextLine ());
135141 if (1 <= num && num <= 5 ) {
136142 selectedNamespace = list .get (num - 1 );
137143 } else {
@@ -147,6 +153,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
147153 } else {
148154 logger .info ("An unexpected error occurred: " + rt .getMessage ());
149155 }
156+ throw cause ;
150157 }
151158 waitForInputToContinue (scanner );
152159 logger .info (DASHES );
@@ -170,7 +177,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
170177 int index = z + 1 ;
171178 logger .info (" " + index + ". " + metList .get (z ));
172179 }
173- num = Integer .parseInt (sc .nextLine ());
180+ num = Integer .parseInt (scanner .nextLine ());
174181 if (1 <= num && num <= 5 ) {
175182 selectedMetrics = metList .get (num - 1 );
176183 } else {
@@ -186,6 +193,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
186193 } else {
187194 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
188195 }
196+ throw cause ;
189197 }
190198
191199 try {
@@ -198,6 +206,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
198206 } else {
199207 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
200208 }
209+ throw cause ;
201210 }
202211
203212 waitForInputToContinue (scanner );
@@ -223,7 +232,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
223232 logger .info (" " + (t + 1 ) + ". {}" , statTypes .get (t ));
224233 }
225234 logger .info ("Select a metric statistic by entering a number from the preceding list:" );
226- num = Integer .parseInt (sc .nextLine ());
235+ num = Integer .parseInt (scanner .nextLine ());
227236 if (1 <= num && num <= 5 ) {
228237 metricOption = statTypes .get (num - 1 );
229238 } else {
@@ -244,6 +253,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
244253 } else {
245254 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
246255 }
256+ throw cause ;
247257 }
248258 waitForInputToContinue (scanner );
249259 logger .info (DASHES );
@@ -263,7 +273,8 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
263273 } else {
264274 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
265275 }
266- }
276+ throw cause ;
277+ }
267278 waitForInputToContinue (scanner );
268279 logger .info (DASHES );
269280
@@ -274,13 +285,14 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
274285 CompletableFuture <PutDashboardResponse > future = cwActions .createDashboardWithMetricsAsync (dashboardName , dashboardJson );
275286 future .join ();
276287
277- } catch (RuntimeException rt ) {
288+ } catch (RuntimeException | IOException rt ) {
278289 Throwable cause = rt .getCause ();
279290 if (cause instanceof CloudWatchException cwEx ) {
280291 logger .info ("CloudWatch error occurred: Error message: {}, Error code {}" , cwEx .getMessage (), cwEx .awsErrorDetails ().errorCode ());
281292 } else {
282293 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
283294 }
295+ throw cause ;
284296 }
285297 waitForInputToContinue (scanner );
286298 logger .info (DASHES );
@@ -299,6 +311,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
299311 } else {
300312 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
301313 }
314+ throw cause ;
302315 }
303316 waitForInputToContinue (scanner );
304317 logger .info (DASHES );
@@ -321,6 +334,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
321334 } else {
322335 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
323336 }
337+ throw cause ;
324338 }
325339 waitForInputToContinue (scanner );
326340 logger .info (DASHES );
@@ -339,6 +353,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
339353 } else {
340354 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
341355 }
356+ throw cause ;
342357 }
343358 logger .info (DASHES );
344359
@@ -357,6 +372,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
357372 } else {
358373 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
359374 }
375+ throw cause ;
360376 }
361377 waitForInputToContinue (scanner );
362378 logger .info (DASHES );
@@ -375,6 +391,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
375391 } else {
376392 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
377393 }
394+ throw cause ;
378395 }
379396 waitForInputToContinue (scanner );
380397 logger .info (DASHES );
@@ -392,6 +409,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
392409 } else {
393410 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
394411 }
412+ throw cause ;
395413 }
396414 waitForInputToContinue (scanner );
397415 logger .info (DASHES );
@@ -410,6 +428,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
410428 } else {
411429 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
412430 }
431+ throw cause ;
413432 }
414433 waitForInputToContinue (scanner );
415434 logger .info (DASHES );
@@ -428,6 +447,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
428447 } else {
429448 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
430449 }
450+ throw cause ;
431451 }
432452 waitForInputToContinue (scanner );
433453 logger .info (DASHES );
@@ -446,6 +466,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
446466 } else {
447467 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
448468 }
469+ throw cause ;
449470 }
450471 logger .info (DASHES );
451472
@@ -473,6 +494,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
473494 } else {
474495 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
475496 }
497+ throw cause ;
476498 }
477499 waitForInputToContinue (scanner );
478500 logger .info (DASHES );
@@ -491,6 +513,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
491513 } else {
492514 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
493515 }
516+ throw cause ;
494517 }
495518 waitForInputToContinue (scanner );
496519 logger .info (DASHES );
@@ -508,6 +531,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
508531 } else {
509532 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
510533 }
534+ throw cause ;
511535 }
512536 logger .info (DASHES );
513537
@@ -527,6 +551,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
527551 } else {
528552 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
529553 }
554+ throw cause ;
530555 }
531556
532557 try {
@@ -542,6 +567,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
542567 } else {
543568 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
544569 }
570+ throw cause ;
545571 }
546572
547573 try {
@@ -557,6 +583,7 @@ provided by Amazon Web Services (AWS). It is designed to help you monitor your
557583 } else {
558584 logger .info ("An unexpected error occurred: {}" , rt .getMessage ());
559585 }
586+ throw cause ;
560587 }
561588 waitForInputToContinue (scanner );
562589 logger .info (DASHES );
0 commit comments