HTTP SDK

Last updated: 2020-06-08 16:09:10

    Note:

    This is a legacy API which has been hidden and will no longer be updated. We recommend using the new CMQ API 3.0 which is standardized and faster.

    Overview

    Tencent Cloud message queue CMQ currently supports Java, Python, PHP, and C + + SDK, more languages will be supported in the future. Developers are welcome to develop more language versions of SDK according to API instructions.
    Since it takes about 1 second for Assign resources and resources to be released, the current message queuing SDK has a 1 second delay when creating and deleting queues / topic. It is recommended to increase the interval between creation and deletion in the program to ensure a successful call.

    • To ensure the security of data transmission and provide you with more reliable services, the Tencent Cloud team will stop treating topic and Access as the public network HTTP of the queue at 23:59:59 on January 31st, 2019. It is recommended that you change the domain name of the public network API request to HTTPS.
    • You can modify the domain name requested by the public network interface as follows:
      Determine whether the Protocol used in the connected domain name is HTTP,. If so, replace it with HTTPS.
      For example: endpoint=http://cmq-topic-gz.api.qcloud.com The access method is HTTP Protocol Access Guangzhou topic public network domain name, modified as follows: endpoint=https://cmq-topic-gz.api.qcloud.com .

    Download URL

    The GitHub addresses of different language versions of SDK are as follows:

    GitHub address

    Notes

    Before using SDK, you should at least get the SecretIDSecretKey And endpoint (that is, which region the request is sent to, private network or public network). Endpoint is described below.

    Queue model

    Please refer to the following instructions to replace {$region} in the domain name with the corresponding region:

    • Domain name requested by public network API: https://cmq-queue-{$region}.api.qcloud.com
    • Domain name requested by private network API: http://cmq-queue-{$region}.api.tencentyun.com

      Topic model

    Please refer to the following instructions to replace {$region} in the domain name with the corresponding region:

    • Domain name requested by public network API: https://cmq-topic-{$region}.api.qcloud.com

    • Domain name requested by private network API: http://cmq-topic-{$region}.api.tencentyun.com

      If the business process is also deployed on Tencent Cloud's CVM CVM, it is strongly recommended to use Intra-region 's private network endpoint. For example, CVM, CVM of Tencent Cloud in Beijing, is recommended to use http://cmq-queue-bj.api.tencentyun.com . Because:

    • Intra-region, private network and Latency are even lower.

    • At present, the message queue for the public network downstream Traffic is to collect Traffic cost, with private network can save this part of cost.

    Description

    {$region} need to replace: gz (Guangzhou), sh (Shanghai), bj (Beijing), shjr (Shanghai Finance), szjr (Shenzhen Finance), hk (Hong Kong), cd (Chengdu), ca (North American), usw (Maxi), sg (Singapore with a specific region. The region value in the common parameters should be consistent with the region value of the domain name. If there is any inconsistency, the request will be sent to the region specified by the domain name region based on the region value of the domain name.

    Demo project use

    Prepare the Demo environment

    1. Install IDE
      You can install IntelliJ IDEA or Eclipse,. This article uses IntelliJ IDEA as an example.
      Please Download the IntelliJ IDEA Ultimate version And refer to the IntelliJ IDEA instructions for installation
    2. Download the Demo project
      Please Download the Demo project for CMQ-HTTP When you unzip it locally, you can see the new local cmq-java-sdk-master folder.

    Configure the Demo project

    1. Create Resource
      You need to create the required message queuing resources in the console, including CMQ queue name, SecretID, SecretKey.
      For the specific creation process, please refer to Queue model Getting Started and Topic model Getting Started .
    2. Import Demo project files
      Open the folder in the Start up interface of IDEA.

      After opening the folder, the Demo project file is stored in the /src/main/java/com/qcloud/cmq/example Under the folder.
    3. Configure Demo parameters
      Modify file request address and key peer. Take Producer as an example, the configuration is as follows:
    String secretId="Acquired SecretID";
    String secretKey="Acquired SecretKey";
    String endpoint = "https://cmq-topic-{$region}.api.qcloud.com";
    String queueName = "test";

    Select "Create queue" and "use existing queue":

    //Create a new queue and set properties
    QueueMeta meta = new QueueMeta();
    meta.pollingWaitSeconds = 10;
    meta.visibilityTimeout = 10;
    meta.maxMsgSize = 1048576;
    meta.msgRetentionSeconds = 345600;
    Queue queue = account.createQueue(queueName,meta);
    //Use the console existing queue
    Queue queue = account.getQueue(queueName);

    This Demo uses the existing queue of the current account. If you need to select the Create queue, change the queueName to the name of the queue to be created, and then find the Create queue-related code to cancel the comments.
    The comment code is a common operation, the "delete" operation should be used with caution, and the configuration in other classes is referred to the Producer class.

    Run Demo

    Send and receive messages using queue model

    First run the Producer class to send the message, and then run the Consumer class to accept the message.
    Example of sending message code:

    String msg = "hello!";
    String msgId = queue.sendMessage(msg);
    System.out.println("==> send success! msg_id:" + msgId);

    Example of receiving message code:

    Message msg = queue.receiveMessage(10);

    Send and receive messages using topic model

    Run the TopicDemo class, and topic's model requests domain name reference. Topic model request domain name .
    Publish message example:

    String msg = "hello!";
    String msgId = topic.publishMessage(msg);

    Example of processing messages:

    String queueName = "test";
    String subscriptionName = "sub-test";
    String Endpoint = queueName;
    String Protocol = "queue";
    account.createSubscribe(topicName,subscriptionName, Endpoint, Protocol);

    When creating a subscriber, Enter uses a queue to process messages.

    Was this page helpful?

    Was this page helpful?

    • Not at all
    • Not very helpful
    • Somewhat helpful
    • Very helpful
    • Extremely helpful
    Send Feedback
    Help