最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合RabbitMQ處理死信隊列和延遲隊列

 更新時間:2022年05月28日 09:23:33   作者:IT利刃出鞘  
這篇文章將通過示例為大家詳細介紹SpringBoot整合RabbitMQ時如何處理死信隊列和延遲隊列,文中的示例代碼講解詳細,需要的可以參考一下

簡介

說明

本文用示例介紹SpringBoot整合RabbitMQ時如何處理死信隊列/延遲隊列。

RabbitMQ消息簡介

RabbitMQ的消息默認不會超時。 

什么是死信隊列?什么是延遲隊列?

死信隊列:

DLX,全稱為Dead-Letter-Exchange,可以稱之為死信交換器,也有人稱之為死信郵箱。當消息在一個隊列中變成死信(dead message)之后,它能被重新被發(fā)送到另一個交換器中,這個交換器就是DLX,綁定DLX的隊列就稱之為死信隊列。

以下幾種情況會導致消息變成死信:

  • 消息被拒絕(Basic.Reject/Basic.Nack),并且設置requeue參數為false;
  • 消息過期;
  • 隊列達到最大長度。

延遲隊列:

延遲隊列用來存放延遲消息。延遲消息:指當消息被發(fā)送以后,不想讓消費者立刻拿到消息,而是等待特定時間后,消費者才能拿到這個消息進行消費。

相關網址

詳解RabbitMQ中死信隊列和延遲隊列的使用詳解

實例代碼

路由配置

package com.example.config;
 
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class RabbitRouterConfig {
    public static final String EXCHANGE_TOPIC_WELCOME   = "Exchange@topic.welcome";
    public static final String EXCHANGE_FANOUT_UNROUTE  = "Exchange@fanout.unroute";
    public static final String EXCHANGE_TOPIC_DELAY     = "Exchange@topic.delay";
 
    public static final String ROUTINGKEY_HELLOS        = "hello.#";
    public static final String ROUTINGKEY_DELAY         = "delay.#";
 
    public static final String QUEUE_HELLO              = "Queue@hello";
    public static final String QUEUE_HI                 = "Queue@hi";
    public static final String QUEUE_UNROUTE            = "Queue@unroute";
    public static final String QUEUE_DELAY              = "Queue@delay";
 
    public static final Integer TTL_QUEUE_MESSAGE       = 5000;
 
    @Autowired
    AmqpAdmin amqpAdmin;
 
    @Bean
    Object initBindingTest() {
        amqpAdmin.declareExchange(ExchangeBuilder.fanoutExchange(EXCHANGE_FANOUT_UNROUTE).durable(true).autoDelete().build());
        amqpAdmin.declareExchange(ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_DELAY).durable(true).autoDelete().build());
        amqpAdmin.declareExchange(ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_WELCOME)
                .durable(true)
                .autoDelete()
                .withArgument("alternate-exchange", EXCHANGE_FANOUT_UNROUTE)
 
                .build());
 
        amqpAdmin.declareQueue(QueueBuilder.durable(QUEUE_HI).build());
        amqpAdmin.declareQueue(QueueBuilder.durable(QUEUE_HELLO)
                .withArgument("x-dead-letter-exchange", EXCHANGE_TOPIC_DELAY)
                .withArgument("x-dead-letter-routing-key", ROUTINGKEY_DELAY)
                .withArgument("x-message-ttl", TTL_QUEUE_MESSAGE)
                .build());
        amqpAdmin.declareQueue(QueueBuilder.durable(QUEUE_UNROUTE).build());
        amqpAdmin.declareQueue(QueueBuilder.durable(QUEUE_DELAY).build());
 
        amqpAdmin.declareBinding(new Binding(QUEUE_HELLO, Binding.DestinationType.QUEUE,
                EXCHANGE_TOPIC_WELCOME, ROUTINGKEY_HELLOS, null));
        amqpAdmin.declareBinding(new Binding(QUEUE_UNROUTE, Binding.DestinationType.QUEUE,
                EXCHANGE_FANOUT_UNROUTE, "", null));
        amqpAdmin.declareBinding(new Binding(QUEUE_DELAY, Binding.DestinationType.QUEUE,
                EXCHANGE_TOPIC_DELAY, ROUTINGKEY_DELAY, null));
 
        return new Object();
    }
}

控制器

package com.example.controller;
 
import com.example.config.RabbitRouterConfig;
import com.example.mq.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.time.LocalDateTime;
 
@RestController
public class HelloController {
    @Autowired
    private Sender sender;
 
    @PostMapping("/hi")
    public void hi() {
        sender.send(RabbitRouterConfig.QUEUE_HI, "hi1 message:" + LocalDateTime.now());
    }
 
    @PostMapping("/hello1")
    public void hello1() {
        sender.send("hello.a", "hello1 message:" + LocalDateTime.now());
    }
 
    @PostMapping("/hello2")
    public void hello2() {
        sender.send(RabbitRouterConfig.EXCHANGE_TOPIC_WELCOME, "hello.b", "hello2 message:" + LocalDateTime.now());
    }
 
    @PostMapping("/ae")
    public void aeTest() {
        sender.send(RabbitRouterConfig.EXCHANGE_TOPIC_WELCOME, "nonono", "ae message:" + LocalDateTime.now());
    }
}

發(fā)送器

package com.example.mq;
 
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
@Component
public class Sender {
    @Autowired
    private AmqpTemplate rabbitTemplate;
 
    public void send(String routingKey, String message) {
        this.rabbitTemplate.convertAndSend(routingKey, message);
    }
 
    public void send(String exchange, String routingKey, String message) {
        this.rabbitTemplate.convertAndSend(exchange, routingKey, message);
    }
}

