向mq写消息

1.基础版本

复制代码
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import com.alibaba.fastjson.JSON;

public class MQProducer {
    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("G-Group_REQ");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();

        String jsonStr = JSON.toJSONString("Your JSON content");
        Message msg = new Message("T-Topic", jsonStr.getBytes());

        producer.send(msg);
        producer.shutdown();
    }
}

2.添加写消息失败处理:登记日志、定时处理、参数配置化

复制代码
import cn.com.*.support.MQLogSupport;
import cn.com.*.entity.MQLog;
import cn.com.*.repository.MQLogRepository;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.javacrumbs.shedlock.core.SchedulerLock;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class MQLogSupportImpl implements MQLogSupport{
    private final Logger logger = LoggerFactory.getLogger(MQLogSupport.class);
    @Autowired
    private MQLogRepository mqLogRepository;

    @Value("${rocketmq.name-server}")
    String namesrvAddr;

    // 写消息服务
    public void sendMessageNoTag(MQLog mqLog) {
        try {
            DefaultMQProducer producer = new DefaultMQProducer(mqLog.getMqGroup());
            producer.setNamesrvAddr(namesrvAddr);
            // 设置发送消息的超时时间为30秒
            producer.setSendMsgTimeout(30000);
            producer.start();
            Message msg = new Message(mqLog.getMqTopic(), mqLog.getMqMessage().getBytes());
            producer.send(msg);
            setMQlogSuccess(mqLog);
            producer.shutdown();
        } catch (Exception e) {
            setMQlogError(mqLog, "9999", "初次:" + e.getMessage());
        }
    }

    // 定时任务,失败后重新写消息,每分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    @SchedulerLock(name = "RocketMQ", lockAtLeastForString = "PT5M", lockAtMostForString = "PT30M")
    public void readAndResendMessage() {
        // 读取失败信息
        List<MQLog> messageList = mqLogRepository.findByDealCodeNot("0000");
        if(CollectionUtils.isEmpty(messageList)){
            return;
        }
        // 重新写消息
        for (MQLog message : messageList) {
            try {
                DefaultMQProducer producer = new DefaultMQProducer(message.getMqGroup());
                producer.setNamesrvAddr(namesrvAddr);
                producer.setSendMsgTimeout(30000);
                producer.start();
                Message msg = new Message(message.getMqTopic(), message.getMqMessage().getBytes());
                producer.send(msg);
                setMQlogSuccess(message);
                producer.shutdown();
            } catch (Exception e) {
                setMQlogError(message, "定时任务:" + e.getMessage());
            }
        }
    }

    public MQLog regMQlog(JSONObject mqLogJson){
        logger.debug("登记 mq 消息......");
        String topic = mqLogJson.getString("topic");
        String tags = mqLogJson.getString("tags");
        String msg = JSON.toJSONString(mqLogJson);
        String key = mqLogJson.getString("key");
        Boolean approveResult = mqLogJson.getBoolean("approved");
        String approver = mqLogJson.getString("approver");
        String comment = mqLogJson.getString("comment");
        String finshTime = mqLogJson.getString("finshTime");

        MQLog.MQLogBuilder mqLogBuilder = MQLog.builder();
        mqLogBuilder.mqTopic(topic)
                .mqTags(tags)
                .key(key)
                .mqMessage(msg)
                .approveResult(approveResult)
                .approver(approver)
                .approveComment(comment)
                .approveFinishTime(finshTime)
                .regDateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")));

        MQLog savedMqLog = mqLogRepository.save(mqLogBuilder.build());
        logger.debug("登记 mq 消息完成。");
        return savedMqLog;
    }

    public void setMQlogError(MQLog mqLog, String errorCode, String errorMsg){
        mqLogRepository.save(mqLog.toBuilder().dealCode(errorCode).dealMsg(errorMsg).build());
    }

    public void setMQlogError(MQLog mqLog, String errorMsg){
        setMQlogError(mqLog,"Error",errorMsg);
    }

    public void setMQlogSuccess(MQLog mqLog){
        mqLogRepository.save(mqLog.toBuilder().dealCode("0000").dealMsg("success").build());
    }
}

