This topic describes how to install CloudMonitor SDK for Java and provides sample code.

Background information

  • To use CloudMonitor SDK for Java, you must install the core library of Alibaba Cloud SDK for Java (aliyun-java-sdk-core) and the core library of Alibaba Cloud CloudMonitor SDK for Java (aliyun-java-sdk-cms).
  • Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For more information about the CloudMonitor API, see List of API operations by function.

Install CloudMonitor SDK for Java

You can use one of the following methods to install CloudMonitor SDK for Java. For more information, see Quick start.

  • Method 1: Add dependencies by using Maven (recommended)
    1. Use Maven to create a project.
      mvn archetype:generate -DgroupId=com.aliyun.cms.sample \
      -DartifactId=cms-sdk-sample \
      -Dpackage=com.aliyun.cms.sample \
      -Dversion=1.0-SNAPSHOT
    2. Add dependencies to the pom.xml file of the project.
      <dependencies>
           <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>3.2.6</version>
          </dependency>
      <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-cms</artifactId>
        <version>7.0.24</version>
      </dependency>
      </dependencies>
      Note You can visit the following links to obtain the latest versions of the aliyun-java-sdk-core and aliyun-java-sdk-cms packages:
  • Method 2: Download the JAR packages of the SDKs and add the packages to your project
    You can use this method to install the SDKs for Java regardless of whether you are using Eclipse or IntelliJ as the integrated development environment (IDE). You can download the JAR packages of the SDKs from the following links:

Sample code

The following sample code provides examples on how to use CloudMonitor SDK for Java to call CloudMonitor API operations DescribeMetricList and PutResourceMetricRule. For other operations, you can use OpenAPI Explorer to debug the operations and obtain sample code.

  • DescribeMetricList

    For more information about the request parameters of the DescribeMetricList operation, see DescribeMetricList.

    • Sample request
      import com.aliyuncs.DefaultAcsClient;
      import com.aliyuncs.IAcsClient;
      import com.aliyuncs.exceptions.ClientException;
      import com.aliyuncs.exceptions.ServerException;
      import com.aliyuncs.profile.DefaultProfile;
      import com.google.gson.Gson;
      import java.util.*;
      import com.aliyuncs.cms.model.v20190101.*;
      
      public class DescribeMetricList {
      
          public static void main(String[] args) {
      
              // Construct an Alibaba Cloud client that is used to initiate requests. 
              // When you construct the client, specify your AccessKey ID and AccessKey secret. 
              DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
              IAcsClient client = new DefaultAcsClient(profile);
      
             // Construct a request and specify request parameters. 
              DescribeMetricListRequest request = new DescribeMetricListRequest();
              request.setRegionId("cn-hangzhou");
              // Specify the Alibaba Cloud service and metric to collect. 
              request.setMetricName("cpu_total");
              request.setNamespace("acs_ecs_dashboard");
      
              // Specify the beginning and end of the time range to query monitoring data. 
              request.setStartTime("1628055731050");
              request.setEndTime("1628062931050");
      
              try {
                  DescribeMetricListResponse response = client.getAcsResponse(request);
                  System.out.println(new Gson().toJson(response));
              } catch (ServerException e) {
                  e.printStackTrace();
              } catch (ClientException e) {
                  System.out.println("ErrCode:" + e.getErrCode());
                  System.out.println("ErrMsg:" + e.getErrMsg());
                  System.out.println("RequestId:" + e.getRequestId());
              }
      
          }
      } 
    • Sample response
      {
          "RequestId": "2BE7EFE0-B50B-5168-BBA2-69B57E98D9C9",
          "NextToken": "1628055900000457fadc4b4c414f7166106dde17be869f14b2c0031323038383633313738363132393533242c692d6270316371686977317a613238303773636d316a242c",
          "Period": "900",
          "Datapoints": "[{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-0xii2bvf42iqvxbp****\",\"Minimum\":0.0,\"Maximum\":2.0,\"Average\":0.11},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze1qff61suafi71****\",\"Minimum\":0.0,\"Maximum\":4.95,\"Average\":0.88},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze90ts4e7dj4fip****\",\"Minimum\":0.0,\"Maximum\":9.3,\"Average\":1.03},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze90ts4e7dj4fip****\",\"Minimum\":0.0,\"Maximum\":10.86,\"Average\":1.2},{\"timestamp\":1628055900000,\"userId\":\"120886317861****\",\"instanceId\":\"i-2ze90ts4e7dj4fip****\",\"Minimum\":0.0,\"Maximum\":7.34,\"Average\":0.96}]",
          "Code": "200",
          "Success": true
      }
  • PutResourceMetricRule

    For more information about the request parameters of the PutResourceMetricRule operation, see PutResourceMetricRule.

    • Sample request
      import com.aliyuncs.DefaultAcsClient;
      import com.aliyuncs.IAcsClient;
      import com.aliyuncs.exceptions.ClientException;
      import com.aliyuncs.exceptions.ServerException;
      import com.aliyuncs.profile.DefaultProfile;
      import com.google.gson.Gson;
      import java.util.*;
      import com.aliyuncs.cms.model.v20190101.*;
      
      public class PutResourceMetricRule {
      
          public static void main(String[] args) {
              // Construct an Alibaba Cloud client that is used to initiate requests. 
              // When you construct the client, specify your AccessKey ID and AccessKey secret. 
              DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
              IAcsClient client = new DefaultAcsClient(profile);
      
              PutResourceMetricRuleRequest request = new PutResourceMetricRuleRequest();
              request.setRegionId("cn-hangzhou");
              // Specify the ID of the alert rule. 
              request.setRuleId("a151cd6023eacee2f0978e03863cc1697c89508****");
              request.setNamespace("acs_ecs_dashboard");
              request.setMetricName("cpu_total");
              // Specify the resources to which the alert rule are applied. 
              request.setResources("[{\"instanceId\":\"i-0xii2bvf42iqvxbp****\"}]");
              request.setContactGroups("ECS_Group");
              // Specify the name of the alert rule. 
              request.setRuleName("myRuleId123");
              request.setEscalationsCriticalStatistics("Average");
              request.setEscalationsCriticalComparisonOperator("GreaterThanOrEqualToThreshold");
              request.setEscalationsCriticalThreshold("95");
              request.setEscalationsCriticalTimes(3);
      
              try {
                  PutResourceMetricRuleResponse response = client.getAcsResponse(request);
                  System.out.println(new Gson().toJson(response));
              } catch (ServerException e) {
                  e.printStackTrace();
              } catch (ClientException e) {
                  System.out.println("ErrCode:" + e.getErrCode());
                  System.out.println("ErrMsg:" + e.getErrMsg());
                  System.out.println("RequestId:" + e.getRequestId());
              }
      
          }
      }
    • Sample response
      {
          "Message": "",
          "RequestId": "80AFF69C-A779-5B68-BAA7-91CD059C05CA",
          "Code": 200,
          "Success": true
      }