This topic describes how to build a Message Queue for Apache RocketMQ test project by using the TCP client SDK for Java. The demo project provides sample
code for normal messages, ordered messages, transactional messages, scheduled messages,
and delayed messages. The demo project also provides Spring configurations for testing.
Prerequisites
- The integrated development environment (IDE) is installed.
You can use IntelliJ IDEA or Eclipse. IntelliJ IDEA is used in the examples in this
topic.
Make sure that IntelliJ IDEA Ultimate Edition is downloaded and installed. For more
information, visit the download page.
- The demo project package is downloaded.
Download the demo project package and decompress the contents to your local machine.
A folder named mq-demo-master is created. The folder contains the sample code for Java, Spring, and Spring Boot.
For more information, see rocketmq-demo.
- The Java Development Kit (JDK) is installed. For more information, see Java Downloads.
Configure the demo project
- Import the demo project files to IntelliJ IDEA.
- Create resources.
- Configure the demo.
You need to configure the MqConfig
class and common.xml file based on the information about resources created in step 2.
- Configure the
MqConfig
class by referring to the following sample code: public static final String TOPIC = "The topic that you created";
public static final String GROUP_ID = "The group ID that you created";
public static final String ORDER_TOPIC = "The topic that you created to send and receive ordered messages";
public static final String GROUP_ID = "The group ID that you created to send and receive ordered messages";
public static final String ACCESS_KEY = "The AccessKey ID is used as an identifier of Alibaba Cloud service users. For more information, see Create an AccessKey pair.";
public static final String SECRET_KEY = "The AccessKey Secret is used to verify the identity of Alibaba Cloud service users. For more information, see Create an AccessKey pair.";
public static final String TAG = "The tag attribute that you configure for the message";
public static final String NAMESRV_ADDR = "The TCP endpoint of your Message Queue for Apache RocketMQ instance. You can view the endpoint in the Endpoints tab on the Instance Details page of the Message Queue for Apache RocketMQ console;
Note
- You can also use the AccessKey pair of the RAM user who is granted the permissions
on the topic that you created.
- For more information, see Methods and parameters.
- Configure the common.xml file.
<props>
<prop key="AccessKey">XXX</prop> <!-- Modify the values based on your resources. -->
<prop key="SecretKey">XXX</prop>
<prop key="GROUP_ID">XXX</prop>
<prop key="Topic">XXX</prop>
<prop key="NAMESRV_ADDR">XXX</prop>
</props>
Run the demo through main classes
- Send messages.
- Send normal messages:
- Java: Run the
SimpleMQProducer
class.
- Spring: Run the
ProducerClient
class.
- Spring Boot: Run the
ProducerClient
class.
- Send transactional messages:
- Send ordered messages:
- Java: Run the
SimpleOrderProducer
class.
- Spring: Run the
OrderProducerClient
class.
- Spring Boot: Run the
OrderProducerClient
class.
Ordered messages are sent and consumed in first-in-first-out (FIFO) order. For more
information, see Send and subscribe to ordered messages.
- Send scheduled messages and delayed messages: Run the
MQTimerProducer
class. Messages are delivered after a delay of 3 seconds.
You can specify the delivery time for these messages. The message delivery can be
delayed for up to 40 days. For more information, see Send and subscribe to scheduled messages.
- Receive messages.
- Receive normal messages:
- Java: Run the
SimpleMQConsumer
class.
- Spring: Run the
ConsumerClient
class.
- Spring Boot: Run the
ConsumerClient
class.
- Receive transactional messages:
- Java: Run the
SimpleMQConsumer
class.
- Spring: Run the
ConsumerClient
class.
- Spring Boot: Run the
ConsumerClient
class.
- Receive ordered messages:
- Java: Run the
SimpleOrderConsumer
class.
- Spring: Run the
OrderConsumerClient
class.
- Spring Boot: Run the
OrderConsumerClient
class.
- Receive scheduled messages and delayed messages: Run the
SimpleMQConsumer
class.
Note Spring and Spring Boot do not support the transmission of scheduled and delayed messages.
A log is printed, indicating that the messages are received. Class initialization
takes several seconds, and is seldom performed in the production environment.
Result verification: Check the consumer status in the Message Queue for Apache RocketMQ console. You can see that the started consumer are online and the subscription relationship
is consistent.