edit-icon download-icon

Step 4: Subscribe to messages

Last Updated: Jun 21, 2018

Once a message is successfully sent, the subscription party needs to be started for message subscription. This topic introduces how to complete message subscription by calling related protocols and development languages by taking TCP Java SDK as an example.

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

Subscribe to messages by calling TCP Java SDK

You can run the following sample code to start the subscription end and test the function of subscribing messages. Related parameters should be correctly set according to the instructions. Currently, the console provides sample codes of Java, C++, .NET, and PHP.

  1. import com.aliyun.openservices.ons.api.Action;
  2. import com.aliyun.openservices.ons.api.ConsumeContext;
  3. import com.aliyun.openservices.ons.api.Consumer;
  4. import com.aliyun.openservices.ons.api.Message;
  5. import com.aliyun.openservices.ons.api.MessageListener;
  6. import com.aliyun.openservices.ons.api.ONSFactory;
  7. import com.aliyun.openservices.ons.api.PropertyKeyConst;
  8. import java.util.Properties;
  9. public class ConsumerTest {
  10. public static void main(String[] args) {
  11. Properties properties = new Properties();
  12. // The consumer ID you have created on the MQ console
  13. properties.put(PropertyKeyConst.ConsumerId, "XXX");
  14. // Authentication AccessKey which has been created on Alibaba ECS Management Console
  15. properties.put(PropertyKeyConst.AccessKey, "XXX");
  16. // Authentication SecretKey which has been created on Alibaba ECS Management Console
  17. properties.put(PropertyKeyConst.SecretKey, "XXX");
  18. // Set a TCP access domain name (internet environment access of public cloud is listed here)
  19. properties.put(PropertyKeyConst.ONSAddr,
  20. "http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet");
  21. Consumer consumer = ONSFactory.createConsumer(properties);
  22. consumer.subscribe("TopicTestMQ", "*", new MessageListener() {
  23. public Action consume(Message message, ConsumeContext context) {
  24. System.out.println("Receive: " + message);
  25. return Action.CommitMessage;
  26. }
  27. });
  28. consumer.start();
  29. System.out.println("Consumer Started");
  30. }
  31. }

Check whether messages are successfully subscribed to

Once said steps have been completed, you can check whether the subscription end has been successfully started on the MQ console, i.e., whether a message is successfully subscribed to.

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

  2. Locate the Topic to be checked, and click Consumer Status in the right action options.
    If Online is displayed as Yes, it means that the subscription end has been successfully started. If the consumer status Online is displayed as No, it means that the consumption end has not been started or has failed to start.

When all the steps are completed, you can succeed in accessing the MQ service, and use MQ for message sending and receiving.

Thank you! We've received your feedback.