admin
2024-10-16 62a447d89331aee1feae7724c7616aa1bb2cfe79
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
package com.yeshi.fanli.util.mq.rabbit.consumer;
 
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import com.yeshi.fanli.entity.bus.user.HongBaoV2;
import com.yeshi.fanli.exception.order.TaoBaoWeiQuanException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.OrderProcessService;
import com.yeshi.fanli.util.RedisKeyEnum;
import com.yeshi.fanli.util.RedisManager;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.mq.rabbit.RabbitmqMsgConsumeUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
 
/**
 * @author hxh
 * @title: QueueHelloWorldListener
 * @description:
 * @date 2024/9/26 13:47
 */
@Component
public class FanliOrderNewListener {
    @Resource
    private RabbitTemplate rabbitTemplate;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private OrderProcessService orderProcessService;
 
 
    @RabbitListener(queues = "fanli-order-new-fanli", ackMode = "MANUAL")
    public void onMessage(Message message, Channel channel) throws Exception {
        RabbitmqMsgConsumeUtil.processMessage(message, channel, rabbitTemplate, () -> {
            String result = new String(message.getBody(), StandardCharsets.UTF_8);
            if (!StringUtil.isNullOrEmpty(result)) {
                HongBaoV2 hongbao = new Gson().fromJson(result, HongBaoV2.class);
                try {
                    if (hongbao != null) {
                        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.hongBaoFanLi,
                                hongbao.getId() + "");
                        if (StringUtil.isNullOrEmpty(redisManager.getCommonString(key))) {
                            // 处理之后要隔2小时再次进行处理
                            redisManager.cacheCommonString(key, "1", 60 * 60 * 2);
                            orderProcessService.fanli(hongbao);
                        }
                    }
                } catch (TaoBaoWeiQuanException e) {
                    try {
                        LogHelper.errorDetailInfo(e);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    throw e;
                } catch (Exception e) {
                    try {
                        LogHelper.errorDetailInfo(e, "HongBaoV2-ID:" + hongbao.getId(), "");
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    throw e;
                }
            }
        });
    }
}