admin
2024-10-16 bd885c7015446c6c0495d3299ef64068a4c9b30e
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
package com.ks.push.manager.rabbitmq;
 
import com.google.gson.Gson;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 org.yeshi.utils.StringUtil;
 
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
 
/**
 * @author hxh
 * @title: RabbitDelayConsumer
 * @description: 延时队列消费处理
 * @date 2024/10/12 11:21
 */
@Component
public class RabbitDelayConsumer  {
 
   private  Logger logger = LoggerFactory.getLogger("infoLog");
 
    @Resource
    private RabbitTemplate rabbitTemplate;
 
 
    @RabbitListener(queues = RabbitmqSenderUtil.DELAY_QUEUE_NAME, ackMode = "MANUAL")
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg =  new String( message.getBody(), StandardCharsets.UTF_8);
        logger.info("RabbitDelayConsumer-{}",msg);
        DelayMsgInfo msgInfo =  new Gson().fromJson(msg, DelayMsgInfo.class);
        if(!StringUtil.isNullOrEmpty(msgInfo.getQueueName())){
          // 队列消息
            RabbitmqSenderUtil.sendQueueMsg(rabbitTemplate, msgInfo.getQueueName(), msgInfo.getMsg());
        }else{
            // 交换机消息
            RabbitmqSenderUtil.sendExchangeMsg(rabbitTemplate, msgInfo.getExchangeName(), msgInfo.getRoutingKey(), msgInfo.getMsg());
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}