file

Broadcast pull message model

Message Service MNS Support the one to many pull message consumption model to meet the one to many subscription and active pull scenarios. This paper introduces how to efficiently use this model to realize parallel pulling and processing of messages by multiple consumers.

explain

This article uses the Java SDK as an example to introduce the broadcast pull message process. For other language SDKs, see New SDK reference (recommended)

prerequisite

background information

Message Service MNS It provides two models: Queue and Topic, which can basically meet most application scenarios.

  • The queue provides a one-to-one shared message consumption model, which uses the client pull mode.

  • The topic model provides a one to many broadcast message consumption model, which uses the server active push mode.

The advantage of push mode is that the instant performance is better, but the client address needs to be exposed to receive the message push from the server. In some cases, some information, such as the enterprise intranet, cannot expose the push address. I hope to use pull instead. although Message Service MNS This consumption model is not directly provided, but it can be combined with topics and queues to achieve a one to many pull message consumption model.

Solution

By creating a subscription, the topic first pushes the message to the queue, and then the consumer pulls the message from the queue. This can not only achieve one to many broadcast messages, but also avoid exposing the address of consumers. Message flow

Install Java dependency libraries

  1. Create a Java project in IDEA.

  2. stay pom.xml Add the following dependencies to the file to import the Java dependency library.

     <dependency> <groupId>com.aliyun.mns</groupId> <artifactId>aliyun-sdk-mns</artifactId> <version>1.1.9.2</version> </dependency>

Interface description

abreast of the times Java SDK(1.1.8) The CloudPullTopic in supports the above solutions by default. MNSClient provides the following interfaces to quickly create CloudPullTopic:

 public CloudPullTopic createPullTopic(TopicMeta topicMeta, Vector<String> queueNameList, boolean needCreateQueue, QueueMeta queueMetaTemplate) public CloudPullTopic createPullTopic(TopicMeta topicMeta, Vector<String> queueNameList)

The parameters are described as follows:

  • TopicMeta : parameter settings for creating themes.

  • QueueMeta : Create the parameter settings of the queue.

  • queueNameList : Specifies the list of queue names for the topic message push.

  • needCreateQueue queueNameList Whether it needs to be created.

  • queueMetaTemplate : Required for queue creation QueueMeta Parameter example.

Sample code

The example code of broadcast pull message is as follows:

 package doc; //Related classes for introducing Alibaba Cloud Message Service MNS import com.aliyun.mns.client. CloudAccount; import com.aliyun.mns.client. MNSClient; import com.aliyun.mns.model. QueueMeta; import com.aliyun.mns.model. TopicMeta; import com.aliyun.mns.client. CloudPullTopic; import com.aliyun.mns.model. TopicMessage; import com.aliyun.mns.model. RawTopicMessage; import com.aliyun.mns.model. Message; import com.aliyun.mns.client. CloudQueue; import com.aliyun.mns.common. ServiceException; import com.aliyun.mns.common. ClientException; import java.util. Vector; public class DemoTopicMessageBroadcast { public static void main(String[] args) { //Obtain the AccessKey ID, AccessKey Secret and MNS service endpoint of the AliCloud account. CloudAccount account = new CloudAccount( ServiceSettings.getMNSAccessKeyId(), ServiceSettings.getMNSAccessKeySecret(), ServiceSettings.getMNSAccountEndpoint()); MNSClient client = account.getMNSClient(); //Create a consumer list. Vector<String> consumerNameList = new Vector<String>(); String consumerName1 = "consumer001"; String consumerName2 = "consumer002"; String consumerName3 = "consumer003"; consumerNameList.add(consumerName1); consumerNameList.add(consumerName2); consumerNameList.add(consumerName3); QueueMeta queueMetaTemplate = new QueueMeta(); queueMetaTemplate.setPollingWaitSeconds(30); try{ //Create a theme. String topicName = "demo-topic-for-pull"; TopicMeta topicMeta = new TopicMeta(); topicMeta.setTopicName(topicName); CloudPullTopic pullTopic = client.createPullTopic(topicMeta, consumerNameList, true, queueMetaTemplate); //Publish messages. String messageBody = "broadcast message to all the consumers:hello the world."; //If the original message is sent, use getMessageBodyAsRawString to parse the message body. TopicMessage tMessage = new RawTopicMessage(); tMessage.setBaseMessageBody(messageBody); pullTopic.publishMessage(tMessage); //Receive messages. CloudQueue queueForConsumer1 = client.getQueueRef(consumerName1); CloudQueue queueForConsumer2 = client.getQueueRef(consumerName2); CloudQueue queueForConsumer3 = client.getQueueRef(consumerName3); Message consumer1Msg = queueForConsumer1.popMessage(30); if(consumer1Msg != null) { System.out.println("consumer1 receive message:" + consumer1Msg.getMessageBodyAsRawString()); } else{ System.out.println("the queue is empty"); } Message consumer2Msg = queueForConsumer2.popMessage(30); if(consumer2Msg != null) { System.out.println("consumer2 receive message:" + consumer2Msg.getMessageBodyAsRawString()); }else{ System.out.println("the queue is empty"); } Message consumer3Msg = queueForConsumer3.popMessage(30); if(consumer3Msg != null) { System.out.println("consumer3 receive message:" + consumer3Msg.getMessageBodyAsRawString()); }else{ System.out.println("the queue is empty"); } //Delete topic. pullTopic.delete(); } catch(ClientException ce) { System.out.println("Something wrong with the network connection between client and MNS service." + "Please check your network and DNS availability."); ce.printStackTrace(); } catch(ServiceException se) { se.printStackTrace(); } client.close(); } }

  • Introduction to this page (1)