Spring AMQP
Rabbit MQ
Connection Issues
AmqpConnectException
java.net.ConnectException

Unable to connect Spring AMQP / Rabbit MQ org.springframework.amqp.AmqpConnectException java.net.ConnectException Connection refused connect

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

org.springframework.amqp.AmqpConnectException wrapped around java.net.ConnectException: Connection refused means the application could not open a TCP connection to RabbitMQ at the configured host and port. In other words, the failure usually happens before authentication, queue declaration, or message publishing logic even begins.

That makes the debugging order straightforward: verify the broker is up, verify the address is correct, verify the port is reachable, and only then look at higher-level Spring configuration.

Start with the Broker Itself

Make sure RabbitMQ is actually running.

bash
rabbitmqctl status

If RabbitMQ is running in Docker:

bash
docker ps
docker logs rabbitmq

If the broker is not listening on the expected port, Spring cannot possibly connect.

The default AMQP port is usually 5672.

Verify the Spring Connection Settings

Typical Spring Boot configuration looks like this:

properties
1spring.rabbitmq.host=localhost
2spring.rabbitmq.port=5672
3spring.rabbitmq.username=guest
4spring.rabbitmq.password=guest

The most common configuration mistakes are:

  • wrong host name
  • wrong port
  • assuming localhost from inside a container means the host machine
  • using guest remotely where the broker policy only allows it locally

That last case is especially common. RabbitMQ often restricts the guest user to loopback connections by default.

Test Raw TCP Reachability

Before digging deeper into Spring, test the port directly.

bash
nc -vz localhost 5672

If that fails with connection refused, the issue is not Spring AMQP. It is network reachability or broker availability.

In containerized setups, also check whether the port is published correctly and whether the application should use a container service name rather than localhost.

Common Docker and Compose Mistake

If your Spring app runs in Docker Compose and RabbitMQ runs in another Compose service, localhost is wrong.

Inside the app container, localhost refers to the app container itself, not the RabbitMQ container.

In that case, use the RabbitMQ service name instead:

properties
spring.rabbitmq.host=rabbitmq
spring.rabbitmq.port=5672

This single mistake causes a large share of connection-refused errors in development environments.

Distinguish Refused vs. Auth Failure

A refused connection means the TCP socket could not be opened. That is different from:

  • authentication failure
  • virtual host permission failure
  • channel or exchange errors after connect

This distinction is useful because it narrows the search dramatically. If the error is connection refused, focus on process state, host, port, networking, and port publishing first.

A Minimal Spring Configuration Example

java
1import org.springframework.amqp.rabbit.core.RabbitTemplate;
2import org.springframework.stereotype.Service;
3
4@Service
5public class Sender {
6    private final RabbitTemplate rabbitTemplate;
7
8    public Sender(RabbitTemplate rabbitTemplate) {
9        this.rabbitTemplate = rabbitTemplate;
10    }
11
12    public void send() {
13        rabbitTemplate.convertAndSend("demo.queue", "hello");
14    }
15}

If this fails with connection refused, the message send code is not the interesting part yet. The infrastructure connection must work first.

Common Pitfalls

The biggest mistake is troubleshooting exchanges, queues, and listeners before confirming the broker port is reachable at all.

Another common issue is using localhost from inside a container or VM and expecting it to refer to RabbitMQ running somewhere else.

People also forget the guest remote access restriction and spend time debugging passwords when the real problem is policy.

Finally, do not rely only on application stack traces. Check RabbitMQ status, container logs, and a raw TCP connection test.

Summary

  • 'Connection refused means the TCP connection itself failed.'
  • Check that RabbitMQ is running and listening on the expected port.
  • Verify spring.rabbitmq.host and spring.rabbitmq.port carefully.
  • In container environments, avoid assuming localhost points to the broker.
  • Use a raw network test such as nc to separate infrastructure problems from Spring problems.
  • Investigate authentication only after basic connectivity is working.

Course illustration
Course illustration

All Rights Reserved.