package com.boot.rocketmq.producer;
import com.boot.rocketmq.entity.MqMsg;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 消息生产接口
* @author 公众号:知了一笑
* @since 2023-07-30 16:17
*/
@RestController
public class ProducerWeb {
private static final Logger log = LoggerFactory.getLogger(ProducerWeb.class);
@Autowired
private RocketMQTemplate rocketMqTemplate;
@GetMapping("/send/msg1")
public String sendMsg1 (){
try {
// 构建消息主体
JsonMapper jsonMapper = new JsonMapper();
String msgBody = jsonMapper.writeValueAsString(new MqMsg(1,"boot_mq_msg"));
// 发送消息
rocketMqTemplate.convertAndSend("boot-mq-topic",msgBody);
} catch (Exception e) {
e.printStackTrace();
}
return "OK" ;
}
@Autowired
private DefaultMQProducer defaultMqProducer ;
@GetMapping("/send/msg2")
public String sendMsg2 (){
try {
// 构建消息主体
JsonMapper jsonMapper = new JsonMapper();
String msgBody = jsonMapper.writeValueAsString(new MqMsg(2,"boot_mq_msg"));
// 构建消息对象
Message message = new Message();
message.setTopic("boot-mq-topic");
message.setTags("boot-mq-tag");
message.setKeys("boot-mq-key");
message.setBody(msgBody.getBytes());
// 发送消息,打印日志
SendResult sendResult = defaultMqProducer.send(message);
log.info("msgId:{},sendStatus:{}",sendResult.getMsgId(),sendResult.getSendStatus());
} catch (Exception e) {
e.printStackTrace();
}
return "OK" ;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
m4-04-boot-rocketmq.zip (8个子文件)
m4-04-boot-rocketmq
pom.xml 2KB
src
main
resources
application.yml 577B
META-INF
spring
org.springframework.boot.autoconfigure.AutoConfiguration.imports 66B
java
com
boot
rocketmq
Application.java 377B
producer
ProducerWeb.java 2KB
consumer
ConsumerListener.java 729B
entity
MqMsg.java 351B
config
RocketMqConfig.java 2KB
共 8 条
- 1
资源评论
飞翔的佩奇
- 粉丝: 5849
- 资源: 1522
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功