Run the demo project

Updated at:

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-master

  • ApsaraMQ 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

  1. Open IntelliJ IDEA and select Import Project.

  2. Navigate to the mq-demo-master/rocketmq/java-rocketmq-demo folder and select it.

  3. Set the import type to Maven.

  4. 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:

FieldDescriptionWhere to find the value
TOPICThe topic that you createdApsaraMQ for RocketMQ console
GROUP_IDThe consumer group that you createdApsaraMQ for RocketMQ console
ORDER_TOPICThe 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 messagesApsaraMQ for RocketMQ console
ACCESS_KEYYour AccessKey IDCreate an AccessKey pair
SECRET_KEYYour AccessKey secretCreate an AccessKey pair
TAGA custom message tag for filteringDefine based on your use case
NAMESRV_ADDRThe TCP endpoint of your instanceTCP 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>";
Note

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 typeProducer classNotes
NormalRocketMQProducer--
TransactionalRocketMQTransactionProducerUses LocalTransactionCheckerImpl to check local transactions. See Send and receive transactional messages.
OrderedRocketMQOrderProducerRequires an ordered message topic. See Send and receive ordered messages.

Step 4: Receive messages

Run the consumer class that matches the message type:

Message typeConsumer class
NormalRocketMQConsumer
TransactionalRocketMQConsumer
OrderedRocketMQOrderConsumer

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.

What's next