Connection refused trying to read from RabbitMQ Queue on Cloud Foundry using Spring


I am trying to use RabbitMQ for posting messages from one application and receiving them in another. These applications are hosted on Cloud Foundry and both have been bound to the same instance of RabbitMQ.

I am able to post messages to the queue from the first application, but in my second application, which uses @RabbitListener and @RabbitHandler to listen to this queue, I am getting a Connection Refused error. It works well on my local instance of RabbitMQ though.

Consumer raised exception, processing can restart if the connection
factory supports it. Exception summary:
org.springframework.amqp.AmqpConnectException: errno: 111 (Connection refused), error:
Connection refused (local port 45596 to address (localhost),
remote port 5672 to address (localhost))

This is the complete log.

The remote port, I observe, remains the same at 5672, but the local port keeps changing from log to log. I am not sure where these ports are picked up from, as I assume Spring should be handling this for me, and also because I have a similar setup for my first application, which seems to be working fine.

Here is how my config looks-

        public MessageConverter jsonMessageConverter(){
            return new Jackson2JsonMessageConverter();

        public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
            return new RabbitAdmin(connectionFactory);

        public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
            RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
            return rabbitTemplate;

        public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory rabbitConnectionFactory) {
            SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
            return container;

Any help would be appreciated.


The operating system picks the local port.

The fact that you are attempting to connect to localhost implies you are using a connection factory with defaults (localhost:5672) instead of the boot configured one.

Turn on DEBUG logging for both apps and compare the auto configuration reports.

Answered By – Gary Russell

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published