All Products
Search
Document Center

ApsaraMQ for RocketMQ:Integrate with Spring

Last Updated:Aug 17, 2023

ApsaraMQ for RocketMQ allows you to integrate the Spring framework with ApsaraMQ for RocketMQ SDK for Java and use the SDK to send and receive messages. This topic describes how to use the Spring-integrated demo project that is provided by ApsaraMQ for RocketMQ.

Background information

ApsaraMQ for RocketMQ allows you to integrate Spring with the following types of producers and consumers:

  • Producers and consumers of normal messages

  • Producers and consumers of transactional messages

  • Producers and consumers of ordered messages

Note

ApsaraMQ for RocketMQ does not support the integration with Spring to send and receive scheduled messages and delayed messages.

Preparations

Step 1: Download the demo project

ApsaraMQ for RocketMQ provides two demos: that for Spring Boot and that for Spring. This topic uses the demo for SpringBoot as an example. The operations for using the demo for Spring are similar.

Step 2: Create resources

Create the required resources in the ApsaraMQ for RocketMQ console. The following items provide links to the corresponding sections.

  • Create an instance

  • Create a topic

    When you create a topic, the message type of the topic must be the same as that of the messages to be sent. For example, if you want to send normal messages, you must create a topic of the normal message type. If you want to send ordered messages, you must create a topic of the ordered message type.

  • Create a group ID

    The protocol type of the group must be the same as that of the SDK. The Sping-integrated ApsaraMQ for RocketMQ SDK for Java uses the TCP protocol. Therefore, you must select the TCP protocol when you create a group ID.

  • Obtain an endpoint

    The Spring-integrated ApsaraMQ for RocketMQ SDK for Java uses the TCP protocol. Therefore, you must obtain the TCP endpoint.

  • Create an AccessKey pair

Step 3: Configure the demo project

Create a Java project in IntelliJ IDEA, import the demo file that you obtained in Step 1: Download the demo project, and then perform the following operations:

Modify the pom.xml file

The following code snippet shows the dependencies to be added to the pom.xml file. We recommend that you modify the ons-client version to the latest version. For more information about versions, 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>
            <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>

Configure parameters

Modify the application.properties file in the java-springboot-demo/src/main/resources path based on the resource information in Step 2: Create resources.

Parameter

Description

Example

accessKey

The AccessKey ID, which is an identifier to verify identities in Alibaba Cloud. This parameter is used to control the permissions of clients.

LTAI5t6K7KY******VjAY3yS

secretKey

The AccessKey Secret, which is a key to verify identities in Alibaba Cloud. This parameter is used to control the permissions of clients.

P6z2pkIKAZ******n61JZ5e1lofFG9

nameSrvAddr

The TCP endpoint of the ApsaraMQ for RocketMQ instance. Users use the endpoint to access the ApsaraMQ for RocketMQ broker.

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

topic

The topic that you created in the console to send messages.

noamal_topic

groupId

The group ID that you created in the console to subscribe to messages.

GID_test

tag

The filter expression that is used to subscribe to messages. For example, "*" indicates that the consumer subscribes to all messages in the specified topic. "Tag A || Tag B" indicates that the consumer subscribes to messages with tags A and B in the topic. For more information, see Message filtering

*

orderTopic

The topic that you created in the console to send ordered messages.

order_topic

orderGroupId

The group ID that you created in the console to subscribe to ordered messages.

GID_order

orderTag

The filter expression that is used to subscribe to ordered messages. For more information, see Message filtering

tagA

Step 4: Run the demo project by calling the main() method

When you run the sample code, make sure that the classes in the java-springboot-demo/src/main/java/com/aliyun/openservices/springboot/example path are imported to the project.

Message type

Sample code for sending messages

Sample code for subscribing to messages

Normal messages

ProducerClient.java

Ordered messages

OrderProducerClient.java

OrderConsumerClient.java

Transactional messages

TransactionProducerClient.java

ConsumerClient.java