Run the demo project
Use the ApsaraMQ for RocketMQ demo project to send and receive normal messages, ordered messages, and transactional messages through the Apache RocketMQ Client of the Community Edition.
The demo project is a Maven-based Java application. Import it into your IDE, configure your ApsaraMQ for RocketMQ resources, and run producer and consumer classes to verify your setup.
Before you begin
Before you begin, make sure that you have:
Java Development Kit (JDK) installed
An integrated development environment (IDE) such as IntelliJ IDEA or Eclipse installed. This guide uses IntelliJ IDEA. Download the Ultimate version from the IntelliJ IDEA website
The demo project downloaded from the ApsaraMQ for RocketMQ code repository and extracted. The extracted folder is named
mq-demo-masterApsaraMQ for RocketMQ resources created, including an instance, topics, a consumer group, and an AccessKey pair. For instructions, see Create resources
Step 1: Import the project into IntelliJ IDEA
Open IntelliJ IDEA and select Import Project.
Navigate to the
mq-demo-master/rocketmq/java-rocketmq-demofolder and select it.Set the import type to Maven.
Click Next on each screen until the import finishes.
The import takes 2 to 3 minutes while Maven downloads JAR dependencies.
Step 2: Configure the demo
Open the MqConfig class and set the following fields to match the resources you created:
| Field | Description | Where to find the value |
|---|---|---|
TOPIC | The topic that you created | ApsaraMQ for RocketMQ console |
GROUP_ID | The consumer group that you created | ApsaraMQ for RocketMQ console |
ORDER_TOPIC | The topic that you created for ordered messages. Must be an ordered message topic. | ApsaraMQ for RocketMQ console |
GROUP_ID (ordered) | The consumer group that you created for ordered messages | ApsaraMQ for RocketMQ console |
ACCESS_KEY | Your AccessKey ID | Create an AccessKey pair |
SECRET_KEY | Your AccessKey secret | Create an AccessKey pair |
TAG | A custom message tag for filtering | Define based on your use case |
NAMESRV_ADDR | The TCP endpoint of your instance | TCP Endpoint section on the Instance Details page in the console |
The following example shows the configuration fields in the MqConfig class:
public static final String TOPIC = "<your-normal-message-topic>";
public static final String GROUP_ID = "<your-consumer-group>";
public static final String ORDER_TOPIC = "<your-ordered-message-topic>";
public static final String ORDER_GROUP_ID = "<your-ordered-consumer-group>";
public static final String ACCESS_KEY = "<your-accesskey-id>";
public static final String SECRET_KEY = "<your-accesskey-secret>";
public static final String TAG = "<your-message-tag>";
public static final String NAMESRV_ADDR = "<your-tcp-endpoint>";You can also use the AccessKey pair of a Resource Access Management (RAM) user, provided that the RAM user has the required permissions on the topic.
Step 3: Send messages
Run the producer class that matches the message type. To run a class in IntelliJ IDEA, right-click the class file and select Run.
| Message type | Producer class | Notes |
|---|---|---|
| Normal | RocketMQProducer | -- |
| Transactional | RocketMQTransactionProducer | Uses LocalTransactionCheckerImpl to check local transactions. See Send and receive transactional messages. |
| Ordered | RocketMQOrderProducer | Requires an ordered message topic. See Send and receive ordered messages. |
Step 4: Receive messages
Run the consumer class that matches the message type:
| Message type | Consumer class |
|---|---|
| Normal | RocketMQConsumer |
| Transactional | RocketMQConsumer |
| Ordered | RocketMQOrderConsumer |
After the consumer starts, a log entry confirms that the message is received. Class initialization takes a few seconds on first run. In production, this initialization happens only once.
Verify the consumer in the console
Open the ApsaraMQ for RocketMQ console and go to the Group Details page. Verify that:
The consumer is connected.
The subscription is consistent.