3.简化代码

复制代码
import cn.com.*.support.MQLogSupport;
import cn.com.*.entity.MQLog;
import cn.com.*.repository.MQLogRepository;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.javacrumbs.shedlock.core.SchedulerLock;
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.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class MQLogSupportImpl implements MQLogSupport {
    private final Logger logger = LoggerFactory.getLogger(MQLogSupport.class);
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private MQLogRepository mqLogRepository;

    // 写消息服务
    public void sendMessageNoTag(MQLog mqLog) {
        rocketMQTemplate.asyncSend(mqLog.getMqTopic(), mqLog.getMqMessage(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                setMQlogSuccess(mqLog);
            }

            @Override
            public void onException(Throwable e) {
                setMQlogError(mqLog, "初次:" + e.getMessage());
                retrySendMessage(mqLog);
            }
        });
    }

    // 重试发送消息
    private void retrySendMessage(MQLog mqLog) {
        int retryCount = mqLog.getRetryCount();
        if (retryCount < 3) { // 最多重试3次
            mqLog.setRetryCount(retryCount + 1);
            mqLogRepository.save(mqLog);
            sendMessageNoTag(mqLog);
        } else {
            setMQlogError(mqLog, "重试3次仍然失败");
        }
    }

    // 定时任务,失败后重新写消息,每分钟执行一次
    @Scheduled(cron = "0 0/1 * * * ?")
    @SchedulerLock(name = "RocketMQ", lockAtLeastForString = "PT5M", lockAtMostForString = "PT30M")
    public void readAndResendMessage() {
        // 读取失败信息
        List<MQLog> messageList = mqLogRepository.findByDealCodeNot("0000");
        if (CollectionUtils.isEmpty(messageList)) {
            return;
        }
        // 重新写消息
        for (MQLog message : messageList) {
            rocketMQTemplate.asyncSend(message.getMqTopic(), message.getMqMessage(), new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    setMQlogSuccess(message);
                }

                @Override
                public void onException(Throwable e) {
                    setMQlogError(message, "定时任务:" + e.getMessage());
                    retrySendMessage(message);
                }
            });
        }
    }

    // 设置消息发送成功
    private void setMQlogSuccess(MQLog mqLog) {
        mqLog.setDealCode("0000");
        mqLog.setDealDesc("发送成功");
        mqLogRepository.save(mqLog);
    }

    // 设置消息发送失败
    private void setMQlogError(MQLog mqLog, String errorMsg) {
        mqLog.setDealCode("9999");
        mqLog.setDealDesc(errorMsg);
        mqLogRepository.save(mqLog);
    }
}

注意:

  1. 配置文件
复制代码
rocketmq:
  name-server: IP:端口
  producer:
    group: "G-Group_REQ"
    send-msg-timeout: 30000
  1. 根据需要实现:
复制代码
MQLogSupport、MQLog、MQLogRepository
相关推荐
学会沉淀。2 分钟前
Docker学习
java·开发语言·学习
如若1233 分钟前
对文件内的文件名生成目录,方便查阅
java·前端·python
初晴~33 分钟前
【Redis分布式锁】高并发场景下秒杀业务的实现思路(集群模式)
java·数据库·redis·分布式·后端·spring·
黑胡子大叔的小屋1 小时前
基于springboot的海洋知识服务平台的设计与实现
java·spring boot·毕业设计
ThisIsClark1 小时前
【后端面试总结】深入解析进程和线程的区别
java·jvm·面试
雷神乐乐2 小时前
Spring学习(一)——Sping-XML
java·学习·spring
小林coding3 小时前
阿里云 Java 后端一面,什么难度?
java·后端·mysql·spring·阿里云
V+zmm101343 小时前
基于小程序宿舍报修系统的设计与实现ssm+论文源码调试讲解
java·小程序·毕业设计·mvc·ssm
文大。3 小时前
2024年广西职工职业技能大赛-Spring
java·spring·网络安全
一只小小翠3 小时前
EasyExcel 模板+公式填充
java·easyexcel