ribbonmq超时配置_使用RabbitMQ实现订单超时取消(延迟队列)

本文介绍了如何使用RabbitMQ实现订单超时取消功能,通过创建延迟队列和死信队列,设置消息TTL,当时间到期后消息将被投递至死信队列,由消费者进行取消订单操作。详细阐述了配置交换机、队列、绑定关系以及生产者和消费者的实现代码。
摘要由CSDN通过智能技术生成

使用RabbitMQ实现订单超时取消,大致流程:

生产者生产一条设置了TTL的延迟取消订单消息=>延迟队列交换机(通过绑定路由键)=>消息投递至延迟队列=>消息延迟队列时间到期=>经过死信队列交换机(通过绑定路由键)=>投递至死信队列=>消费者监听死信队列消息即时消费(做取消订单逻辑)。

下面来看代码:

一、先声明交换机、队列以及他们的绑定关系:

@Configurationpublic classRabbitMQConfig {//声明延时队列交换机

public static final String DELAY_EXCHANGE_NAME = "delay.queue.demo.business.exchange";//延时队列c

public static final String DELAY_QUEUEC_NAME = "delay.queue.demo.business.queuec";//延时队列c路由key

public static final String DELAY_QUEUEC_ROUTING_KEY = "delay.queue.demo.business.queuec.routingkey";//声明死信队列交换机

public static final String DEAD_LETTER_EXCHANGE = "delay.queue.demo.deadletter.exchange";//死信队列c

public static final String DEAD_LETTER_QUEUEC_NAME = "delay.queue.demo.deadletter.queuec";//死信交换机 的 不设时间路由key

public static final String DEAD_LETTER_QUEUEC_ROUTING_KEY = "delay.queue.demo.deadletter.delay_anytime.routingkey";//声明延时Exchange

@Bean("delayExchange")publicDirectExchange delayExchange(){return newDirectExchange(DELAY_EXCHANGE_NAME);

}//声明死信Exchange

@Bean("deadLetterExchange")publicDirectExchange deadLetterExchange(){return newDirectExchange(DEAD_LETTER_EXCHANGE);

}//声明延时队列C 不设置TTL//并绑定到对应的死信交换机

@Bean("delayQueueC")publicQueue delayQueueC(){

Map args = new HashMap<>(3);//x-dead-letter-exchange 这里声明当前队列绑定的死信交换机

args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);//x-dead-letter-routing-key 这里声明当前队列的死信路由key

args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUEC_ROUTING_KEY);returnQueueBuilder.durable(DELAY_QUEUEC_NAME).withArguments(args).build();

}//声明死信队列C 用于接收延时任意时长处理的消息

@Bean("deadLetterQueueC")publicQueue deadLetterQueueC(){return newQueue(DEAD_LETTER_QUEUEC_NAME);

}//声明延时队列C绑定关系

@Beanpublic Binding delayBindingC(@Qualifier("delayQueueC") Queue queue,

@Qualifier("delayExchange") DirectExchange exchange){returnBindingBuilder.bind(queue).to(exchange).with(DELAY_QUEUEC_ROUTING_KEY);

}//声明死信队列C绑定关系

@Beanpublic Binding deadLetterBindingC(@Qualifier("deadLetterQueueC") Queue queue,

@Qualifier("deadLetterExchange") DirectExchange exchange){returnBindingBuilder.bind(queue).to(exchange).with(DEAD_LETTER_QUEUEC_ROUTING_KEY);

}

}

二、设置延迟队列配置绑定关系

@Configurationpublic classDelayedRabbitMQConfig {public static final String DELAYED_QUEUE_NAME = "delay.queue.demo.delay.queue";public static final String DELAYED_EXCHANGE_NAME = "delay.queue.demo.delay.exchange";public static final String DELAYED_ROUTING_KEY = "delay.queue.demo.delay.routingkey";

@BeanpublicQueue immediateQueue() {return newQueue(DELAYED_QUEUE_NAME);

}

@BeanpublicCustomExchange customExchange() {

Map args = new HashMap<>();

args.put("x-delayed-type", "direct");return new CustomExchange(DELAYED_EXCHANGE_NAME, "x-delayed-message", true, false, args);

}

@Beanpublic Binding bindingNotify(@Qualifier("immediateQueue") Queue queue,

@Qualifier("customExchange") CustomExchange customExchange) {returnBindingBuilder.bind(queue).to(customExchange).with(DELAYED_ROUTING_KEY).noargs();

}

}

三、生产者发送消息

@Componentpublic classDelayMessageSender {

@AutowiredprivateRabbitTemplate rabbitTemplate;public voidsendDelayMsg(String msg, Integer delayTime) {

rabbitTemplate.convertAndSend(DELAYED_EXCHANGE_NAME, DELAYED_ROUTING_KEY, msg, a->{

a.getMessageProperties().setDelay(delayTime);returna;

});

}

}

四、设置消费者监听

@Slf4j

@Componentpublic classDeadLetterQueueConsumer {

@RabbitListener(queues=DELAYED_QUEUE_NAME)public void receiveD(Message message, Channel channel) throwsIOException {

String msg= newString(message.getBody());

log.info("当前时间:{},取消订单,msg:{}",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), msg); channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

}

}

五、调用接口

@Slf4j

@RequestMapping("rabbitmq")

@RestControllerpublic classRabbitMQMsgController {

@AutowiredprivateDelayMessageSender sender;/*** 发送延迟取消订单消息

*@parammsg 消息体

*@paramdelayTime 自定义延迟取消订单时间(毫秒)*/@RequestMapping("delayMsg")public voiddelayMsg2(String msg, Integer delayTime) {

msg= msg +"=>"+ (int)(Math.random() * 90000.0D + 10000.0D);

log.info("当前时间:{},生成订单,msg:{},delayTime:{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newDate()), msg, delayTime);

sender.sendDelayMsg(msg, delayTime);

}

}

输出结果:我设置的是5秒后取消订单

rmq配置:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值