All Products
Search
Document Center

ApsaraMQ for RocketMQ:Set up the demo project for ApsaraMQ for RocketMQ

Last Updated:Mar 11, 2026

Build and run a test project with the TCP client SDK for Java. The demo project includes working examples for five message types -- normal, ordered, transactional, scheduled, and delayed -- across Java, Spring, and Spring Boot.

Prerequisites

Before you begin, make sure that you have:

Configure the demo project

Step 1: Import the project

Import the demo project files into IntelliJ IDEA.

Step 2: Update the SDK version

In pom.xml, update the TCP client SDK for Java dependency to the latest version. For available versions, see Release notes.

<dependency>
    <groupId>com.aliyun.openservices</groupId>
    <artifactId>ons-client</artifactId>
    <!--Replace with the latest version from the release notes-->
    <version>1.8.8.5.Final</version>
</dependency>

Step 3: Configure the MqConfig class

Set the following fields in the MqConfig class to match the resources you created:

public static final String TOPIC = "<your-topic>";
public static final String GROUP_ID = "<your-group-id>";
public static final String ORDER_TOPIC = "<your-order-topic>";
public static final String ORDER_GROUP_ID = "<your-order-group-id>";
public static final String ACCESS_KEY = "<your-access-key>";
public static final String SECRET_KEY = "<your-secret-key>";
public static final String TAG = "<your-message-tag>";
public static final String NAMESRV_ADDR = "<your-tcp-endpoint>";

Replace the placeholders with your actual values:

PlaceholderDescriptionWhere to find it
<your-topic>Topic for normal and transactional messagesApsaraMQ for RocketMQ console > your instance > Topics
<your-group-id>Consumer group IDApsaraMQ for RocketMQ console > your instance > Groups
<your-order-topic>Topic for ordered messagesSame as above (create a separate topic for ordered messages)
<your-order-group-id>Consumer group for ordered messagesSame as above
<your-access-key>AccessKey ID of your Alibaba Cloud accountSee Create an AccessKey pair
<your-secret-key>AccessKey secret of your Alibaba Cloud accountSame as above
<your-message-tag>Custom message tag for filteringAny string value
<your-tcp-endpoint>TCP endpoint of your instanceApsaraMQ for RocketMQ console > your instance > Instance Details page > TCP Endpoint section
You can also use the AccessKey pair of a RAM user who has permissions on the topic. For details about available configuration parameters, see Methods and parameters.

Step 4: Configure common.xml (Spring and Spring Boot only)

If you plan to use the Spring or Spring Boot examples, update the common.xml file with your resource information:

<props>
    <prop key="AccessKey">XXX</prop> <!--Replace with your AccessKey ID-->
    <prop key="SecretKey">XXX</prop> <!--Replace with your AccessKey secret-->
    <prop key="GROUP_ID">XXX</prop> <!--Replace with your consumer group ID-->
    <prop key="Topic">XXX</prop> <!--Replace with your topic-->
    <prop key="NAMESRV_ADDR">XXX</prop> <!--Replace with your TCP endpoint-->
</props>

Send and receive messages

Important

The demo project runs producers and consumers in a single thread. For production systems, run producers and consumers on separate threads or hosts. For multi-threaded examples, see Send messages by using multiple threads.

Run each example by calling the main method of the corresponding class.

Send messages

Run the producer class that matches your message type and framework:

Message typeJavaSpringSpring Boot
NormalSimpleMQProducerProducerClientProducerClient
TransactionalSimpleTransactionProducerTransactionProducerClientTransactionProducerClient
Ordered (FIFO)SimpleOrderProducerOrderProducerClientOrderProducerClient
Scheduled and delayedMQTimerProducerNot supportedNot supported

Details by message type:

Verify that messages are sent

After sending messages, go to the ApsaraMQ for RocketMQ console and query messages by topic. The messages you sent should appear in the results.

Receive messages

Run the consumer class that matches your message type and framework:

Message typeJavaSpringSpring Boot
NormalSimpleMQConsumerConsumerClientConsumerClient
TransactionalSimpleMQConsumerConsumerClientConsumerClient
Ordered (FIFO)SimpleOrderConsumerOrderConsumerClientOrderConsumerClient
Scheduled and delayedSimpleMQConsumerNot supportedNot supported
Class initialization takes a few seconds on startup. This is expected and only happens once -- in production, classes are initialized only during application startup.

Verify that messages are received

A log entry confirms that the consumer received the message. To verify further, view the status of consumers in the ApsaraMQ for RocketMQ console. The consumers you started should appear as online and subscribed to the topic.

What's next