springboot整合rabbitmq利用延迟插件发送延时消息

1.需求

利用rabbitmq的死信交换机构建延时队列,在容器启动的时候往死信交换机发送一条消息,一分钟后监听者接收到消息并消费。

2.依赖

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

3.配置文件

spring.rabbitmq.host=121.199.31.160
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=root
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
#默认情况下消息消费者是自动确认消息的,如果要手动确认消息则需要修改确认模式为manual
spring.rabbitmq.listener.simple.cknowledge-mode=manual
# 消费者每次从队列获取的消息数量。此属性当不设置时为:轮询分发,设置为1为:公平分发
spring.rabbitmq.listener.simple.prefetch=1

4.编写配置类

/**
 * @author yhd
 * @createtime 2021/1/22 14:37
 */
@SpringBootApplication
public class DelayConfig {

	//延时交换机
    public static final String EXCHANGE_DIRECT_ORDER_CANCEL = "spring.boot.test.delay.exchange";
    //路由键
    public static final String ROUTING_ORDER_CANCEL = "spring.boot.test.delay.routing";
    //延迟队列
    public static final String QUEUE_ORDER_CANCEL  = "spring.boot.test.delay.queue";
    // 延迟时间 单位:秒
    public static final int DELAY_TIME  = 60;

    @Bean //声明死信队列
    public Queue delayQueue() {
        // 第一个参数是创建的queue的名字,第二个参数是是否支持持久化
        return new Queue(QUEUE_ORDER_CANCEL, true);
    }

    @Bean //声明私信交换机
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(EXCHANGE_DIRECT_ORDER_CANCEL, "x-delayed-message", true, false, args);
    }

    @Bean //死信交换机绑定死信队列并设置路由键
    public Binding bindingDelay() {
        return BindingBuilder.bind(delayQueue()).to(delayExchange()).with(ROUTING_ORDER_CANCEL).noargs();
    }
}

5.编写消息发送方和接收方

/**
 * @author yhd
 * @createtime 2021/1/22 14:34
 * 测试springboot整合mq利用死信队列发送消息并接收
 */
@Component
@Slf4j
public class SpringBootDelayQueueTest {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private AmqpTemplate amqpTemplate;

    /**
     * 发送消息
     */
    public void sendMessage() {
        amqpTemplate.convertAndSend(
                DelayConfig.EXCHANGE_DIRECT_ORDER_CANCEL,DelayConfig.ROUTING_ORDER_CANCEL,
                "try send message to delay queue !",
                msg -> {
                    msg.getMessageProperties().setDelay(DelayConfig.DELAY_TIME * 1000);
                    return msg;
                });

    }

    /**
     * 接收消息
     */
    @RabbitListener(queues = DelayConfig.QUEUE_ORDER_CANCEL)
    public void receiveMessage(String msg, Message message, Channel channel) throws Exception {
        log.info("the delaty queue received message : {}", msg);
        //log.info("the delaty queue received message : {}", new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}

6.容器启动发送消息

/**
 * @author yhd
 * @createtime 2021/1/22 14:56
 */
@Component
@Slf4j
public class BootMq implements ApplicationRunner {

    @Resource
    private SpringBootDelayQueueTest springBootDelayQueueTest;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("发送私信");
        springBootDelayQueueTest.sendMessage();
    }
}

7.结果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值