Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Springboot integrates RocketMQ to send and receive messages

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how Springboot integrates RocketMQ to send and receive messages. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Springboot integrates RocketMQ to send and receive messages to create springboot project

Pom.xml adds rocketmq-spring-boot-starter dependencies.

Org.apache.rocketmq rocketmq-spring-boot-starter 2.1.0yml configuration

Application.yml

Rocketmq: name-server: 192.168.64.141:9876

Application-demo1.yml

Use demo1 profile to specify the producer group name

Rocketmq: producer: group: producer-demo1

Application-demo2.yml

Use demo2 profile to specify the producer group name

Rocketmq: producer: group: producer-demo2 Test demo 1

Send ordinary messages

Send a generic Message object for Spring

Send asynchronous messages

Send sequential messages

Producer

Package cn.tedu.demo2.m1;import org.apache.rocketmq.client.producer.SendCallback;import org.apache.rocketmq.client.producer.SendResult;import org.apache.rocketmq.spring.core.RocketMQTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.Message;import org.springframework.messaging.support.MessageBuilder;import org.springframework.stereotype.Component;@Componentpublic class Producer {@ Autowired private RocketMQTemplate t Public void send () {/ / send synchronization message t.convertAndSend ("Topic1:TagA", "Hello world!"); / / send Message Message message = MessageBuilder.withPayload ("Hello Spring message!") .build () of spring; t.send ("Topic1:TagA", message) / / send asynchronous message t.asyncSend ("Topic1:TagA", "hello world asyn", new SendCallback () {@ Override public void onSuccess (SendResult sendResult) {System.out.println ("sent successfully") } @ Override public void onException (Throwable throwable) {System.out.println ("send failed");}}); / / send sequential messages t.syncSendOrderly ("Topic1", "98456237, create", "98456237"); t.syncSendOrderly ("Topic1", "98456237, payment", "98456237") T.syncSendOrderly ("Topic1", "98456237, complete", "98456237");}}

Consumer

Package cn.tedu.demo2.m1;import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;import org.apache.rocketmq.spring.core.RocketMQListener;import org.springframework.stereotype.Component;@Component@RocketMQMessageListener (topic = "Topic1", consumerGroup = "consumer-demo1") public class Consumer implements RocketMQListener {@ Override public void onMessage (String s) {System.out.println ("receive" + s);}}

Main class

Package cn.tedu.demo2.m1;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Main {public static void main (String [] args) {SpringApplication.run (Main.class, args);}}

Test class

Need to put it in the test folder

Activate demo1 profile @ ActiveProfiles ("demo1")

Package cn.tedu.demo2.m1;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.ActiveProfiles;@SpringBootTest@ActiveProfiles ("demo1") public class Test1 {@ Autowired private Producer producer; @ Test public void test1 () {producer.send (); try {Thread.sleep (5000) } catch (InterruptedException e) {e.printStackTrace ();} demo 2

Send transaction messages

Producer

Package cn.tedu.demo2.m2;import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;import org.apache.rocketmq.spring.core.RocketMQTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.messaging.Message;import org.springframework.messaging.support.MessageBuilder;import org.springframework.stereotype.Component;@Componentpublic class Producer {@ Autowired private RocketMQTemplate t Public void send () {Message message = MessageBuilder.withPayload ("Hello world") .build (); / / once the message is sent, execute the listener t.sendMessageInTransaction ("Topic2", message,null);} @ RocketMQTransactionListener class Lis implements RocketMQLocalTransactionListener {@ Override public RocketMQLocalTransactionState executeLocalTransaction (Message message, Object o) {System.out.println ("execute local transaction"); return RocketMQLocalTransactionState.UNKNOWN } @ Override public RocketMQLocalTransactionState checkLocalTransaction (Message message) {System.out.println ("perform transaction review"); return RocketMQLocalTransactionState.COMMIT;}

Consumer

Package cn.tedu.demo2.m2;import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;import org.apache.rocketmq.spring.core.RocketMQListener;import org.springframework.stereotype.Component;@Component@RocketMQMessageListener (topic = "Topic2", consumerGroup = "consumer-demo2") public class Consumer implements RocketMQListener {@ Override public void onMessage (String s) {System.out.println ("receive" + s);}}

Main class

Package cn.tedu.demo2.m2;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Main {public static void main (String [] args) {SpringApplication.run (Main.class, args);}}

Test class

Package cn.tedu.demo2.m2;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.ActiveProfiles;@SpringBootTest@ActiveProfiles ("demo2") public class Test2 {@ Autowired private Producer producer; @ Test public void test1 () {producer.send () / / in order to receive data on consumer consumption, the waiting time try {Thread.sleep (30000);} catch (InterruptedException e) {e.printStackTrace () is simulated by dormancy. } this is the end of the article on "how Springboot integrates RocketMQ sending and receiving messages". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 254

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report