//snippet-sourcedescription:[ListMetrics.java demonstrates how to list CloudWatch metrics.]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-service:[cloudwatch]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourceauthor:[soo-aws]
// snippet-start:[cloudwatch.java2.list_metrics.complete]
/*
 * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.example.cloudwatch;
// snippet-start:[cloudwatch.java2.list_metrics.import]
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest;
import software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse;
import software.amazon.awssdk.services.cloudwatch.model.Metric;

/**
 * Lists CloudWatch metrics
 */
// snippet-end:[cloudwatch.java2.list_metrics.import]
public class ListMetrics {

    public static void main(String[] args) {

        final String USAGE =
            "To run this example, supply a metric namespace\n" +
            "Ex: ListMetrics <metric-namespace>\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String namespace = args[0];

        
        // snippet-start:[cloudwatch.java2.list_metrics.main]
        CloudWatchClient cw =
        		CloudWatchClient.builder().build();

        boolean done = false;
        String next_token = null;

        while(!done) {
            
        	ListMetricsResponse response;

        	if (next_token == null) {
        		ListMetricsRequest request = ListMetricsRequest.builder()
        				.namespace(namespace)
        				.build();

        		response = cw.listMetrics(request);
        	}
        	else {
        		ListMetricsRequest request = ListMetricsRequest.builder()
                        .namespace(namespace)
                        .nextToken(next_token)
                        .build();

        		response = cw.listMetrics(request);
        	}

            for(Metric metric : response.metrics()) {
                System.out.printf(
                    "Retrieved metric %s", metric.metricName());
                System.out.println();
            }

            if(response.nextToken() == null) {
                done = true;
            }
            else {
            	next_token = response.nextToken();
            }
        }
        // snippet-end:[cloudwatch.java2.list_metrics.main]
    }
}
// snippet-end:[cloudwatch.java2.list_metrics.complete]
@@ -287,66 +287,10 @@
 aws%5D
+%0D
 %0A
-// snippet-start:%5Bcloudwatch.java2.list_metrics.complete%5D
 %0D%0A/*
@@ -884,16 +884,17 @@
 se.%0D%0A */
+%0D
 %0Apackage
@@ -918,16 +918,77 @@
 dwatch;%0D
+%0A%0D%0A// snippet-start:%5Bcloudwatch.java2.list_metrics.complete%5D%0D
 %0A// snip
@@ -1329,49 +1329,8 @@
 ic;%0D
-%0A%0D%0A/**%0D%0A * Lists CloudWatch metrics%0D%0A */%0D
 %0A// 
@@ -1379,16 +1379,58 @@
 .import%5D
+%0D%0A/**%0D%0A * Lists CloudWatch metrics%0D%0A */%0D%0A%0D
 %0Apublic 
@@ -1542,16 +1542,20 @@
         
+    
 %22To run 
@@ -1600,16 +1600,28 @@
 e%5Cn%22 +%0D%0A
+            
         
@@ -1821,26 +1821,16 @@
 %5B0%5D;%0D%0A%0D%0A
-        %0D%0A
         
@@ -1923,18 +1923,24 @@
         
-%09%09
+        
 CloudWat
@@ -2016,26 +2016,25 @@
  String next
-_t
+T
 oken = null;
@@ -2061,28 +2061,16 @@
 one) %7B%0D%0A
-            
 %0D%0A      
@@ -2071,17 +2071,20 @@
         
-%09
+    
 ListMetr
@@ -2120,17 +2120,20 @@
     
-%09
+    
 if (next
 _tok
@@ -2124,26 +2124,25 @@
     if (next
-_t
+T
 oken == null
@@ -2146,34 +2146,40 @@
 ull) %7B%0D%0A        
-%09%09
+        
 ListMetricsReque
@@ -2229,20 +2229,32 @@
         
-%09%09%09%09
+                
 .namespa
@@ -2280,12 +2280,24 @@
     
-%09%09%09%09
+                
 .bui
@@ -2305,34 +2305,40 @@
 d();%0D%0A%0D%0A        
-%09%09
+        
 response = cw.li
@@ -2370,21 +2370,14 @@
     
-%09%7D%0D%0A        %09
+    %7D 
 else
@@ -2388,18 +2388,24 @@
         
-%09%09
+        
 ListMetr
@@ -2537,26 +2537,25 @@
 xtToken(next
-_t
+T
 oken)%0D%0A     
@@ -2598,10 +2598,16 @@
     
-%09%09
+        
 resp
@@ -2647,17 +2647,20 @@
         
+  
-%09
+  
 %7D%0D%0A%0D%0A   
@@ -2671,16 +2671,17 @@
      for
+ 
 (Metric 
@@ -2748,16 +2748,20 @@
 rintf(%0D%0A
+    
         
@@ -2965,29 +2965,16 @@
        %7D
-%0D%0A           
  else %7B%0D
@@ -2990,15 +2990,17 @@
     
-%09
+    
 next
-_t
+T
 oken
@@ -3157,28 +3157,30 @@
 java2.list_metrics.complete%5D
+%0D%0A

Update ListMetrics.java