接收器

package com.example.mq;
 
import com.example.config.RabbitRouterConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
 
@Component
public class Receiver {
    @RabbitListener(queues = RabbitRouterConfig.QUEUE_HI)
    public void hi(String payload) {
        System.out.println ("Receiver(hi) : "  + payload);
    }
 
    // @RabbitListener(queues = RabbitRouterConfig.QUEUE_HELLO)
    // public void hello(String hello) throws InterruptedException {
    //     System.out.println ("Receiver(hello) : "  + hello);
    //     Thread.sleep(5 * 1000);
    //     System.out.println("(hello):sleep over");
    // }
    //
    // @RabbitListener(queues = RabbitRouterConfig.QUEUE_UNROUTE)
    // public void unroute(String hello) throws InterruptedException {
    //     System.out.println ("Receiver(unroute) : "  + hello);
    //     Thread.sleep(5 * 1000);
    //     System.out.println("(unroute):sleep over");
    // }
 
    @RabbitListener(queues = RabbitRouterConfig.QUEUE_DELAY)
    public void delay(String hello) throws InterruptedException {
        System.out.println ("Receiver(delay) : "  + hello);
        Thread.sleep(5 * 1000);
        System.out.println("(delay):sleep over");
    }
}

application.yml

server:
#  port: 9100
  port: 9101
spring:
  application:
#    name: demo-rabbitmq-sender
    name: demo-rabbitmq-receiver
  rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: 123456
#    virtualHost: /
    publisher-confirms: true
    publisher-returns: true
#    listener:
#      simple:
#        acknowledge-mode: manual
#      direct:
#        acknowledge-mode: manual

實例測試

分別啟動發(fā)送者和接收者。

訪問:http://localhost:9100/hello2

五秒鐘后輸出:

Receiver(delay) : hello2 message:2020-11-27T09:30:51.548
(delay):sleep over

以上就是SpringBoot整合RabbitMQ處理死信隊列和延遲隊列的詳細內容,更多關于SpringBoot RabbitMQ死信隊列 延遲隊列的資料請關注腳本之家其它相關文章!

相關文章

  • SpringBoot中@ComponentScan注解過濾排除不加載某個類的3種方法

    SpringBoot中@ComponentScan注解過濾排除不加載某個類的3種方法

    這篇文章主要給大家介紹了關于SpringBoot中@ComponentScan注解過濾排除不加載某個類的3種方法,文中通過實例代碼介紹的非常詳細,對大家學習或者使用SpringBoot具有一定的參考學習價值,需要的朋友可以參考下
    2023-07-07
  • springboot Rabbit MQ topic 配置文件綁定隊列和交換機的實現方法

    springboot Rabbit MQ topic 配置文件綁定隊列和交換機的

    本文詳細講解了在SpringBoot中使用RabbitMQ進行隊列與交換機的綁定方法,包括創(chuàng)建交換機、隊列和綁定它們的步驟,以及如何發(fā)送和接收消息,適用于開發(fā)高并發(fā)系統(tǒng),如秒殺系統(tǒng)等
    2024-09-09
  • Dubbo+Nacos服務啟動報錯,返回unknown user的問題

    Dubbo+Nacos服務啟動報錯,返回unknown user的問題

    這篇文章主要介紹了Dubbo+Nacos服務啟動報錯,返回unknown user的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 詳解json在SpringBoot中的格式轉換

    詳解json在SpringBoot中的格式轉換

    這篇文章主要介紹了詳解json在SpringBoot中的格式轉換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 基于maven的ssm框架整合的示例代碼

    基于maven的ssm框架整合的示例代碼

    本篇文章主要介紹了基于maven的ssm框架整合的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • SpringMVC生成的驗證碼圖片不顯示問題及解決方法

    SpringMVC生成的驗證碼圖片不顯示問題及解決方法

    這篇文章主要介紹了SpringMVC生成的驗證碼圖片不顯示問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • java實現文件導入導出

    java實現文件導入導出

    這篇文章主要介紹了java實現文件導入導出的方法和具體示例代碼,非常的簡單實用,有需要的小伙伴可以參考下
    2016-04-04
  • 一文詳解Java中的反射與new創(chuàng)建對象

    一文詳解Java中的反射與new創(chuàng)建對象

    Java中的反射(Reflection)和使用new關鍵字創(chuàng)建對象是兩種不同的對象創(chuàng)建方式,各有優(yōu)缺點和適用場景,本文小編給大家詳細介紹了Java中的反射與new創(chuàng)建對象,感興趣的小伙伴跟著小編一起來看看吧
    2024-07-07
  • 關于FastJson?long?溢出問題的小結

    關于FastJson?long?溢出問題的小結

    這篇文章主要介紹了關于FastJson?long?溢出問題的小結,具有很好的參考價值,希望對大家有所幫助。
    2022-01-01
  • Java使用C3P0數據源鏈接數據庫

    Java使用C3P0數據源鏈接數據庫

    這篇文章主要為大家詳細介紹了Java使用C3P0數據源鏈接數據庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08

最新評論

丹阳市| 盐池县| 锡林浩特市| 禄劝| 上林县| 绥滨县| 康平县| 疏勒县| 抚宁县| 融水| 凉城县| 报价| 那曲县| 屏东县| 房山区| 闻喜县| 江达县| 庆城县| 册亨县| 台江县| 广德县| 嫩江县| 博野县| 梧州市| 安新县| 南漳县| 教育| 苍山县| 酒泉市| 青田县| 大厂| 临湘市| 射阳县| 钟山县| 满洲里市| 六盘水市| 新安县| 雷波县| 晋中市| 吉安县| 高陵县|