Getting Started

Last updated: 2020-02-26 20:18:31

PDF

Cloud Message Queue (CMQ) is a distributed message queuing service that provides a reliable message-based asynchronous communication between distributed applications or components of an application. Each message is stored in highly available and highly reliable queues. Multiple processes can read/write from/to a queue at the same time without interfering with each other.
CMQ provides four kinds of SDK,. This article takes Python as an example.

1. Introduction to Python SDK

For ease of use, CMQ classifies users' actions, queue operations, and topic operations into the following categories:
Account: To encapsulate account secretId and secretKey. Users can create/delete queues, topics and subscriptions, and view these objects;
Queue: To send/receive messages and view queue setting attributes;
Topic: To publish messages and view topic setting attributes and subscribers;
Cmq_client: Users can set some attributes for connection from client to server, such as whether to enable log writing, connection timeout and persistent connection.

Please note that all the categories are non-thread-safe. If you want to use it for multi-threading, you'd better instantiate your object for each thread.

Queue Model

The queue here is somewhat different from the Queue defined in the data structure. Queues in the data structure operate strictly according to FIFO, and distributed queues here do not have strict FIFO (a dedicated FIFO product will be introduced later). The queue here is equivalent to a high-performance, high-capacity, highly reliable container, which can produce messages shipping to enter, or take messages out for consumption. The queue has its own property settings when initializing. Properties and their meanings are as follows:

Attribute DESCRIPTION
MaxMsgHeapNum Maximum number of heap messages. The number of messages that can be stored in the queue. This attribute represents the storage and heap capabilities of the queue.
PollingWaitSeconds Long-polling Waiting Time for Message Receipt. Value range: 0-30 seconds. This time setting represents the default time to wait for a message to be received when consuming a message.
For example, if it is set to 10, when consuming a message, if there is no message, the default will wait for 10s to return; if there is a message, it will return immediately.
You can also set a custom wait time when receiving a message without using the property values of the queue.
VisibilityTimeout Message visibility timed out.
When the message is obtained by the consumer, there will be an invisible time during which other consumers cannot get the message. The value range is 1-43200 seconds (that is, within 12 hours), and the default value is 30.
MaxMsgSize Max Message Length. The value range is 1024-1048576Byte (that is, 1K-1024K), and the default value is 65536.
MsgRetentionSeconds The message retention period, that is, the message storage time in the queue. Value range is 60 minute-15 days). 1296000 seconds (1 minute-15 days).) Default is 345600 (4 days).
CreateTime The time when the queue was created. Returns Unix timestamp, accurate to seconds.
LastModifyTime When the queue property was last modified. Returns Unix timestamp, accurate to seconds.
ActiveMsgNum Total number of messages in the queue whose status is Active (i.e. not Consumed). This is an approximate value.
InactiveMsgNum Total number of messages in the queue whose status is Inactive (i.e. being consumed). This is an approximate value.
RewindSeconds The maximum rewind time for messages in the queue. Value range is 0mur43200 seconds. 0 means message rewind is disabled.
RewindmsgNum Number of messages that has been deleted by calling the DelMsg API but are still within the rewind time.
MinMsgTime Minimum time for messages to be in the "not consumed" status (in seconds).
DelayMsgNum Number of delayed messages.

Please see Common Error Codes .

Topic Model

The topic model is similar to the Publish/Subscribe model in the design pattern. Topic is equivalent to the one who publishes the message and the subscriber of the topic is equivalent to the observer.

|Attribute|DESCRIPTION|-|-|-|
|MsgCount|Current number of messages in the topic (number of retained messages).|MaxMsgSize|Max Message Length. The value range is 1024-1048576Byte (that is, 1-1024K), and the default value is 65536.|MsgRetentionSeconds|The maximum available time of the message in the topic (in seconds). Whether or not the message has been retrieved after being pushed to the users, it will be deleted after the period of time specified in this parameter.|CreateTime|The founding time of topic. Returns Unix timestamp, accurate to seconds.|LastModifyTime|The time when the topic attribute was last modified. Returns Unix timestamp, accurate to seconds.|FilterType|Int|Indicate the filtering policy that a user selects when creating a subscription: filterType = 0 indicates that filterTag is used for filtering; filterType = 1 indicates that bindingKey is used for filtering.|

Please see Common Error Codes .