展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

在@Component标注的类中注入@Component标注的类

@Component
public class MsgPubMqttUtil {
@Resource
private EmqClient emqClient;
@Resource
private MqttProperties mqttProperties;
@Resource
private TMessageTemplateMapper tMessageTemplateMapper;
@Resource
private TMessageMapper tMessageMapper;
private static MsgPubMqttUtil msgPubMqttUtil;
@PostConstruct
public void init(){
msgPubMqttUtil = this;
msgPubMqttUtil.emqClient = this.emqClient;
msgPubMqttUtil.mqttProperties = this.mqttProperties;
msgPubMqttUtil.tMessageTemplateMapper = this.tMessageTemplateMapper;
msgPubMqttUtil.tMessageMapper = this.tMessageMapper;
}
/**
* 发送消息
* templateId 模板id
* pubId 订阅者id
* userType 订阅者用户类型
* @throws Exception
*/
public void sendMqttMessage(String templateId, String pubId, String userType) {
// 连接服务器端
emqClient.connect(mqttProperties.getUsername(),mqttProperties.getPassword());
// 拼接消息主题
StringBuffer result = new StringBuffer();
result.append("testtopic");
result.append("/");
result.append(pubId);
// 获取消息模板
TMessageTemplate tMessageTemplate = msgPubMqttUtil.tMessageTemplateMapper.selectById(templateId);
Map<String,String> map = new HashMap<String,String>();
map.put("msgTemplateTitle",tMessageTemplate.getMsgTemplateTitle());
map.put("templateContent",tMessageTemplate.getTemplateContent());
String str = ConverUitl.getMapToString(map);
// 发布消息
emqClient.publish(result.toString(), str, QosEnum.QoS2,false);
// 存入历史记录表
TMessage tMessage = new TMessage();
tMessage.setId(SnowflakeIdWorkerUtil.nextId());
tMessage.setCreated(new Date());
tMessage.setUpdated(new Date());
tMessage.setReadStatus("1"); // 设置为未读
tMessage.setMsgTitle(map.get("msgTemplateTitle"));
tMessage.setMsgContent(map.get("templateContent"));
tMessage.setUserType(userType); // 接收者类型
tMessage.setUserId(pubId); // 接收者主键
tMessage.setMtId(templateId); // 关联的模板id
tMessageMapper.insert(tMessage);
}
}
posted @   DogLeftover  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
历史上的今天:
2021-07-22 ubuntu入门
点击右上角即可分享
微信分享提示