KKSU
2025-02-12 1bb96bab1185f2e1aa2f468266512ee0beb83286
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
package cc.mrbird.febs.rabbit.producter;
 
import cc.mrbird.febs.mall.dto.ApiMemberChargeFailDto;
import cc.mrbird.febs.rabbit.enumerates.RabbitQueueEnum;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
/**
 * @author wzy
 * @date 2021-09-25
 **/
@Slf4j
@Component
public class AgentProducer implements RabbitTemplate.ConfirmCallback {
 
    /**
     * 配置中配置的RabbitTemplate的是prototype类型,不能直接注入
     */
    private RabbitTemplate rabbitTemplate;
 
    /**
     * 在构造方法上注入RabbitTemplate
     *
     * @param
     */
    @Autowired
    public AgentProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
        rabbitTemplate.setConfirmCallback(this);
    }
 
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
 
    }
 
    /**
     * 购买VIP自动过期
     * @param apiMemberChargeFailDto
     */
    public void sendMemberChargeFailMsg(ApiMemberChargeFailDto apiMemberChargeFailDto) {
        log.info("发送购买VIP自动过期: {}", JSON.toJSONString(apiMemberChargeFailDto));
        rabbitTemplate.convertAndSend(
                RabbitQueueEnum.RUN_VIP_OPERATION_CHARGE_FAIL_TTL.getExchange(),
                RabbitQueueEnum.RUN_VIP_OPERATION_CHARGE_FAIL_TTL.getRoute(),
                apiMemberChargeFailDto,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setExpiration(String.valueOf(apiMemberChargeFailDto.getFailTime()));
                        return message;
                    }
                });
    }
 
    public void sendBuyVipSuccessMsg(Long id) {
        log.info("发送购买成功消息:{}",id);
        rabbitTemplate.convertAndSend(
                RabbitQueueEnum.RUN_VIP_OPERATION_CHARGE.getExchange(),
                RabbitQueueEnum.RUN_VIP_OPERATION_CHARGE.getRoute(),
                id);
    }
 
    public void sendChargeSuccessMsg(Long id) {
        log.info("发送充值成功消息:{}",id);
        rabbitTemplate.convertAndSend(
                RabbitQueueEnum.RUN_VIP_OPERATION_CHARGE_BALANCE.getExchange(),
                RabbitQueueEnum.RUN_VIP_OPERATION_CHARGE_BALANCE.getRoute(),
                id);
    }
 
    public void sendNodeUpMsg(Long memberId) {
        log.info("发送节点升级消息:{}",memberId);
        rabbitTemplate.convertAndSend(
                RabbitQueueEnum.RUN_VIP_NODE_UP.getExchange(),
                RabbitQueueEnum.RUN_VIP_NODE_UP.getRoute(),
                memberId);
    }
}