死信队列与延迟队列

0、基本概念

死信队列
在这里插入图片描述延时队列
在这里插入图片描述在这里插入图片描述总结**在这里插入图片描述**

1、引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2、编写配置类,创建队列、交换机、绑定关系


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @Author: sgc
 * @Date: 2023/3/8 21:02
 */
@Configuration
public class RabbitConfig {

    //死信交换机名称
    private static final String dlxExchange = "dlxExchange-key-delay";
    //死信交换机路由
    private static final String routing_key_delay = "routing-key-delay";

    //创建延时队列 @bean后面的时间可以与durable中命名的时间不一致  @bean后面的用于引用时区分bean
    //durable中的是对队列的命名
    @Bean("delayQueue")
    public Queue delayQueue(){
        return QueueBuilder.durable("delayQueue")
                //如果消息过时 则会被投递到当前对应的交换机 dlxExchange
                .withArgument("x-dead-letter-exchange",dlxExchange)
                //如果消息过时 my-dlx-exchange交换机会根据routing-key-delay投递到对应的队列
                .withArgument("x-dead-letter-routing-key",routing_key_delay)
                .build();
    }

    //创建死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue(){
        return QueueBuilder.durable("dlxQueue").build();
    }

    //创建死信交换机
    @Bean("dexExchange")
    public Exchange dexExchange(){
        return ExchangeBuilder.directExchange(dlxExchange).build();
    }

    //绑定死信队列与死信交换机
    @Bean("dexBinding")
    public Binding dexBinding(@Qualifier("dlxQueue") Queue queue,
                              @Qualifier("dexExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(routing_key_delay).noargs();
    }

}

3、发送mq消息

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * @Description:
 * @Author: sgc
 * @Date: 2023/3/8 21:41
 */
@RequestMapping("/test")
@RestController
public class TestController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("sendMsg")
    public String sendMsg(){
        String msg = "hello rabbitMq";
        //发送消息到延迟队列

        //构造消息参数
        HashMap<String, Object> messageMap = new HashMap<>();
        messageMap.put("msg",msg);

        /**
         * 参数1:队列名称
         * 参数2:消息map
         * 参数3: 消息处理器 可设置 过期时间(秒)
         */
        rabbitTemplate.convertAndSend("delayQueue", messageMap, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //设置10s过期
                message.getMessageProperties().setExpiration("10000");
                return message;
            }
        });
        System.out.println("发送时间:"+ LocalDateTime.now());
        return "success";
    }

}

4、接收mq消息

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Description:
 * @Author: sgc 接受死信队列的消息
 * @Date: 2023/3/8 21:54
 */
@Component
@RabbitListener(queues = "dlxQueue")
public class MessageListener {

    /**
     * 注意 此处入参类型与发送的类型要一直 发map则接收map 发String则接收String
     * @param map
     */
    @RabbitHandler
    public void receiveMsg(Map<String,Object> map){
        System.out.println("接收时间:"+ LocalDateTime.now());
        System.out.println("接收到消息:"+map.get("msg"));
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值