When you create a subscription to a topic, you can specify a queue for the endpoint to receive messages from the topic. After messages are published to the topic, Message Service (MNS) pushes the messages to the specified queue. You can pull the messages from the queue. This topic describes the scenarios of queue endpoints, the operations that you perform in the MNS console to specify a queue endpoint, and the sample code that you can use to send messages.

Scenarios

Send messages to queues in the following scenarios:

  • Elastic Compute Service (ECS) instances of subscribes do not need to remain online.
  • ECS instances of subscribes are deployed in VPCs.
  • Subscribers need to improve the efficiency of consuming messages under highly concurrent workloads.

Create a topic

Create a topic named MyTopic.

  1. In the top navigation bar of the MNS console, select a region, for example, China (Hangzhou).
  2. In the left-side navigation pane, select Topics. In the upper-right corner of the Topics page, click Create Topic.
  3. In the Create Topic dialog box that appears, specify the parameters and click OK.createtopic

Create a queue

To create a queue, perform the following steps. In this example, the name of the queue is MyQueue.

  1. In the top navigation bar of the MNS console, select a region, for example, China (Hangzhou).
  2. In the left-side navigation pane, select Queues.
  3. In the upper-right corner of the Queues page, click Create Queue.
  4. In the Create Queue dialog box that appears, specify the parameters and click OK.create a queue

Create a subscription

  1. On the Topics page, find MyTopic. In the Action column, click Subscription List.
  2. In the upper-right corner of the section that appears, click Subscribe.create a subscription 1
  3. In the Subscribe dialog box that appears, specify Queues for the Push Type parameter and specify a queue that you have created for the Endpoint parameter.
    Notice MNS does not check whether the specified queue exists. You must make sure that the specified queue has been created.
    队列推送订阅消息

Sample code

You can also use the SDK to push messages to the queue. The following section provides the sample code for the Java SDK.

client = new DefaultMNSClient(ServiceSettings.getMNSEndpoint(),
            ServiceSettings.getMNSAccessKeyId(),
            ServiceSettings.getMNSAccessKeySecret());

// Create a queue
String queueName = "JavaSDKTestQueue";
String subName = "JavaTestSub"
QueueMeta queueMeta = new QueueMeta();
queueMeta.setQueueName(queueName);
CloudQueue queue = client.createQueue(queueMeta);
// Create a topic
String topicName = "SampleTopic";
TopicMeta meta = new TopicMeta();
meta.setTopicName(topicName);
topic = client.createTopic(meta);
// Specify a queue endpoint in a subscription
String queueEndpoint = topic.generateQueueEndpoint(queueName);
SubscriptionMeta subMeta = new SubscriptionMeta();
subMeta.setSubscriptionName(subName);
subMeta.setNotifyStrategy(SubscriptionMeta.NotifyStrategy.EXPONENTIAL_DECAY_RETRY);
subMeta.setEndpoint(queueEndpoint);
String subUrl = topic.subscribe(subMeta);