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

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

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

簡介

說明

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

RabbitMQ消息簡介

RabbitMQ的消息默認(rèn)不會超時。 

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

死信隊列:

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

以下幾種情況會導(dǎo)致消息變成死信:

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

延遲隊列:

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

相關(guān)網(wǎng)址

詳解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處理死信隊列和延遲隊列的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot RabbitMQ死信隊列 延遲隊列的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

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

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

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

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

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

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

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

    詳解json在SpringBoot中的格式轉(zhuǎn)換

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

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

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

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

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

    java實現(xiàn)文件導(dǎo)入導(dǎo)出

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

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

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

    關(guān)于FastJson?long?溢出問題的小結(jié)

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

    Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫

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

最新評論

陆河县| 阿拉善左旗| 安顺市| 南部县| 南漳县| 梅河口市| 西盟| 应城市| 靖远县| 呼伦贝尔市| 宣威市| 镇赉县| 垦利县| 广汉市| 平昌县| 若羌县| 介休市| 武冈市| 普洱| 教育| 光山县| 和静县| 都兰县| 康马县| 古蔺县| 香格里拉县| 额济纳旗| 阳东县| 钦州市| 永嘉县| 塔河县| 台州市| 长白| 西青区| 鹤壁市| 阿拉尔市| 炎陵县| 邓州市| 呼和浩特市| 桃园县| 聊城市|