Spring AMQP: Register BlockedListener to Connectio

2020-03-30 01:58发布

问题:

I am trying to implement Blocked Listeners to RabbitMQ using RabbitTemplate of Spring AMQP. In my code i am using Spring-amqp 1.1.3 version jar file, whereas i have looked into the version 1.3.1 as well and this is unsupported in this version also. Does anyone know if i am missing any version which has support of registering Blocked listeners to the new connections in RabbitMQ. Or if is there any future release of spring amqp to support this feature.

Example Code:

    Connection connection = factory.newConnection();
    connection.addBlockedListener(new BlockedListener() {
     @Override
     public void handleUnblocked() throws IOException {
        System.out.println("Connection is Unblocked");
     }

     @Override
     public void handleBlocked(String arg0) throws IOException {
        System.out.println("Connection Blocked");
     }           



    });
    com.rabbitmq.client.Channel channel = connection.createChannel();    

回答1:

This is not currently available out of the box; please feel free to open an Improvement JIRA Issue.

However, you can add a Spring AMQP ConnectionListener to the CachingConnectionFactory...

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

    }

});

It will be called even if the connection has already been established when you add the listener.