Issue
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: java.net.ConnectException: errno: 111 (Connection refused), error: Connection refused (local port 45596 to address 127.0.0.1 (localhost), remote port 5672 to address 127.0.0.1 (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-
@Bean
public MessageConverter jsonMessageConverter(){
return new Jackson2JsonMessageConverter();
}
@Bean
public AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
@Bean
public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory rabbitConnectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(rabbitConnectionFactory);
container.setQueueNames("queue2911");
container.setAutoStartup(false);
//container.setMessageListener(exampleListener());
return container;
}
Any help would be appreciated.
Solution
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
Answer Checked By - Timothy Miller (JavaFixing Admin)