All Products
Search
Document Center

API sample code and endpoints

Last Updated: Mar 28, 2019

This topic describes the RocketMQ API access procedure and precautions, including SDK obtaining, initialization parameter settings, and endpoint settings.

1. Obtain an SDK

Add the following configuration items to the pom.xml file to add an API-dependent SDK.

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-core</artifactId>
  5. <optional>true</optional>
  6. <version>4.3.3</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>com.aliyun</groupId>
  10. <artifactId>aliyun-java-sdk-ons</artifactId>
  11. <version>3.1.0</version> <! -- set to the latest version -->
  12. </dependency>
  13. </dependencies>

Note: For earlier API SDK versions, see API version.

2. API calling example

#

Before using an API, you need to start the API client. To start the client, you need to set parameters, such as regionId, accessKey, and secretKey. The following shows an example.

  1. /**
  2. *The endpoint of the API, which is set to the target region.
  3. */
  4. String regionId = "XXXXX";
  5. /**
  6. *The AccessKeyId you created in the Alibaba Cloud console for identity authentication.
  7. */
  8. String accessKey = "XXXXXXXXXXXXXXXXX";
  9. /**
  10. *The AccessKeySecret you created in the Alibaba Cloud console for identity authentication.
  11. */
  12. String secretKey = "XXXXXXXXXXXXXXXXX";
  13. IClientProfile profile= DefaultProfile.getProfile(regionIdaccessKeysecretKey);
  14. IAcsClient iAcsClient= new DefaultAcsClient(profile);
  15. //Construct request objects: Take TopicList as an example. Different request objects are constructed for different APIs.
  16. OnsTopicListRequest request = new OnsTopicListRequest();
  17. request.setPreventCache(System.currentTimeMillis()); //The current timestamp.
  18. OnsTopicListResponse response = iAcsClient.getAcsResponse(request);
  19. System.out.println(JSON.toJSONString(response));

Description

Parameter Description
regionId The region where the API gateway resides.
accessKey The AccessKeyId obtained in the Alibaba Cloud console.
secretKey The AccessKeySecret obtained in the Alibaba Cloud console.
productName The name of the API. Enter Ons.
domain The domain of the API endpoint, which complies with ONS. ${RegionId}.aliyuncs.com rule. The region corresponding to the domain must be consistent with the region corresponding to the region ID.

Regions and endpoints

The following table lists API regions supported by RocketMQ as well as corresponding region IDs and domains.

Region name regionId domain
Internet mq-internet-access ons.mq-internet-access.aliyuncs.com
China (Hangzhou) cn-hangzhou ons.cn-hangzhou.aliyuncs.com
China (Shanghai) cn-shanghai ons.cn-shanghai.aliyuncs.com
China (Beijing) cn-beijing ons.cn-beijing.aliyuncs.com
China (Zhangjiakou) cn-zhangjiakou ons.cn-zhangjiakou.aliyuncs.com
China (Shenzhen) cn-shenzhen ons.cn-shenzhen.aliyuncs.com
Hong Kong cn-hongkong ons.cn-hongkong.aliyuncs.com
Singapore ap-southeast-1 ons.ap-southeast-1.aliyuncs.com
Australia (Sydney) ap-southeast-2 ons.ap-southeast-2.aliyuncs.com
Malaysia (Kuala Lumpur) ap-southeast-3 ons.ap-southeast-3.aliyuncs.com
Japan (Tokyo) ap-northeast-1 ons.ap-northeast-1.aliyuncs.com
Germany (Frankfurt) eu-central-1 ons.eu-central-1.aliyuncs.com
UK (London) eu-west-1 ons.eu-west-1.aliyuncs.com
US (Virginia) us-east-1 ons.us-east-1.aliyuncs.com
US (Silicon Valley) us-west-1 ons.us-west-1.aliyuncs.com
India (Mumbai) ap-south-1 ons.ap-south-1.aliyuncs.com
UAE (Dubai) me-east-1 ons.me-east-1.aliyuncs.com
East China 1 Finance cn-hangzhou-finance ons.cn-hangzhou-finance.aliyuncs.com
South China 1 Finance cn-shenzhen-finance-1 ons.cn-shenzhen-finance-1.aliyuncs.com
China East 2 Finance cn-shanghai-finance-1 ons.cn-shanghai-finance-1.aliyuncs.com