All Products
Search
Document Center

Demo project (TCP)

Last Updated: Apr 01, 2019

This demo helps engineers who are new to RocketMQ to build a RocketMQ test project step by step. The demo program provides test codes in Java mode for normal messages, transactional messages, and scheduled messages, as well as Spring configuration.

Prepare the environment

Step 1: Install IDE

You can use IntelliJ IDEA or Eclipse. IntelliJ IDEA is used in this example.

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

Step 2: Download the demo project

Download the demo project from https://github.com/AliwareMQ/mq-demo to a local machine and decompress the .zip file. Then, a folder named Aliware-MQ-demo-master is created on the local machine.

Set the demo project as follows:

Configure the demo project

Step 1: Import the demo project to IntelliJ IDEA

Prerequisites: You have installed the JDK on your local machine. If not, download and install it first.

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

    demo import

  2. Select Import project from external model.

    idea-import-2

  3. Click Next until the project is imported. The dependent JAR package needs to be loaded to the demo project. Therefore, it takes two to three minutes to import the project.

Step 2: Create resources

Create the required resources in the RocketMQ console, including a RocketMQ instance, a topic, a group ID, and an AccessKey for authentication.

For more information and operation instructions, see Step 2: Create resources in Quick start for primary accounts.

Step 3: Configure the demo

Configure two files: MqConfig class and common.xml.

  1. Configure the MqConfig class as follows:
  • public static final String TOPIC = “the topic you created”;
  • public static final String GROUP_ID = “the group ID you created”;
  • public static final String ACCESS_KEY = “the AccessKeyId of your Alibaba Cloud account”;
  • public static final String SECRET_KEY = “the AccessKeySecret of your Alibaba Cloud account”;
  • public static final String NAMESRV_ADDR = “the TCP endpoint of your RocketMQ instance that is displayed as TCP Endpoint in the Endpoint Information area on the Instances page”;

Note:

  • For more information about how to create an AccessKey (including an AccessKeyId and an AccessKeySecret), see Create an AccessKey.
  • You can also use the AccessKey of the RAM sub-account if it is granted with the permissions of the topic you created.
  1. Configure producer.xml.
  1. <props>
  2. <prop key="AccessKey">XXX</prop> <!-- enter values for these parameters -->
  3. <prop key="SecretKey">XXX</prop>
  4. <prop key="GROUP_ID">XXX</prop>
  5. <prop key="Topic">XXX</prop>
  6. <prop key="NAMESRV_ADDR">XXX</prop>
  7. </props>
  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="GROUP_ID" value="XXX"/><!——Replace GROUP_ID with the group ID of the consumer.——>
  6. <entry key="AccessKey" value="XXX"/><!——Replace AccessKey with the AccessKeyId of your account.——>
  7. <entry key="SecretKey" value="XXX"/><!——Replace SecretKey with the AccessKeySecret of your account.——>
  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 the topic with the one you created.——>
  17. <property name="expression" value="*"/><!——The name of the message type. Separate multiple message types with "||".——>
  18. </bean>
  19. </key>
  20. </entry>
  21. </map>
  22. </property>
  23. </bean>

Run the demo project

Start sending and receiving messages in Main mode

  1. Execute SimpleMQProducer to send messages.

  2. Log on to the RocketMQ console. In the left-side navigation pane, choose Message Query > By Topic. On the page that is displayed, select the topic you want to query. The query result indicates that the messages have been sent to the topic.

  3. Execute SimpleMQConsumer to receive messages. The log indicating that the messages are received is printed. The class needs to be initialized, which takes several seconds. Initialization does not often occur in the production environment.

In the RocketMQ console, choose Consumers > Consumer Status. The system shows that the started consumer is online and the subscriptions are consistent.

Start sending and receiving messages in Spring mode

  1. Execute ProducerClient to send messages.
  2. Execute ConsumerClient to receive messages.

Perform similar operations as above to view the results.

Send transactional messages

Execute SimpleTransactionProducer to send messages.

Note: LocalTransactionCheckerImpl is an API that you can call to check local transactions. It is used for verifying transactions. For more information, see Send and receive transactional messages.

Send and receive ordered messages

Execute SimpleOrderConsumer to receive messages.

Execute SimpleOrderProducer to send messages.

Note: In this mode, messages are delivered and consumed in order. For more information, see Send and receive ordered messages.

Send scheduled/delayed messages

Execute MQTimerProducer to send messages. Messages are delivered in 3s.

Note: You can specify an exact delivery time, which is up to 40 days. For more information, see Send and receive scheduled messages.