This topic describes how to build a ApsaraMQ for 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 package to your local machine. A folder named rocketmq-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.
Go to the Message Queue for Apache RocketMQ console to create resources, including the instance, topic, consumer group, AccessKey ID (AK), and AccessKey secret (SK).
For more information, see Create resources.
- Configure the demo. Configure the
MqConfig
class and common.xml file based on the information about resources that are created in step 2.
Run the demo by calling the main method
- Send messages.
- Send normal messages:
- Java: Run the
SimpleMQProducer
class. - Spring: Run the
ProducerClient
class.
- Java: Run the
- Send transactional messages:
- Java: Run the
SimpleTransactionProducer
class.The
LocalTransactionCheckerImpl
class is used to check the status of local transactions. For more information, see Send and receive transactional messages. - Spring: Run the
TransactionProducerClient
class. - Spring Boot: Run the
TransactionProducerClient
class.
- Java: Run the
- 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.
- Java: Run the
- Send scheduled messages and delayed messages: Run the
MQTimerProducer
class. Messages are delivered after a delay of 3 seconds.You can also specify a delay time to deliver these messages. The maximum value of the delay time is 40 days. For more information, see Send and receive scheduled messages.
In the ApsaraMQ for RocketMQ console, query messages by topic. You can view that the messages are sent to the topic. - Send normal messages:
- Receive messages.
- Receive normal messages:
- Java: Run the
SimpleMQConsumer
class. - Spring: Run the
ConsumerClient
class. - Spring Boot: Run the
ConsumerClient
class.
- Java: Run the
- Receive transactional messages:
- Java: Run the
SimpleMQConsumer
class. - Spring: Run the
ConsumerClient
class. - Spring Boot: Run the
ConsumerClient
class.
- Java: Run the
- Receive ordered messages:
- Java: Run the
SimpleOrderConsumer
class. - Spring: Run the
OrderConsumerClient
class. - Spring Boot: Run the
OrderConsumerClient
class.
- Java: Run the
- 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. The log indicates that the message is received. Class initialization requires several seconds to complete. In production environments, class initialization is seldom performed. - Receive normal messages:
Verify the result: View the status of consumers in the ApsaraMQ for RocketMQ console. You can find that the started consumers are online and have subscribed to the same topic.