All Products
Search
Document Center

ApsaraMQ for RocketMQ:Integrate with Spring

Last Updated:Mar 11, 2026

Send and receive messages using the ApsaraMQ for RocketMQ SDK for Java in a Spring or Spring Boot application.

ApsaraMQ for RocketMQ provides demo projects that wire the ons-client SDK into Spring-managed beans, giving you producer and consumer components ready for dependency injection. This guide walks through the Spring Boot demo. The plain Spring demo follows the same pattern.

Supported message types

The Spring integration supports producers and consumers for the following message types:

  • Normal messages

  • Ordered messages

  • Transactional messages

Note

The Spring integration does not support scheduled messages or delayed messages.

Prerequisites

Before you begin, make sure you have:

Step 1: Download the demo project

Clone or download one of the demo projects from GitHub:

DemoRepository
Spring Boot (used in this guide)java-springboot-demo
Springjava-spring-demo

Step 2: Create resources

Create the following resources in the ApsaraMQ for RocketMQ console:

  1. Create an instance.

  2. Create a topic. Set the message type to match the messages you plan to send. For example, select Normal for normal messages or Ordered for ordered messages.

  3. Create a group. Select TCP as the protocol type because the ons-client SDK communicates over TCP.

  4. Get the TCP endpoint for your instance.

Step 3: Configure the demo project

Open the demo project in IntelliJ IDEA, then update the Maven dependency and application properties.

Add the ons-client dependency

Add the following dependencies to your pom.xml. Replace the ons-client version with the latest available version. For version history, see Release notes.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

Set application properties

Edit java-springboot-demo/src/main/resources/application.properties with the resource information from Step 2.

Parameter

Description

Example

accessKey

AccessKey ID for authentication

<your-access-key-id>

secretKey

AccessKey secret for authentication

<your-access-key-secret>

nameSrvAddr

TCP endpoint of your ApsaraMQ for RocketMQ instance

http://MQ_INST_XXXXXX.cn-hangzhou.mq.aliyuncs.com:80

topic

Topic name for normal messages

noamal_topic

groupId

Group ID for the consumer

GID_test

tag

Tag filter expression. Use * to subscribe to all messages, or specify tags such as TagA || TagB. For details, see Message filtering.

*

orderTopic

Topic name for ordered messages

order_topic

orderGroupId

Group ID for the ordered message consumer

GID_order

orderTag

Tag filter expression for ordered messages

tagA

Step 4: Run the demo

Make sure the classes under java-springboot-demo/src/main/java/com/aliyun/openservices/springboot/example are imported into your project, then run the main() method.

Sample code reference

The demo includes producers and consumers for each supported message type. All source code is available on GitHub.

Normal messages

RoleClass
ProducerProducerClient.java
Synchronous sendingSyncProducerTest.java
Asynchronous sendingAsyncProducerTest.java
Consumer (tag-based filtering)ConsumerClient.java
Consumer (SQL-based filtering)SqlConsumerClient.java

Ordered messages

RoleClass
ProducerOrderProducerClient.java
ConsumerOrderConsumerClient.java

Transactional messages

RoleClass
ProducerTransactionProducerClient.java
ConsumerConsumerClient.java

What's next