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
The Spring integration does not support scheduled messages or delayed messages.
Prerequisites
Before you begin, make sure you have:
IntelliJ IDEA or another Java IDE
An ApsaraMQ for RocketMQ instance with a TCP endpoint
An AccessKey pair (AccessKey ID and AccessKey secret)
Step 1: Download the demo project
Clone or download one of the demo projects from GitHub:
| Demo | Repository |
|---|---|
| Spring Boot (used in this guide) | java-springboot-demo |
| Spring | java-spring-demo |
Step 2: Create resources
Create the following resources in the ApsaraMQ for RocketMQ console:
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.
Create a group. Select TCP as the protocol type because the ons-client SDK communicates over TCP.
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 ID for authentication |
|
| AccessKey secret for authentication |
|
| TCP endpoint of your ApsaraMQ for RocketMQ instance |
|
| Topic name for normal messages |
|
| Group ID for the consumer |
|
| Tag filter expression. Use |
|
| Topic name for ordered messages |
|
| Group ID for the ordered message consumer |
|
| Tag filter expression for ordered messages |
|
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
| Role | Class |
|---|---|
| Producer | ProducerClient.java |
| Synchronous sending | SyncProducerTest.java |
| Asynchronous sending | AsyncProducerTest.java |
| Consumer (tag-based filtering) | ConsumerClient.java |
| Consumer (SQL-based filtering) | SqlConsumerClient.java |
Ordered messages
| Role | Class |
|---|---|
| Producer | OrderProducerClient.java |
| Consumer | OrderConsumerClient.java |
Transactional messages
| Role | Class |
|---|---|
| Producer | TransactionProducerClient.java |
| Consumer | ConsumerClient.java |
What's next
Message filtering -- Filter messages by tag or SQL expression.
Release notes -- Check for the latest ons-client SDK version.