All Products
Document Center

Broadcast Message

Last Updated: Jan 30, 2019


Alibaba Cloud Message Service provides the queue and topic models. The queue provides a one-to-multiple sharing message consumption model, and adopts active pull by the client. The topic provides a one-to-multiple broadcast message consumption model, and adopts active push by the server. The preceding two models can meet requirements in most application scenarios.

The push mode features good instant performance. However, the client’s address must be exposed to receive messages pushed from the server. In some circumstances, for example, in the intranet of an enterprise, the push address cannot be exposed. The pull mode is required accordingly. The one-to-multiple pull-based message consumption model can be enabled based on the queue and topic, although Message Service does not provide this consumption model. The solution is as follows:


The topic pushes the message to a queue, from which the consumer pulls the message. In this way, one-to-multiple broadcast messaging is enabled, and the consumer’s address is not exposed, as shown in the following figure.

broadcast message

Interface description

CloudPullTopic in the latest Java SDK supports the above solution by default. Where,MNSClient provides the following two interfaces for you to rapidly create CloudPullTopic:

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

Where, TopicMeta specifies the meta settings for creating a topic; queueNameList specifies the queue name list to be pushed by the topic; needCreateQueue specifies whether queueNameList needs to be created; and queueMetaTemplate specifies the queue meta parameter required for creating a queue.

Demo code

  1. CloudAccount account = new CloudAccount(accessKeyId, accessKeySecret, endpoint);
  2. MNSClient client = account.getMNSClient();
  3. // build consumer name list.
  4. Vector<String> consumerNameList = new Vector<String>();
  5. String consumerName1 = "consumer001";
  6. String consumerName2 = "consumer002";
  7. String consumerName3 = "consumer003";
  8. consumerNameList.add(consumerName1);
  9. consumerNameList.add(consumerName2);
  10. consumerNameList.add(consumerName3);
  11. QueueMeta queueMetaTemplate = new QueueMeta();
  12. queueMetaTemplate.setPollingWaitSeconds(30);
  13. try{
  14. //producer code:
  15. // create pull topic which will send message to 3 queues for consumer.
  16. String topicName = "demo-topic-for-pull";
  17. TopicMeta topicMeta = new TopicMeta();
  18. topicMeta.setTopicName(topicName);
  19. CloudPullTopic pullTopic = client.createPullTopic(topicMeta, consumerNameList, true, queueMetaTemplate);
  20. //publish message and consume message.
  21. String messageBody = "broadcast message to all the consumers:hello the world.";
  22. // if we sent raw message,then should use getMessageBodyAsRawString to parse the message body correctly.
  23. TopicMessage tMessage = new RawTopicMessage();
  24. tMessage.setBaseMessageBody(messageBody);
  25. pullTopic.publishMessage(tMessage);
  26. // consumer code:
  27. //3 consumers receive the message.
  28. CloudQueue queueForConsumer1 = client.getQueueRef(consumerName1);
  29. CloudQueue queueForConsumer2 = client.getQueueRef(consumerName2);
  30. CloudQueue queueForConsumer3 = client.getQueueRef(consumerName3);
  31. Message consumer1Msg = queueForConsumer1.popMessage(30);
  32. if(consumer1Msg != null)
  33. {
  34. System.out.println("consumer1 receive message:" + consumer1Msg.getMessageBodyAsRawString());
  35. }else{
  36. System.out.println("the queue is empty");
  37. }
  38. Message consumer2Msg = queueForConsumer2.popMessage(30);
  39. if(consumer2Msg != null)
  40. {
  41. System.out.println("consumer2 receive message:" + consumer2Msg.getMessageBodyAsRawString());
  42. }else{
  43. System.out.println("the queue is empty");
  44. }
  45. Message consumer3Msg = queueForConsumer3.popMessage(30);
  46. if(consumer3Msg != null)
  47. {
  48. System.out.println("consumer3 receive message:" + consumer3Msg.getMessageBodyAsRawString());
  49. }else{
  50. System.out.println("the queue is empty");
  51. }
  52. // delete the pullTopic.
  53. pullTopic.delete();
  54. }catch(ClientException ce)
  55. {
  56. System.out.println("Something wrong with the network connection between client and MNS service."
  57. + "Please check your network and DNS availablity.");
  58. ce.printStackTrace();
  59. }
  60. catch(ServiceException se)
  61. {
  62. se.printStackTrace();
  63. }
  64. client.close();

You can get more MNS service error code in Error codes.