Spring RabbitMQ tutorial results in Connection Refused error

14,565

Solution 1

You do not have RabbitMQ server installed. Luckily this is pretty easy to do. You mentioned you are using CentOS, here you can download RabbitMQ server: https://www.rabbitmq.com/install-rpm.html

There are a couple of steps you will need to follow, including installing Erlang.

The Spring example assumes you already know a bit about using a message broker. This is in itself a technology you will need to use, like when you had to learn to use a database or an operating system.

Solution 2

This is an old question but I'll post my answer anyway as it addresses an issue with this particular step of the Spring tutorial that the other answer doesn't.

If you are using the docker-compose.yml file specified in the Spring tutorial to launch a RabbitMQ server in a Docker container you might run into the same issue I had connecting to it, which I outlined in this question;

Docker image for Spring/RabbitMQ tutorial results in connection refused

After coming back to it a couple of days later though I managed to find the solution;

https://stackoverflow.com/a/44882772/8215811

The Spring tutorial unfortunately does not clarify that you need to configure connection information when choosing using the docker-compose file over the manual installation of RabbitMQ, which is unfortunate as the Docker method seems easier and more straightforward.

Share:
14,565
stackoverflowuser2010
Author by

stackoverflowuser2010

Updated on June 04, 2022

Comments

  • stackoverflowuser2010
    stackoverflowuser2010 almost 2 years

    I'm an experienced Java programmer and am trying out Spring Rabbit MQ for the first time.

    I followed the messaging-rabbitMQ tutorial exactly using Maven. http://spring.io/guides/gs/messaging-rabbitmq/

    I am running on CentOS as a user account.

    When I ran the application at the very end of the tutorial with java -jar target/gs-messaging-rabbitmq-0.1.0.jar, I got the following Connection Refused error. Can someone help?

    prompt> java -jar target/gs-messaging-rabbitmq-0.1.0.jar
    
      .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.2.3.RELEASE)
    
    2015-05-13 11:49:44.833  INFO 26218 --- [           main] hello.Application                        : Starting Application v0.1.0 on minerva-02-33.gbcl.net with PID 26218 (/home/ccpm/test/spring_rabbitmq/target/gs-messaging-rabbitmq-0.1.0.jar started by ccpm in /home/ccpm/test/spring_rabbitmq)
    2015-05-13 11:49:44.912  INFO 26218 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@8146158: startup date [Wed May 13 11:49:44 PDT 2015]; root of context hierarchy
    2015-05-13 11:49:45.863  INFO 26218 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [class org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$9a886eed] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2015-05-13 11:49:46.397  INFO 26218 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2015-05-13 11:49:46.404  INFO 26218 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
    2015-05-13 11:49:46.433 ERROR 26218 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
    
    org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
        at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:54)
        at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:207)
        at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:441)
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils$1.createConnection(ConnectionFactoryUtils.java:80)
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:130)
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:67)
        at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1035)
        at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1028)
        at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1004)
        at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:254)
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.redeclareElementsIfNecessary(SimpleMessageListenerContainer.java:947)
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$300(SimpleMessageListenerContainer.java:82)
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1065)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:615)
        at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:199)
        ... 12 common frames omitted
    
    Waiting five seconds...
    2015-05-13 11:49:51.437  WARN 26218 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
    2015-05-13 11:49:51.437  INFO 26218 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0
    2015-05-13 11:49:51.441 ERROR 26218 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Failed to check/redeclare auto-delete queue(s).
    
    org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused
        at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:54)
        at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:207)
        at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:441)
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils$1.createConnection(ConnectionFactoryUtils.java:80)
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:130)
        at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:67)
        at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1035)
        at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1028)
        at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1004)
        at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:254)
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.redeclareElementsIfNecessary(SimpleMessageListenerContainer.java:947)
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$300(SimpleMessageListenerContainer.java:82)
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1065)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:579)
        at com.rabbitmq.client.impl.FrameHandlerFactory.create(FrameHandlerFactory.java:32)
        at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:615)
        at org.springframework.amqp.rabbit.connection.AbstractConnectionFactory.createBareConnection(AbstractConnectionFactory.java:199)
        ... 12 common frames omitted
    
  • stackoverflowuser2010
    stackoverflowuser2010 about 9 years
    The tutorial webpage at http://spring.io/guides/gs/messaging-rabbitmq/ clearly says at the top: "This guide walks you through the process of setting up a RabbitMQ AMQP server that publishes and subscribes to messages." Why would it say that and not set up the server when I complete all the steps?
  • Leon
    Leon about 9 years
    I think it is a wording problem. The tutorial shows you how to create a publisher and a consumer for RabbitMQ
  • Dave Newton
    Dave Newton about 9 years
    @stackoverflowuser2010 Maybe because there's parts of the docs missing? It happens. I'd have been suspicious when it said that installation instructions were below and then there weren't any.
  • Gary Russell
    Gary Russell about 9 years
    Yes; you have to install rabbitmq itself. The Spring application is simply a client that uses the broker. I have opened an issue against the guide. Refer to the rabbitmq site for how to install the server.
  • stackoverflowuser2010
    stackoverflowuser2010 about 9 years
    @GaryRussell: After I install RabbitMQ, how do I "connect" the Spring library to that RabbitMQ server? I don't see anything saying "localhost" or other IP address.
  • Sajib Ghosh
    Sajib Ghosh about 9 years
  • Leon
    Leon about 9 years
    @stackoverflowuser2010 you will need to provide an ConnectionFactory bean with the properties to connect to your configured server
  • stackoverflowuser2010
    stackoverflowuser2010 about 9 years
    @Leon: Thanks. Is this ConnectionFactory bean in the source examples on that webpage? There are two Java classes, Application.java and Receiver.java, on the webpage. I don't see any mention of connection parameters.
  • Leon
    Leon about 9 years
    No it is not, the example uses a default bean provided by Spring. You can create a bean of this docs.spring.io/spring-amqp/docs/latest-ga/api/org/…
  • Gary Russell
    Gary Russell about 9 years
    Spring Boot (convention over configuration) sets the defaults to localhost:5672 github.com/spring-projects/spring-boot/blob/master/… It assumes the rabbitmq server is on localhost on the default port. See the spring boot documentation about how to configure the properties.