edit-icon download-icon

Step 3: Send messages

Last Updated: Jun 20, 2018

Once you have created Topics in the console, you can send messages via the console or SDK/API. Sending messages via the console is used mainly for quickly verifying the availability of the Topics, while in a production scenario, it is suggested that you use SDK/API for message sending.

Send messages via the console

To send messages via the console, follow the steps below:

  1. Click Producers in the left-side navigation pane of the MQ console.

  2. Locate the Topic you just created in the list, and in the Actions column, click Send.

  3. In the Send Message dialog box, enter the message content, and click OK. The console will return a confirmation message and the corresponding message ID once the message is sent successfully.

Send messages via SDK/API

In a production scenario, it is suggested that you use SDK/API to send messages. This section demonstrates to you how to do it by using the Java SDK under the TCP protocol. If you want to use other protocols or development languages, see the related help documentation.

Send messages via TCP Java SDK

  1. Introduce the dependency through one of the following two methods:

    • Introduce the dependency through Maven:

      1. <dependency>
      2. <groupId>com.aliyun.openservices</groupId>
      3. <artifactId>ons-client</artifactId>
      4. <version>1.7.0.Final</version>
      5. </dependency>
    • Download a dependency JAR package: Download Link

    Note: Please refer to TCP access instruction for a TCP access point domain name.

  2. Set related parameters and run sample code according to the following instructions:

  1. import com.aliyun.openservices.ons.api.Message;
  2. import com.aliyun.openservices.ons.api.Producer;
  3. import com.aliyun.openservices.ons.api.SendResult;
  4. import com.aliyun.openservices.ons.api.ONSFactory;
  5. import com.aliyun.openservices.ons.api.PropertyKeyConst;
  6. import java.util.Properties;
  7. public class ProducerTest {
  8. public static void main(String[] args) {
  9. Properties properties = new Properties();
  10. // The producer ID you have created on the MQ console
  11. properties.put(PropertyKeyConst.ProducerId, "XXX");
  12. // Authentication AccessKey which has been created on Alibaba ECS Management Console
  13. properties.put(PropertyKeyConst.AccessKey,"XXX");
  14. // Authentication SecretKey which has been created on Alibaba ECS Management Console
  15. properties.put(PropertyKeyConst.SecretKey, "XXX");
  16. // Set a TCP access domain name (internet access of public cloud is listed here)
  17. properties.put(PropertyKeyConst.ONSAddr,
  18. "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
  19. Producer producer = ONSFactory.createProducer(properties);
  20. // A start method must be called once before message sending to start a producer
  21. producer.start();
  22. //Send messages recurrently
  23. while(true){
  24. Message msg = new Message( //
  25. // The Topic which has been created on the console, i.e., a Topic name of the message
  26. "TopicTestMQ",
  27. // Message Tag,
  28. // It can be understood as a Tag in Gmail used for reclassifying the message so as to facilitate the consumer to specify a filter condition to implement a filter in the MQ broker
  29. "TagA",
  30. // Message Body
  31. // It may be any data in binary form, and MQ will make no intervention
  32. // Compatible serialization and deserialization methods that need to be negotiated by the producer and the consumer
  33. "Hello MQ".getBytes());
  34. // Set a critical service property representing the message, and try to keep the critical service property globally unique, such that you can query and resend the message via the MQ console when you cannot receive the message normally
  35. // Note: No setting will affect the normal sending and receiving of messages
  36. msg.setKey("ORDERID_100");
  37. // Message sending will succeed as long as an exception is not thrown
  38. // Print the message ID to facilitate querying the message sending status
  39. SendResult sendResult = producer.send(msg);
  40. System.out.println("Send Message success. Message ID is: " + sendResult.getMessageId());
  41. }
  42. // The producer object can be destroyed before exiting the application
  43. // Note: it's OK if the producer object has not been destroyed
  44. producer.shutdown();
  45. }
  46. }

Check whether a message is successfully sent

Once a message is sent, you can check its sending status in the console by following the steps below:

  1. On the left-side navigation pane of the MQ console, choose Message Query.

  2. On the Message Query page, select the By Message ID tab.

  3. In the search box, enter the message ID returned after the message is sent, and click Search to query the sending status of the message.

    “Storage time” indicates the time period for which the MQ server stored the message. when the MQ broker stores the message. If the message can be queried out, it means that the message has been successfully sent to the server.

Note: This section demonstrates the scenario where MQ is first used, when the consumer has not been started yet. Therefore, the message status shows that there is not any consumption data available. To start the consumer and subscribe to messages, see Step 4: Subscribe to messages. For more information on the message status, see Message Query.

Thank you! We've received your feedback.