【问题标题】:Spring amqp is not raising timeout exception when the connection is blocked by RabbitMQ当连接被 RabbitMQ 阻止时,Spring amqp 不会引发超时异常
【发布时间】:2017-11-15 14:10:18
【问题描述】:

当 RabbitMQ 磁盘使用量或内存使用量达到更高的阈值时,RabbitMQ 会阻塞连接

在 spring amqp 中,只要 rabbitMQ 阻塞连接,就没有关于连接失败的错误消息

有没有什么方法可以为 spring amqp 中的发布设置超时?

提前致谢

【问题讨论】:

    标签: spring-boot rabbitmq amqp


    【解决方案1】:

    Spring amqp默认不开启阻塞连接监听,我们需要在rabbitmq连接工厂bean中添加阻塞连接监听来获取阻塞连接通知。

    以下代码将起作用:

    connectionFactory.addConnectionListener(new ConnectionListener() {
    
        @Override
        public void onCreate(Connection connection) {
            Channel channel = connection.createChannel(false);
            channel.getConnection().addBlockedListener(new BlockedListener() {
                @Override
                public void handleUnblocked() throws IOException {
    
                }
    
                @Override
                public void handleBlocked(String reason) throws IOException {
    
                }
            });
    
            try {
                channel.close();
            }
            catch (IOException e) {
    
            }
        }
    
        @Override
        public void onClose(Connection connection) {
    
        }
    
    });
    

    参考

    1. https://www.rabbitmq.com/connection-blocked.html
    2. Spring AMQP: Register BlockedListener to Connection

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-11
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多