xuhy
2024-09-10 09d010da12e5dd22766cf0979f8f34c968ab6ffd
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.ruoyi.integration.rocket.listener;
 
import com.ruoyi.chargingPile.api.dto.CheckChargingStrategyDTO;
import com.ruoyi.chargingPile.api.feignClient.AccountingStrategyDetailClient;
import com.ruoyi.chargingPile.api.model.TAccountingStrategyDetail;
import com.ruoyi.integration.api.model.AcquisitionBillingMode;
import com.ruoyi.integration.api.model.BillingModeVerify;
import com.ruoyi.integration.api.model.BillingModeVerifyReply;
import com.ruoyi.integration.iotda.enums.ServiceIdMenu;
import com.ruoyi.integration.iotda.utils.produce.IotMessageProduce;
import com.ruoyi.integration.iotda.utils.tools.MessageUtil;
import com.ruoyi.integration.mongodb.service.BillingModeVerifyReplyService;
import com.ruoyi.integration.mongodb.service.BillingModeVerifyService;
import com.ruoyi.integration.rocket.model.BillingModeVerifyMessage;
import com.ruoyi.integration.rocket.util.EnhanceMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Slf4j
@Component
@RocketMQMessageListener(
        messageModel = MessageModel.CLUSTERING,
        consumerGroup = "charge_billing_mode_verify",
        topic = "charge_billing_mode_verify",
        selectorExpression = "billing_mode_verify",
        consumeThreadMax = 5 //默认是64个线程并发消息,配置 consumeThreadMax 参数指定并发消费线程数,避免太大导致资源不够
)
public class BillingModeVerifyMessageListener extends EnhanceMessageHandler<BillingModeVerifyMessage> implements RocketMQListener<BillingModeVerifyMessage> {
 
    @Autowired
    private BillingModeVerifyService billingModeVerifyService;
    @Autowired
    private IotMessageProduce iotMessageProduce;
    @Autowired
    private MessageUtil messageUtil;
    @Autowired
    private AccountingStrategyDetailClient accountingStrategyDetailClient;
 
    @Override
    protected void handleMessage(BillingModeVerifyMessage message) throws Exception {
        // 此时这里才是最终的业务处理,代码只需要处理资源类关闭异常,其他的可以交给父类重试
        log.info("计费模型验证请求-业务消息处理:{}",message);
        // 持久化消息
        BillingModeVerify billingModeVerify = new BillingModeVerify();
        BeanUtils.copyProperties(message,billingModeVerify);
        billingModeVerifyService.create(billingModeVerify);
        // 业务处理
        BillingModeVerifyReply billingModeVerifyReply = new BillingModeVerifyReply();
        if(message.getBilling_model_code().equals("0")){
            // 首次
            billingModeVerifyReply.setCharging_pile_code(billingModeVerify.getCharging_pile_code());
            billingModeVerifyReply.setBilling_model_code("0");
            billingModeVerifyReply.setBilling_model_result(1);
        }else {
            // 查询桩使用的模版
            CheckChargingStrategyDTO dto = new CheckChargingStrategyDTO();
            dto.setCode(message.getBilling_model_code());
            dto.setStrategyDetailId(Integer.valueOf(message.getBilling_model_code()));
            Boolean check = accountingStrategyDetailClient.checkChargingStrategy(dto).getData();
            // 校验计费模版是否准确
            billingModeVerifyReply.setCharging_pile_code(billingModeVerify.getCharging_pile_code());
            billingModeVerifyReply.setBilling_model_code(message.getBilling_model_code());
            if(check){
                billingModeVerifyReply.setBilling_model_result(0);
            }else {
                billingModeVerifyReply.setBilling_model_result(1);
            }
        }
        iotMessageProduce.sendMessage(billingModeVerifyReply.getCharging_pile_code(), ServiceIdMenu.BILLING_MODE_VERIFY_REPLY.getKey(),messageUtil.billingModeVerifyReply(billingModeVerifyReply));
    }
 
    @Override
    protected void handleMaxRetriesExceeded(BillingModeVerifyMessage message) {
        // 当超过指定重试次数消息时此处方法会被调用
        // 生产中可以进行回退或其他业务操作
        log.error("消息消费失败,请执行后续处理");
    }
 
 
    /**
     * 是否执行重试机制
     */
    @Override
    protected boolean isRetry() {
        return true;
    }
 
    @Override
    protected boolean throwException() {
        // 是否抛出异常,false搭配retry自行处理异常
        return false;
    }
 
    /**
     * 若需要处理消息过滤,在父级中进行统一处理,或者在此处实现之后,自行处理
     * @param message 待处理消息
     * @return true: 本次消息被过滤,false:不过滤
     */
    @Override
    protected boolean filter(BillingModeVerifyMessage message) {
        // 此处可做消息过滤
        return false;
    }
 
    /**
     * 监听消费消息,不需要执行业务处理,委派给父类做基础操作,父类做完基础操作后会调用子类的实际处理类型
     */
    @Override
    public void onMessage(BillingModeVerifyMessage message) {
        super.dispatchMessage(message);
    }
}