<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                通過設置隊列的 TTL 來打造延時隊列,本章通過 SpringBoot 演示下圖的延遲隊列架構。 ![](https://img.kancloud.cn/bc/62/bc62dd079ec92e1c7d0aa043609164b5_1433x245.jpg) 創建兩個隊列 QA 和 QB,兩個隊列 TTL 分別設置為 10S 和 40S,然后在創建一個交換機 X 和死信交換機 Y,它們的類型都是 direct。再創建一個死信隊列 QD。 <br/> 步驟如下: **1. 創建SpringBoot項目** ```xml <dependencies> <!--RabbitMQ 依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <!--RabbitMQ 測試依賴--> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies> ``` **2. `resources/application.properties`** ```properties spring.rabbitmq.host=192.168.0.107 spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin ``` **3. 配置類** ```java @Configuration public class TtlQueueConfig { public static final String X_EXCHANGE = "X"; public static final String QUEUE_A = "QA"; public static final String QUEUE_B = "QB"; public static final String Y_DEAD_LETTER_EXCHANGE = "Y"; public static final String DEAD_LETTER_QUEUE = "QD"; @Bean("xExchange") public DirectExchange xExchange() { return new DirectExchange(X_EXCHANGE); } @Bean("yExchange") public DirectExchange yExchange() { return new DirectExchange(Y_DEAD_LETTER_EXCHANGE); } /** * 隊列 A 綁定到對應的死信交換機 */ @Bean("queueA") public Queue queueA() { Map<String, Object> args = new HashMap<>(16); //聲明當前隊列綁定的死信交換機 args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE); //聲明當前隊列的死信路由 key args.put("x-dead-letter-routing-key", "YD"); //聲明隊列的 TTL args.put("x-message-ttl", 10000); return QueueBuilder.durable(QUEUE_A).withArguments(args).build(); } /** * 隊列 A 綁定 X 交換機 */ @Bean public Binding queueaBindingX(@Qualifier("queueA") Queue queueA, @Qualifier("xExchange") DirectExchange xExchange) { // with(routingKey) return BindingBuilder.bind(queueA).to(xExchange).with("XA"); } /** * 隊列 B 綁定到對應的死信交換機 */ @Bean("queueB") public Queue queueB() { Map<String, Object> args = new HashMap<>(3); //聲明當前隊列綁定的死信交換機 args.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE); //聲明當前隊列的死信路由 key args.put("x-dead-letter-routing-key", "YD"); //聲明隊列的 TTL args.put("x-message-ttl", 40000); return QueueBuilder.durable(QUEUE_B).withArguments(args).build(); } /** * 隊列 B 綁定 X 交換機 */ @Bean public Binding queuebBindingX(@Qualifier("queueB") Queue queue1B, @Qualifier("xExchange") DirectExchange xExchange) { // with(routingKey) return BindingBuilder.bind(queue1B).to(xExchange).with("XB"); } @Bean("queueD") public Queue queueD() { return new Queue(DEAD_LETTER_QUEUE); } /** * 死信隊列 QD 綁定死信交換機Y */ @Bean public Binding deadLetterBindingQAD(@Qualifier("queueD") Queue queueD, @Qualifier("yExchange") DirectExchange yExchange) { // with(routingKey) return BindingBuilder.bind(queueD).to(yExchange).with("YD"); } } ``` **4. 生產者** ```java @Slf4j @RestController public class ProducerController { @Autowired private RabbitTemplate rabbitTemplate; @GetMapping("/ttl/send") public void ttlSend() { //convertAndSend(String exchange, String routingKey, Object object) rabbitTemplate.convertAndSend("X", "XA", "ttl為10S隊列消息"); rabbitTemplate.convertAndSend("X", "XB", "ttl為40S隊列消息"); } } ``` **5. 消費者** ```java @Slf4j @Component public class ConsumerService { @RabbitListener(queues = "QD") public void receiveD(Message message, Channel channel) throws IOException { String msg = new String(message.getBody(), "UTF-8"); log.info(msg); } } ``` **6. 測試** 啟動項目后訪問 http://localhost:8080/ttl/send 生產者將生產兩條消息。消費者消費的消息如下。 ``` 2022-10-27 21:58:26 : ttl為10S隊列消息 2022-10-27 21:58:56 : ttl為40S隊列消息 ``` 第一條消息在 10S 后變成了死信消息,然后被消費者消費掉。 第二條消息在 40S 之后變成了死信消息,然后被消費掉。 這樣一個延時隊列就打造完成了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看