Search code examples
spring-bootrabbitmqamqp

Spring amqp is not raising timeout exception when the connection is blocked by RabbitMQ


When RabbitMQ disk usage or memory usage reaches the higher threshold RabbitMQ will block connection

In the spring amqp whenever rabbitMQ blocks the connection there is no error message about the connection failure

is there is any way to set timeout for a publish in spring amqp?

Thanks in advance


Solution

  • Blocking connection listener is not enabled in Spring amqp by default, we need to add Blocked connection listener to the rabbitmq connection factory bean for getting blocked connection notification.

    Following code will works :

    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) {
    
        }
    
    });
    

    Reference

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