使用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配置: