edit-icon download-icon

Demo project

Last Updated: Dec 03, 2018

The main purpose of this demo is to help engineers who are new to MQ to build a MQ test project step by step. The demo program uses Java as an example, including test code for standard message, transactional message, and scheduled message, as well as related Spring configuration.

Prepare the environment

Step 1: Install IDE

You can use IDEA or Eclipse. The document uses IDEA as an example.

Download IDEA Ultimate Version from https://www.jetbrains.com/idea/, and install it according to IDEA instructions.

Step 2: Download MQ Demo Project

Download MQ demo project from https://github.com/AliwareMQ/mq-demo to your local disk and decompress the ZIP file, then you can see a folder named Aliware-MQ-demo-master has been created on your local disk.

Demo project setting includes the following steps:

Set up the project

Step 1: Import MQ Demo Project to IDEA

Prerequisite: If JDK is not installed on your local disk, please download and install it first.

  1. In IDEA, select Import Project, and select folder mq-demo-master.

    IDEA Import Demo

  2. Select Import Type.

    idea-import-2

  3. Click Next until the import is completed. Dependent JAR package needs to be downloaded for the demo project, so the import process will take 2-3 minutes.

Step 2: Create MQ Resources

Create Topic, Producer ID (PID), and Consumer ID (CID) in the MQ console.

  1. Log on to the MQ console.

  2. Select Internet region (default), click Create Topic on the right side, enter topic name (as unique as possible to prevent repetition), select message type, enter description, and click OK.

  3. Click Create Producer in the Actions column to create a producer ID.

  4. Click Create Consumer in the Actions column to create a consumer ID.

For detailed information, see Step 2: Create resources in Quick start guide for primary accounts.

Step 3: Configure MQ Demo

3 files need to be configured: MqConfig class, producer.xml, and consumer.xml.

  1. Configure MqConfig class as follows.

    • public static final String TOPIC = “the topic just created”;
    • public static final String PRODUCER_ID = “the PID just created”;
    • public static final String CONSUMER_ID = “the CID just created”;
    • public static final String ACCESS_KEY = “AK of your Alibaba Cloud Account”;
    • public static final String SECRET_KEY = “SK of your Alibaba Cloud Account”;

    Log on your Alibaba Cloud Account, and click AccessKeys to obtain accesskeys. AK refers to Access Key ID and SK Access Key Secret.

    Note: You can also use the AK and SK of the RAM sub-account if it is authorized in the MQ console when the topic is created for the primary account.

  2. Configure producer.xml.

  1. <bean id="producer" class="com.aliyun.openservices.ons.api.bean.ProducerBean"
  2. init-method="start" destory-method="shutdown">
  3. <property name="properties">
  4. <map>
  5. <entry key="ProducerId" value="XXX"/><!——Replace CID——>
  6. <entry key="AccessKey" value="XXX"/><!——Replace ACCESS_KEY——>
  7. <entry key="SecretKey" value="XXX"/><!——Replace SECRET KEY——>
  8. <entry key="ONSAddr" value="http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet"/>
  9. </map>
  10. </property>
  11. </bean>
  1. Configure consumer.xml.
  1. <bean id="consumer" class="com.aliyun.openservices.ons.api.bean.Consumerbean"
  2. init-method="start" destory-method="shutdown">
  3. <property name="properties">
  4. <map>
  5. <entry key="ConsumerId" value="XXX"/><!——Replace CID——>
  6. <entry key="AccessKey" value="XXX"/><!——Replace ACCESS_KEY——>
  7. <entry key="SecretKey" value="XXX"/><!——Replace SECRET KEY——>
  8. <entry key="ONSAddr" value="http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet"/>
  9. </map>
  10. </property>
  11. <property name="subscriptionTable">
  12. <map>
  13. <entry value-ref="messageListener">
  14. <key>
  15. <bean class="com.aliyun.openservices.ons.api.bean.Subscription">
  16. <property name="topic" value="XXX"/><!——Replace Topic——>
  17. <property name="expression" value="*"/><!——MessageType: use "||" to split multiple messageTypes——>
  18. </bean>
  19. </key>
  20. </entry>
  21. </map>
  22. </property>
  23. </bean>

Run the demo

Start Sending and Receiving Messages in Main Method

  1. Run SimpleMQProducer class to send a message.

  2. Log on to the MQ console, in the left-side navigation pane select Message Query > By Topic, and select the topic name to be queried. You can see that the message has been sent to the topic.

  3. Run SimpleMQConsumer class to receive a message. You can see the log that shows the message is received and printed. The initialization will take seconds, which doesn’t often occur in the production environment.

In the MQ console, go to Consumers > Consumer Status, and you can see the started consumer is online and the subscription relationship is consistent in the Consumer Status Information section.

Start Sending and Receiving Messages in Spring Method

  1. Run MQProducer4Spring class to send a message.
  2. Run MQConsumer4Spring class to receive a message.

The process for viewing the result is similar to that shown above.

Send Transactional Messages

Run SimpleTransactionProducer class to send a message.

Note: LocalTransactionCheckerImpl class is the check interface class for local transactions. It is used for verifying transactions. For detailed information, see Send and Receive Transactional Message.

Send and Receive Ordered Messages

Run SimpleOrderConsumer class to receive a message.

Run SimpleOrderProducer class to send a message.

Note: Message delivery and consumption is processed in order. For detailed information, see Send and Receive Ordered Message.

Send Scheduled/Delayed Messages

Run MQTimerProducer class to send a message. The message will be delivered in 3 seconds.

Note: You can specify an exact delivery time, which is up to 40 days. For detailed information, see Send and Receive Scheduled Message.

Thank you! We've received your feedback.