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.
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.
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:
Prerequisites: You have installed the JDK on your local machine. If not, download and install it first.
In IntelliJ IDEA, select Import Project, and then select the folder mq-demo-master.
Select Import project from external model.
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.
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.
Configure two files: MqConfig class and common.xml.
- 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”;
- 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.
- Configure producer.xml.
<prop key="AccessKey">XXX</prop> <!-- enter values for these parameters -->
- Configure consumer.xml.
<bean id="consumer" class="com.aliyun.openservices.ons.api.bean.Consumerbean"
<entry key="GROUP_ID" value="XXX"/><!——Replace GROUP_ID with the group ID of the consumer.——>
<entry key="AccessKey" value="XXX"/><!——Replace AccessKey with the AccessKeyId of your account.——>
<entry key="SecretKey" value="XXX"/><!——Replace SecretKey with the AccessKeySecret of your account.——>
<entry key="ONSAddr" value="http://onsaddr-internet.aliyun.com/rocketmq/nsaddr4client-internet"/>
<property name="topic" value="XXX"/><!——Replace the topic with the one you created.——>
<property name="expression" value="*"/><!——The name of the message type. Separate multiple message types with "||".——>
Execute SimpleMQProducer to send messages.
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.
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.
- Execute ProducerClient to send messages.
- Execute ConsumerClient to receive messages.
Perform similar operations as above to view the results.
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.
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.
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.