当前位置:首页 > Java 框架原理百科 > 正文

Java优学网SpringBoot整合RabbitMQ讲解:轻松实现订单超时自动处理

org.springframework.boot spring-boot-starter-amqp

spring: rabbitmq:

host: 127.0.0.1
port: 5672
username: admin
password: 123456
virtual-host: /dev
connection-timeout: 5000
template:
  retry:
    enabled: true
    initial-interval: 1000
    max-attempts: 3

@Configuration public class DelayQueueConfig {

// 订单超时交换机
@Bean
public DirectExchange orderTimeoutExchange() {
    return new DirectExchange("order.timeout.exchange");
}

// 延迟队列,设置5分钟TTL
@Bean
public Queue orderDelayQueue() {
    Map<String, Object> args = new HashMap<>();
    args.put("x-dead-letter-exchange", "order.timeout.exchange");
    args.put("x-dead-letter-routing-key", "order.timeout");
    args.put("x-message-ttl", 300000); // 5分钟
    return new Queue("order.delay.queue", true, false, false, args);
}

// 实际处理超时订单的队列
@Bean
public Queue orderTimeoutQueue() {
    return new Queue("order.timeout.queue", true);
}

@Bean
public Binding timeoutBinding() {
    return BindingBuilder.bind(orderTimeoutQueue())
                       .to(orderTimeoutExchange())
                       .with("order.timeout");
}

}

Java优学网SpringBoot整合RabbitMQ讲解:轻松实现订单超时自动处理

你可能想看:

相关文章:

文章已关闭评论!