Topic Model

Last updated: 2020-02-26 20:23:05

PDF

The prerequisite for publishing topic messages is that a subscriber must first subscribe a topic. If there is no subscriber, the message in the topic cannot be delivered. Thus, the publishing operation makes no sense.

1. Creating a Topic

    endpoint='' //CMQ 
    secretId ='' //  ID , key
    SecretKey
    account = Account(endpoint,secretId,secretKey)
    "topicName": "testtopic",
    my_topic = account.get_topic(topicName)
    topic_meta = TopicMeta()
    my_topic.create(topic_meta)

You can view the created topic from the console. Where QPS = 5000, which indicates the upper limit of the frequency of calling the same API. The default is 5000 / s. If you need to increase the upper limit of QPS, you can go to Submit a ticket Give us feedback.

2. Publishing a Message

You can send publish messages through SDK or the console.

Send messages using SDK

    'message' : message
    message.msgBody = "this is a test message"
    my_topic.publish_message(message)

Use the console publish message

Topic currently supports message filtering, that is, message labels and message types, which are used to distinguish message classifications under the Topic of a CMQ. MQ allows consumers to filter messages by tags, ensuring that consumers end up consuming only the types of messages they care about. This function is not enabled by default, and when it is not enabled, all messages are sent to all subscribers; after adding the tag, subscribers will only receive messages with the tag. The message filtering tag describes the label of message filtering in the subscription (only messages with consistent labels are pushed). A single label is no more than 16 characters, and a single Message can add up to 5 tags.
Topic currently supports both tag filtering and routingKey filtering.

Publishing messages in batch:

    vmsg = []
    for i in range(6):
    'message' : message
    message.msgBody = "this is a test message"
    vmsg.append(message)

    my_topic.batch_publish_message(vmsg)

3. Message Processing

Topic and publish will automatically push the message to the subscription after the message. When the push fails, there are two retry strategies:
Backoff retry: Retry 3 times with a random interval between 10 and 20 seconds. After that, the message will be discarded for the subscriber, and will not try again;

  • Decay exponential retry : retry 176 times, the total retry time is 1 day, and the interval is: 2 ^0, 2^ 1,. , 512, 512,. , 512 seconds. The default is the recession index retry strategy.

Using a Queue to Process Messages

Subscribers can enter a Queue to receive published messages.

    subscription_name = "subsc-test"
    my_sub = my_account.get_subscription(topic_name, subscription_name)
    subscription_meta = SubscriptionMeta()
    # Enter the subscription name. Here is the queue name
    subscription_meta.Endpoint = "queue name  "
    subscription_meta.Protocal = "queue"
    my_sub.create(subscription_meta)

Other Means to Process Messages

Besides Queue, subscribers can also process messages by other means.

4. Routing Match

Binding key and Routing key are used at the same time, fully compatible with rabbitmq topic matching mode. The Routing key for sending messages is in the messages sent by the client. The Binding key for subscription creation is used to bind the topic and the subscriber.

Usage Restrictions:

  • The number of Binding key does not exceed 5. A single binding key is ≤ 64 bytes in length and is used to indicate the routing path for sending messages, with a maximum of 15 ".", that is, a maximum of 16 phrases.
  • The number of Routing key consists of 1 string. A single Routing key is ≤ 64 bytes in length and is used to indicate the routing path for sending messages, with a maximum of 15 ".", that is, a maximum of 16 phrases.

Wildcard description:
:: (Asterisk) can be a substitute for a word (a sequence of alphabetic string)

(Pound sign) can be used to match one or more characters

Special case of rabbitmq: If routing_key is an empty string, * cannot be matched, but # can.

Example:
Subscribers to. * .0 "receive all messages for" 1.any characters.0 ".
Subscribers to "1.room.0" receive all messages for "1.2.3.4.4.2.2.0". (It can be any elements in between.)

Enabling Route Matching

    endpoint='' //CMQ 
    secretId ='' //  ID , key
    SecretKey
    account = Account(endpoint,secretId,secretKey)
    "topicName": "testtopic",
    my_topic = account.get_topic(topicName)
    topic_meta = TopicMeta()
    topic_meta.filterType = =2 //
    If filterType = 1, a tag is used for filtering.
    my_topic.create(topic_meta)

    subscription_name = "subsc-test"
    my_sub = my_account.get_subscription(topic_name, subscription_name)
    subscription_meta = SubscriptionMeta()
    Enter the subscription name. Here is the queue name
    subscription_meta.Endpoint = "queue name  "
    subscription_meta.Protocal = "queue"
    subscription_meta.bindingKey=[1.*.0]  //
    receive the message
    my_sub.create(subscription_meta)

Publishing a Message

    'message' : message
    message.msgBody = "this is a test message"
    routingKey = '1.test.0' //my_sub
    my_topic.publish_message(message)