Updated on 2024-12-17 GMT+08:00

RocketMQ-Spring

This section describes how to use rocketmq-spring to connect to a Huawei Cloud RocketMQ instance to produce and consume messages. Obtain the related code from rocketmq-springboot-demo.

The RocketMQ instance connection addresses, topic name, and user information used in the following examples are available in Collecting Connection Information.

Adding the rocketmq-spring Dependency to the pom.xml File

<dependency>
     <groupId>org.apache.rocketmq</groupId>
     <artifactId>rocketmq-spring-boot-starter</artifactId>
     <version>${RELEASE.VERSION}</version>
</dependency>

Configuring the application.properties File

#=============== Producer Configuration =======================
## Use the actual NameServer address and port of RocketMQ
rocketmq.name-server=127.0.0.1:9876
rocketmq.producer.group=my-group
## Whether to enable SSL.
rocketmq.producer.tls-enable=true
#=============== Consumer Configuration =======================
## Use the actual NameServer address and port of RocketMQ
rocketmq.name-server=127.0.0.1:9876

Producing Messages

The following code is an example. Replace the information in bold with the actual values.
@SpringBootApplication
public class ProduceDemoApplication implements CommandLineRunner {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public static void main(String[] args) {
        SpringApplication.run(ProduceDemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // send message synchronously
        rocketMQTemplate.convertAndSend("topic", "Hello, World!");
        // send spring message
        rocketMQTemplate.send(
                "topic", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
        // send message asynchronously
        rocketMQTemplate.asyncSend(
                "topic",
                MessageBuilder.withPayload("Hello, World! I'm from spring message").build(),
                new SendCallback() {
                    @Override
                    public void onSuccess(SendResult var1) {
                        System.out.printf("async onSucess SendResult=%s %n", var1);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        System.out.printf("async onException Throwable=%s %n", var1);
                    }
                });
        // Send messages orderly
        rocketMQTemplate.syncSendOrderly(
                "topic", MessageBuilder.withPayload("Hello, World").build(), "hashkey");
    }
}

Consuming Messages

The following code is an example. Replace the information in bold with the actual values.
@SpringBootApplication
public class ConsumeDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumeDemoApplication.class, args);
    }

    @Service
    @RocketMQMessageListener(topic = "topic", consumerGroup = "group", tlsEnable = "true")
    public static class MyConsumer implements RocketMQListener<String> {
        public void onMessage(String message) {
            System.out.printf("received message: %s", message);
        }
    }
}