基于上述讨论点,封装建议基于实体类来,实体类不管是排查问题、新人熟悉系统代码、信息校验等String和JSONObject无法像实体类一样轻松胜任2.2 RocketMQTemplate封装2.2.1 封装基础实体类
- 基础消息实体类封装了除了业务消息外所有其他公共字段,主要看下面代码中的字段和注释
- 基础抽象消息实体,包含基础的消息、根据自己的业务消息设置更多的字段 其中也可以包含所有消费者可能用得到的方法等,比如做些数据的加解密
package com.codecoord.rocketmq.domain;import lombok.Data;import java.time.LocalDateTime;import java.util.UUID;/** * 基础消息实体,包含基础的消息 * 根据自己的业务消息设置更多的字段 * * @author tianxincoord@163.com * @since 2022/6/16 */@Datapublic abstract class BaseMqMessage {/*** 业务键,用于RocketMQ控制台查看消费情况*/protected String key;/*** 发送消息来源,用于排查问题*/protected String source = "";/*** 发送时间*/protected LocalDateTime sendTime = LocalDateTime.now();/*** 跟踪id,用于slf4j等日志记录跟踪id,方便查询业务链*/protected String traceId = UUID.randomUUID().toString();/*** 重试次数,用于判断重试次数,超过重试次数发送异常警告*/protected Integer retryTimes = 0;} - 有了此基础抽象实体类,那么剩下的所有业务消息实体只需要继承此基类,然后在自己业务类中包含自己需要的字段即可,因为这些公共字段不管是向上转型还是向下转型,子类和父类都可以看得到
2.2.2 RocketMQTemplate
- RocketMQTemplate发送消息的代码如果不封装,我们发送消息需要这样 String destination = topic + ":" + tag; template.syncSend(destination, message);
- 每个人发送消息都要自己处理这个冒号,直接传入topic和tag不香吗?按照抽离变化点中的变化点,只有消息是变化的,除此之外的其他规则交给封装类
- RocketMQTemplate主要封装发送消息的日志、异常的处理、消息key设置、等等其他配置
- 封装代码类如下,下面包含了主要发送方式,更多自己添加即可 这里就是消息发送的点餐机器,同时也提供了封装方法也提供原始RocketMQTemplate供使用 此处只是提供一种方式,生产中按照项目组商量决定
package com.codecoord.rocketmq.template;import com.alibaba.fastjson.JSONObject;import com.codecoord.rocketmq.constant.RocketMqSysConstant;import com.codecoord.rocketmq.domain.BaseMqMessage;import com.codecoord.rocketmq.util.JsonUtil;import org.Apache.rocketmq.client.producer.SendResult;import org.apache.rocketmq.spring.core.RocketMQTemplate;import org.apache.rocketmq.spring.support.RocketMQHeaders;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.messaging.Message;import org.springframework.messaging.support.MessageBuilder;import org.springframework.stereotype.Component;import javax.annotation.Resource;/** * RocketMQ模板类 * * @author tianxincoord@163.com * @since 2022/4/15 */@Componentpublic class RocketMqTemplate {private static final Logger LOGGER = LoggerFactory.getLogger(RocketMqTemplate.class);@Resource(name = "rocketMQTemplate")private RocketMQTemplate template;/*** 获取模板,如果封装的方法不够提供原生的使用方式*/public RocketMQTemplate getTemplate() {return template;}/*** 构建目的地*/public String buildDestination(String topic, String tag) {return topic + RocketMqSysConstant.DELIMITER + tag;}/*** 发送同步消息*/public <T extends BaseMqMessage> SendResult send(String topic, String tag, T message) {// 注意分隔符return send(topic + RocketMqSysConstant.DELIMITER + tag, message);}public <T extends BaseMqMessage> SendResult send(String destination, T message) {// 设置业务键,此处根据公共的参数进行处理// 更多的其它基础业务处理...Message<T> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();SendResult sendResult = template.syncSend(destination, sendMessage);// 此处为了方便查看给日志转了json,根据选择选择日志记录方式,例如ELK采集LOGGER.info("[{}]同步消息[{}]发送结果[{}]", destination, JsonUtil.toJson(message), JSONObject.toJSON(sendResult));return sendResult;}/*** 发送延迟消息*/public <T extends BaseMqMessage> SendResult send(String topic, String tag, T message, int delayLevel) {return send(topic + RocketMqSysConstant.DELIMITER + tag, message, delayLevel);}public <T extends BaseMqMessage> SendResult send(String destination, T message, int delayLevel) {Message<T> sendMessage = MessageBuilder.withPayload(message).setHeader(RocketMQHeaders.KEYS, message.getKey()).build();SendResult sendResult = template.syncSend(destination, sendMessage, 3000, delayLevel);LOGGER.info("[{}]延迟等级[{}]消息[{}]发送结果[{}]", destination, delayLevel, JsonUtil.toJson(message), JsonUtil.toJson(sendResult));return sendResult;}}
推荐阅读
-
上海上港|虽然目前恒大和上港同为小组第一,实际上硬实力比起来相差甚远?
-
-
娱乐|筷子兄弟出道13年:一个亿万身家,一个负债千万,网友:江郎才尽
-
-
同样是生在9月的娃,一个提前入学一个推迟入学,三年后差距明显
-
李连杰|李连杰的腿功比李小龙厉害?双腿并用加回旋踢,让甄子丹有压力
-
-
「引」引“垂直森林”入华 居然之家与博埃里全面合作
-
-
普洛帝液体颗粒监测清洁度分析系统评估电子半导体清洁程度
-
狂言体育|英超2队 西甲德甲意甲各1队 黑马将大战盟主,欧联杯8强对阵出炉
-
『天天爱逗乐』有些没有?后雨挡的开关在哪?,没什么有些车有后档雨刷
-
-
河北辛集公安发布一起重大刑事案件协查通报:嫌疑人可能携带凶器出逃
-
-
斗鱼直播|预言家闪现Q兵后,TES招辅助了!斗鱼主播透露:试训的不止我一人
-
苹果7怎样降级ios13.5 如何降级到ios13.3.1
-
鲜闻说▲我姐快生了!赶紧送医院吧!,开心笑话:别争了
-
-
青年|王者荣耀:飞将上线第1天,销量轻松破百万,德华却因8个字惹怒QG