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 and the sample code that you can use to push messages to a queue. This topic also describes how to specify a queue endpoint in the MNS console.

Scenarios

You can push messages to queues in the following scenarios:

  • The Elastic Compute Service (ECS) instances of subscriber clients do not need to remain online.
  • The ECS instances of subscriber clients are deployed in virtual private cloud (VPCs).
  • Subscriber clients need to increase concurrent workloads to consume messages at a higher speed.

Prerequisites

The following operations are performed:

Create a topic

To create a topic named MyTopic in the MNS console, perform the following steps:

  1. Log on to the MNS console.
  2. Select a region in the top navigation bar.
  3. In the left-side navigation pane, click Topics.
  4. In the upper-right corner of the Topics page, click Create Topic.
  5. In the Create Topicdialog box, enter a topic name and a maximum message size, and then click OK.

Create a queue

To create a queue named MyQueue in the MNS console, perform the following steps.

  1. Select a region in the top navigation bar.
  2. In the left-side navigation pane, click Queues.
  3. In the upper-right corner of the Queues page, click Create Queue.
  4. In the Create Queue dialog box, specify the parameters and click OK.

Create a subscription

  1. On the Topics page of the console, 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
  3. In the Subscribe dialog box, set the Push Type parameter to Queues, enter the topic name in the Endpoint field, and then click OK.
    Notice MNS does not check whether the specified queue exists. You must make sure that the specified queue is created.
    Push messages to queues

Sample code

You can also use the SDK to push messages to the queue. The following sample code shows how to push messages to the queue by using the SDK for Java.

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);