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.
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.
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
.The GitHub addresses of different language versions of SDK are as follows:
Before using SDK, you should at least get the SecretID 、 SecretKey And endpoint (that is, which region the request is sent to, private network or public network). Endpoint is described below.
Please refer to the following instructions to replace {$region} in the domain name with the corresponding region:
https://cmq-queue-{$region}.api.qcloud.com
http://cmq-queue-{$region}.api.tencentyun.com
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.
{$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.
/src/main/java/com/qcloud/cmq/example
Under the folder.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.
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);
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?