This topic describes how to build a test project in ApsaraMQ for RocketMQ to send and receive messages by using Apache RocketMQ Client of the Community Edition. The demo project provides sample code for normal messages, ordered messages, and transactional messages.
Prerequisites
The integrated development environment (IDE) is installed.
You can use IntelliJ IDEA or Eclipse. IntelliJ IDEA is used in the examples of this topic.
Download the IntelliJ IDEA Ultimate version from the IntelliJ IDEA official website and install IntelliJ IDEA.
The demo project package is downloaded.
Download the demo project from the ApsaraMQ for RocketMQ code library to your local computer. After you decompress the demo project, you can see that the
mq-demo-masterfolder is added to your local computer.The Java Development Kit (JDK) is installed.
Configure the demo project
Import the demo project to IntelliJ IDEA.
On the IntelliJ IDEA page, select Import Project and then select the mq-demo-master/rocketmq/java-rocketmq-demo folder.
Set the Import parameter to Maven.
Click Next until the project is imported. Importing the project requires 2 to 3 minutes to complete because the JAR dependency needs to be loaded to the demo project.
Create resources.
You must create the required resources in the ApsaraMQ for RocketMQ console, including the ApsaraMQ for RocketMQ instance, topic, group, and AccessKey pair that is required for authentication.
For more information, see Create resources.
Configure the demo.
You must configure the information about the resources that you created in Step 2 to the file
MqConfigclass.public static final String TOPIC = "The topic that you created"; public static final String GROUP_ID = "The consumer group 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 consumer group that you created to send and receive ordered messages"; public static final String ACCESS_KEY = "The AccessKey ID of your Alibaba Cloud account". For information about how to obtain an AccessKey ID, see Create an AccessKey pair"; public static final String SECRET_KEY = "The AccessKey secret of your Alibaba Cloud account. For information about how to obtain an AccessKey secret, see Create an AccessKey pair"; public static final String TAG = "The custom tag of the message"; public static final String NAMESRV_ADDR = "The TCP endpoint that can be used to access your Message Queue for Apache RocketMQ instance. You can view the endpoint in the TCP Endpoint section of the Instance Details page in the Message Queue for Apache RocketMQ console;NoteFor information about how to create an AccessKey pair, see Create an AccessKey pair.
You can also use the AccessKey of the Resource Access Management (RAM) user if the RAM user is granted the required permissions on the topic that you created.
Run the demo by calling the main method
Send a message.
Normal message: Run the
RocketMQProducerclass.Transactional message: Run the
RocketMQTransactionProducerclass.LocalTransactionCheckerImplis an interface class that you can call to check local transactions. For more information, see Send and receive transactional messages.Ordered message: Run the
RocketMQOrderProducerclass.Note that the topic that you created must be of the ordered message type. For more information, see Send and receive ordered messages.
Receive a message.
Normal message: Run the
RocketMQConsumerclass.Transactional message: Run the
RocketMQConsumerclass.Ordered message: Run the
RocketMQOrderConsumerclass.
A log is printed. The log indicates that the message is received. Class initialization requires a few seconds to complete. In production environments, class initialization is seldom performed.
Log on to the ApsaraMQ for RocketMQ console. On the Group Details page, you can see that the launched consumer is connected and the subscription is consistent.