目录
导入依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>
YAML配置
rocketmq:
name-server: localhost:9876
producer:
group: test-group-producer #生产者必须配备生产者组
1. 同步消息
同步消息是发送消息后等待Broker的响应,确保消息被成功接收。
生产者:
@Autowired
RocketMQTemplate rocketMQTemplate;
@Test
void contextLoads() {
SendResult result = rocketMQTemplate.syncSend("test", MessageBuilder.withPayload("同步消息").build());
// SendResult result = rocketMQTemplate.syncSend("test", "同步消息");
System.out.println("发送状态:" + result.getSendStatus() + " 消息id:" + result.getMsgId());
}
2. 异步消息
异步消息是发送消息后不等待Broker响应,通过回调函数处理发送结果。
@Autowired
RocketMQTemplate rocketMQTemplate;
@Test
void contextLoads() {
rocketMQTemplate.asyncSend("test", MessageBuilder.withPayload("异步消息").build(), new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.println(