Home > database >  RabbitMQ setPort causes ConnectionRefused
RabbitMQ setPort causes ConnectionRefused

Time:09-25

I have a simple RabbitMQ example starts as follows :

    private Channel channel;
    private ConnectionFactory connectionFactory;
    
    @Autowired
    public RabbitMqManager() {
        connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        //connectionFactory.setPort(15672); // ERROR : this breaks rabbitmq connection
        try {
            Connection connection = connectionFactory.newConnection();
            channel = connection.createChannel();

If I add setPort then it sometimes causes TimeoutException, sometimes ConnectionRefused. I spent half a day to understand what was happening. Then I've commented out the setPort, everything works.

Note : I can see WebUI (http://localhost:15672) without any problem and server is up & running.

Why setPort breaks the connection initialization? Does RabbitMQ search all the ports to check available server? or does it use the default port 15672 ?

CodePudding user response:

The default port is 5672, not 15672. This is why it works when you comment the line, it's setting the correct port as it's taking the default one. You can check this by calling setPort(5672) (although this is not necessary to set the port, but it will show that the issue is the port number and not the fact you're calling this function).

  • Related