郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.gkhy.safePlatform.specialWork.mq.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.JsonUtils;
import com.gkhy.safePlatform.specialWork.mq.msg.AnalysisExpireMsg;
import com.gkhy.safePlatform.specialWork.mq.service.WorkAnalysisProducerService;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
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.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
 
@Service("workAnalysisProducerService")
public class WorkAnalysisProducerServiceImpl implements WorkAnalysisProducerService {
 
 
    private static final String analysisExpireTopic = "analysisExpireTopic";
 
    private static Logger log = LoggerFactory.getLogger(WorkAnalysisProducerService.class);
 
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
 
 
    @Override
    public SendResult syncSendWorkExpireMsg(AnalysisExpireMsg message) {
        // 发送消息生成唯一标识
        message.setMessageKey("MUKey_" + IdUtil.getSnowflake(0, 0).nextIdStr());
        // 调用 template 发送同步消息
        SendResult sendResult = rocketMQTemplate.syncSend(analysisExpireTopic, MessageBuilder.withPayload(message).build());
        // 没发送成功 抛出异常
        if (sendResult.getSendStatus() == SendStatus.SEND_OK) {
            return sendResult;
        }else{
            throw new BusinessException("S0001", "发送超时任务失败");
        }
 
    }
 
    @Override
    public SendResult syncSendWorkExpireMsgDelay(AnalysisExpireMsg message, int delayLevel) {
        // 发送消息生成唯一标识
        message.setMessageKey("MUKey_" + IdUtil.getSnowflake(0, 0).nextIdStr());
        // 调用 template 发送同步消息
        SendResult sendResult = rocketMQTemplate.syncSend(analysisExpireTopic, MessageBuilder.withPayload(message).build(), 3000, delayLevel);
 
        // 没发送成功 抛出异常
        if (sendResult.getSendStatus() == SendStatus.SEND_OK) {
            return sendResult;
        }else{
            throw new BusinessException("S0001", "发送超时任务失败");
        }
    }
}