rabbitmq实现延迟消息

pom

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

配置application.properties


server.port=8181
spring.rabbitmq.addresses=192.168.75.128
spring.rabbitmq.port=15672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

配置订单队列,并配置相应的exchange和routingKey

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderQueueConfig {

    /**
     * 订单队列
     */
    public static final String ORDER_QUEUE = "woniu.order.queue";

    /**
     * 超时订单队列
     */
    public static final String ORDER_EXCHANGE = "woniu.order.exchage";

    /**
     * 订单exchange
     */
    public static final String ORDER_TIMEOUT_QUEUE = "woniu.order.timeout.queue";


    /**
     * 订单exchange
     */
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(ORDER_EXCHANGE,true,false,null);
    }

    /**
     * 订单队列
     */
    @Bean
    public Queue orderQueue() {
        // 设置超时转发策略 超时后消息会通过x-dead-letter-exchange 转发到x-dead-letter-routing-key绑定的队列中
        Map<String, Object> arguments = new HashMap<>(2);
        arguments.put("x-dead-letter-exchange", ORDER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", ORDER_TIMEOUT_QUEUE);
        Queue queue = new Queue(ORDER_QUEUE,true,false,false,arguments);
        return queue;
    }

    /**
     * 超时订单队列
     * @return
     */
    @Bean
    public Queue orderTimeoutQueue() {
        Queue queue = new Queue(ORDER_TIMEOUT_QUEUE,true,false,false);
        return queue;
    }

    /**
     * 订单队列绑定exchange
     * @return
     */
    @Bean
    public Binding orderQueueBinding() {
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(ORDER_QUEUE);
    }


    /**
     * 超时订单队列绑定exchange
     * @return
     */
    @Bean
    public Binding  orderTimeoutQueueBinding() {
        return BindingBuilder.bind(orderTimeoutQueue()).to(orderExchange()).with(ORDER_TIMEOUT_QUEUE);
    }
}

创建消费者,消费超时队列

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DirectReceiver {
    @RabbitHandler
    @RabbitListener(queues = OrderQueueConfig.ORDER_TIMEOUT_QUEUE)
    public void consumeTimeOutQueue(@Payload String orderId){
        System.out.println("接收到消息的时间"+System.currentTimeMillis());
        System.out.println("消息ID="+orderId);
    }
    
}

创建生产者,发送消息

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.RestController;

@RestController
public class controller {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("mq")
    public void send(){
        rabbitTemplate.convertAndSend(OrderQueueConfig.ORDER_EXCHANGE, OrderQueueConfig.ORDER_QUEUE, "1", message -> {
            // 设置超时时间 3000ms
            message.getMessageProperties().setExpiration("3000");
            return message;
        });
    }
}

启动访问:http://ip:8181/mq

延迟3秒接收到消息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今朝花落悲颜色

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值