spring cloud 集成rabbitMQ实现延时队列

假如环境已经配置好,现在我们有这么个需求,我们把消息发给消息队列后,并不希望马上消费这个消息,而是想等一段时间再让他消费,直接上代码吧:

mq配置文件:定义一些队列名称配置

@Configuration
public class QueueConfiguration {  
      
    //信道配置  
    @Bean
    public DirectExchange defaultExchange() {
        return new DirectExchange(MQConstant.DEFAULT_EXCHANGE, true, false);
    }
    @Bean
    public Queue repeatTradeQueue() {
        Queue queue = new Queue(MQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME,true,false,false);
        return queue;
    }

    @Bean
    public Binding  drepeatTradeBinding() {
        return BindingBuilder.bind(repeatTradeQueue()).to(defaultExchange()).with(MQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
    }

    @Bean
    public Queue deadLetterQueue() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", MQConstant.DEFAULT_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", MQConstant.DEFAULT_REPEAT_TRADE_QUEUE_NAME);
        Queue queue = new Queue(MQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME,true,false,false,arguments);
        System.out.println("arguments :" + queue.getArguments());
        return queue;
    }

    @Bean
    public Binding  deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(defaultExchange()).with(MQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME);
    }
      
    /*********************    hello 队列  测试    *****************/  
    @Bean  
    public Queue queue() {
        Queue queue = new Queue(MQConstant.HELLO_QUEUE_NAME,true);  
        return queue;   
    }  
  
    @Bean  
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(defaultExchange()).with(MQConstant.HELLO_QUEUE_NAME);
    }  
  
}  

队列常量:

public final class MQConstant {
      
    private MQConstant(){  
    }  
      
    //exchange name  
    public static final String DEFAULT_EXCHANGE = "test-exchange";
      
    //DLX QUEUE  
    public static final String DEFAULT_DEAD_LETTER_QUEUE_NAME = "kshop.dead.letter.queue";  
      
    //DLX repeat QUEUE 死信转发队列  
    public static final String DEFAULT_REPEAT_TRADE_QUEUE_NAME = "kshop.repeat.trade.queue";  
      
      
    //Hello 测试消息队列名称  
    public static final String HELLO_QUEUE_NAME = "test-queue-name";
      
      
} 

 

消息类:

public class DLXMessage implements Serializable {

    private static final long serialVersionUID = 9956432152000L;  

    public DLXMessage() {  
        super();  
    }  

    public DLXMessage(String queueName, String content, long times) {  
        super();  
        this.queueName = queueName;  
        this.content = content;  
        this.times = times;  
    }  

    public DLXMessage(String exchange, String queueName, String content, long times) {  
        super();  
        this.exchange = exchange;  
        this.queueName = queueName;  
        this.content = content;  
        this.times = times;  
    }  


    private String exchange;  

    private String queueName;  

    private String content;  

    private long times;  

    //省略getter setter  

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getExchange() {
        return exchange;
    }

    public void setExchange(String exchange) {
        this.exchange = exchange;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public long getTimes() {
        return times;
    }

    public void setTimes(long times) {
        this.times = times;
    }
}

消息发送对象:

@Service("messageQueueService")
public class MessageQueueServiceImpl implements IMessageQueueService {
      
    @Autowired
    private RabbitTemplate rabbitTemplate;
  
    @Override  
    public void send(String queueName, String msg) {  
        rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE,queueName, msg);
    }

    @Override
    public void send(String queueName, String message, long times) {

        DLXMessage dlxMessage = new DLXMessage(queueName,message,times);
        MessagePostProcessor processor = new MessagePostProcessor(){
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(times + "");
                return message;
            }
        };
        dlxMessage.setExchange(MQConstant.DEFAULT_EXCHANGE);
        rabbitTemplate.convertAndSend(MQConstant.DEFAULT_EXCHANGE,MQConstant.DEFAULT_DEAD_LETTER_QUEUE_NAME, JSON.toJSONString(dlxMessage), processor);


    }


}  

 

posted @ 2019-10-22 15:29  蜗牛的信仰  阅读(813)  评论(0编辑  收藏  举报