title: RabbitMQ+SpringAMPQ: Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: connect
body: <p>I am currently learning RabbitMQ and Spring AMPQ.
  I was trying some examples this week but I am facing an issue in some step of the configuration.</p>
  
  <p>I have this docker-compose file:</p>
  
  <pre><code>app:
    build: .
    environment:
      INSERTION_QUEUE: insertion.queue
      VALIDATION_QUEUE: validation.queue
      NUMBER_OF_VALIDATION_CONSUMERS: 1
      RESPONSE_EXCHANGE: response.exchange
      RESPONSE_ROUTING_KEY: response.routing.key
      RABBITMQ_HOST: rabbitmq
      RABBITMQ_PORT: 5672
      RABBITMQ_VHOST: /
      RABBITMQ_USERNAME: guest
      RABBITMQ_PASSWORD: guest
      JDBC_URL: jdbc:mysql://mysql:3306/hided
    links:
      - mysql:mysql
      - rabbitmq:rabbitmq
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: hided
      MYSQL_ROOT_PASSWORD: secret
  rabbitmq:
    image: rabbitmq:3.6-management
    ports:
      - 15672:15672
  </code></pre>
  
  <p>And I copied this example, adding maven configuration to it: <a href="https://github.com/spring-guides/gs-messaging-rabbitmq/tree/master/complete" rel="nofollow noreferrer">https://github.com/spring-guides/gs-messaging-rabbitmq/tree/master/complete</a></p>
  
  <p>The RabbitMQ is accessed through the URL localhost:15672 correctly with username and password "guest"</p>
  
  <p>But when I run this example, it gives me this error (which I believe the important message is <strong>Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: connect</strong>):</p>
  
  <pre><code>2020-04-11 04:50:49.202  INFO 25576 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
  2020-04-11 04:50:51.222  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Broker not available; cannot force queue declarations during start: java.net.ConnectException: Connection refused: connect
  2020-04-11 04:50:51.225  INFO 25576 --- [    container-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
  2020-04-11 04:50:53.242 ERROR 25576 --- [    container-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
  
  org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
      at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:510) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2095) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2068) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2048) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueInfo(RabbitAdmin.java:407) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:391) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.attemptDeclarations(AbstractMessageListenerContainer.java:1830) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.redeclareElementsIfNecessary(AbstractMessageListenerContainer.java:1811) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.initialize(SimpleMessageListenerContainer.java:1342) [spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1188) [spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at java.lang.Thread.run(Unknown Source) [na:1.8.0_241]
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_241]
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_241]
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_241]
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_241]
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
      at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
      at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
      at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_241]
      at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.7.3.jar:5.7.3]
      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1113) ~[amqp-client-5.7.3.jar:5.7.3]
      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1063) ~[amqp-client-5.7.3.jar:5.7.3]
      at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:526) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:473) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      ... 12 common frames omitted
  
  2020-04-11 04:50:53.244  INFO 25576 --- [    container-1] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
  2020-04-11 04:50:55.266  INFO 25576 --- [           main] c.a.A.AppNameApplication           : Started AppNameApplication in 7.328 seconds (JVM running for 8.175)
  Sending message...
  2020-04-11 04:50:55.268  INFO 25576 --- [           main] o.s.a.r.c.CachingConnectionFactory       : Attempting to connect to: [localhost:5672]
  2020-04-11 04:50:57.285  INFO 25576 --- [           main] ConditionEvaluationReportLoggingListener : 
  
  Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
  2020-04-11 04:50:57.293 ERROR 25576 --- [           main] o.s.boot.SpringApplication               : Application run failed
  
  java.lang.IllegalStateException: Failed to execute CommandLineRunner
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
      at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
      at com.appname.AppName.AppNameApplication.main(AppNameApplication.java:19) [classes/:na]
  Caused by: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect
      at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:61) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:510) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:751) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.createConnection(ConnectionFactoryUtils.java:214) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:2095) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:2068) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:1009) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1075) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend(RabbitTemplate.java:1068) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at com.appname.AppName.Runner.run(Runner.java:23) ~[classes/:na]
      at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
      ... 5 common frames omitted
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_241]
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_241]
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_241]
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_241]
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
      at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
      at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_241]
      at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_241]
      at com.rabbitmq.client.impl.SocketFrameHandlerFactory.create(SocketFrameHandlerFactory.java:60) ~[amqp-client-5.7.3.jar:5.7.3]
      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1113) ~[amqp-client-5.7.3.jar:5.7.3]
      at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:1063) ~[amqp-client-5.7.3.jar:5.7.3]
      at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.connect(AbstractConnectionFactory.java:526) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:473) ~[spring-rabbit-2.2.5.RELEASE.jar:2.2.5.RELEASE]
      ... 14 common frames omitted
  
  2020-04-11 04:50:57.296  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
  2020-04-11 04:50:57.296  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.
  2020-04-11 04:50:57.298  INFO 25576 --- [           main] o.s.a.r.l.SimpleMessageListenerContainer : Shutdown ignored - container is not active already
  </code></pre>
  
  <p>I would like to understand what I am doing wrong, I tried to add the code below to "MessagingRabbitmqApplication.java" to force the connection, made some changes in ports, address, disabled firewall, no success:</p>
  
  <pre><code>@Bean
      public ConnectionFactory connectionFactory() {
          CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
          connectionFactory.setHost("localhost");
          connectionFactory.setVirtualHost("/");
          connectionFactory.setUsername("guest");
          connectionFactory.setPassword("guest");
          return connectionFactory;
      }
  </code></pre>
  
  <p>I did try other examples but I keep facing this same error.</p>
  
  <p>My Dockerfile is:</p>
  
  <pre><code># Start with a base image containing Java runtime
  FROM openjdk:8-jdk-alpine
  
  # Add Maintainer Info
  LABEL maintainer="hidden"
  
  # Add a volume pointing to /tmp
  VOLUME /tmp
  
  ARG JAR_FILE=target/*.jar
  COPY ${JAR_FILE} app.jar
  ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
  </code></pre>
  
  <p>And my application.properties is:</p>
  
  <pre><code>#Database properties
  spring.datasource.url = jdbc:mysql://mysql:3306/app_name?user=root&amp;password=secret
  spring.datasource.username = root
  spring.datasource.password = secret
  spring.datasource.platform = mysql
  spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
  
  #Rabbitmq properties
  spring.rabbitmq.host=localhost
  spring.rabbitmq.port=5672
  spring.rabbitmq.username=guest
  spring.rabbitmq.password=guest
  spring.rabbitmq.template.exchange=response.exchange
  spring.rabbitmq.template.routing-key=response.routing.key
  spring.rabbitmq.virtual-host=/
  </code></pre>
  
  <p>Thank you for your time,
  I appreciate any help.</p>