A topic can publish messages only if it is subscribed to by at least one subscriber. If there are no subscribers, messages in the topic will not be delivered, and message publishing will be meaningless.
endpoint='' // CMQ domain name
secretId ='' // User ID and key
secretKey = ''
account = Account(endpoint,secretId,secretKey)
topicName = 'TopicTest8B'
my_topic = account.get_topic(topicName)
topic_meta = TopicMeta()
my_topic.create(topic_meta)
You can view the created topic in the console. Here, QPS is 5000, indicating that the default highest frequency for calling the same API is 5,000 calls per second. To increase the upper limit, please submit a ticket for application.
You can publish a message through the SDK or in the console.
message = Message()
message.msgBody = "this is a test message"
my_topic.publish_message(message)
Topics support message filtering. A tag, i.e., message tag or message type, is used to identify a message category under a topic in CMQ. A consumer can filter messages by tag so that it can consume only message types of interest to it. This feature is disabled by default. If it is disabled, all messages will be sent to all subscribers. If a tag is added, subscribers can receive only messages with the set tag. A message filter tag describes the tag used for message filtering in the subscription (only messages with the same tag can be pushed). One tag can contain up to 16 characters, and up to 5 tags can be added to one message.
Topics currently support filtering by tag and routingKey
.
Publish messages in batches:
vmsg = []
for i in range(6):
message = Message()
message.msgBody = "this is a test message"
vmsg.append(message)
my_topic.batch_publish_message(vmsg)
After a message is published by a topic, it will automatically be pushed to the subscription. If the push fails, there are two retry policies:
A subscriber can enter a queue so as to use it 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, which is a queue name here
subscription_meta.Endpoint = "queue name "
subscription_meta.Protocal = "queue"
my_sub.create(subscription_meta)
Subscribers can process messages on their own without using queues. For more information, please see Delivering Messages.
The binding key and routing key are used together and are compatible with the topic match mode of RabbitMQ. The routing key carried when a message is sent is added by the client and must be a string without a wildcard, and the binding key carried when a subscription relationship is created is the binding relationship between the topic and the subscriber.
Use limits:
.
, i.e., up to 16 phrases..
, i.e., up to 16 phrases.Wildcard description:
routing_key
is an empty string, it cannot match * but can match #.Example:
endpoint='' // CMQ domain name
secretId ='' // User ID and key
secretKey = ''
account = Account(endpoint,secretId,secretKey)
topicName = 'TopicTest'
my_topic = account.get_topic(topicName)
topic_meta = TopicMeta()
topic_meta.filterType = =2 // It indicates that routing matching will be used when messages are delivered to subscriptions
// If `filterType` is 1, tags are 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, which is a queue name here
subscription_meta.Endpoint = "queue name "
subscription_meta.Protocal = "queue"
subscription_meta.bindingKey=[1.*.0] // If the message tag is `[1.any characters.0]`, all subscribers will
// receive messages carrying this tag
my_sub.create(subscription_meta)
message = Message()
message.msgBody = "this is a test message"
routingKey = '1.test.0' // This message will be delivered to the address subscribed to by `my_sub`
my_topic.publish_message(message)
Was this page helpful?