JAVA全系列 教程
3762个小节阅读:7092.1k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
创建springboot-rocketmq-producer工程
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
xxxxxxxxxx
# application.properties
spring.application.name=springboot_rocketmq_producer
# nameserver的地址
rocketmq.name-server=192.168.139.128:9876
#指定生产组名称
rocketmq.producer.group=my-group
xxxxxxxxxx
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {MyRocketProducerApplication.class})
public class MyRocketProducerApplicationTest {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Test
public void testSendMessage() {
// 用于向broker发送消息
// 第一个参数是topic名称
// 第二个参数是消息内容
this.rocketMQTemplate.convertAndSend(
"topic_springboot_01",
"springboot: hello rocketmq..."
);
}
}
创建springboot-rocketmq-consumer工程
pom.xml文件同producer工程
application.properties配置
xxxxxxxxxx
spring.application.name=springboot-rocketmq-consumer
rocketmq.name-server=192.168.139.128:9876
启动类
xxxxxxxxxx
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootRocketmqConsumerApplication {
public static void main(String[] args) { SpringApplication.run(SpringbootRocketmqConsumerApplication.class, args);
}
}
消息监听器
xxxxxxxxxx
@Slf4j
@Component
@RocketMQMessageListener(topic="topic_springboot_01",consumerGroup="springboot-rocketmq-consumer-01")
public class Consumer implements RocketMQlistener{
@override
public void onMessage(String message){
log.info("Received messsge:" + message);
}
}
启动consumer工程,观察控制台窗口打印内容
xxxxxxxxxx
springboot: hello rocketmq...