21

I am new to Spring AMQP / Rabbit MQ.

Am using a Spring AMQP / Rabbit MQ in my project. I am facing following error after running a tomcat:

org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer - Consumer raised exception, processing can restart if the connection factory supports it.

Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

Below is the configuration file :

spring-amqp.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit        http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <rabbit:connection-factory id="connectionFactory" host="127.0.0.1"/>
    <rabbit:admin connection-factory="connectionFactory"/>

    <rabbit:template connection-factory="connectionFactory" id="rabbitTemplate" channel-transacted="true"/>
    <rabbit:queue name="proposalQueue" />

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="listener" queue-names="proposalQueue"/>
    </rabbit:listener-container>

    <bean id="rabbitMQTransactionManager" class="org.springframework.amqp.rabbit.transaction.RabbitTransactionManager">
        <property name="connectionFactory" ref="connectionFactory"/>
    </bean>

    <rabbit:direct-exchange name="myExchange">
        <rabbit:bindings>
             <rabbit:binding queue="proposalQueue" key="userMesssage" />
        </rabbit:bindings>
    </rabbit:direct-exchange>
    <bean id="listener" class="com.xxx.xxxx.rabbitmq.QueueServer"/>
 </beans>

QueueServer.java

@Override
    public void onMessage(Message message) {

    Map<String, Object> result = new HashMap<>();       
    MessageProperties props = message.getMessageProperties();
    BasicProperties replyProps = new BasicProperties.Builder().correlationId(new String(message.getMessageProperties().getCorrelationId())).build();
    String inputParameterStr = new String(message.getBody());

        try {
            Map<String,Object> inputParameters  = (Map<String, Object>) Utility.StringToObject(inputParameterStr, "java.util.Map");
            result = service.createQueue(inputParameters);

        } catch (ClassNotFoundException e) {
            logger.error("Error :::: "+getClass()+proposalID, e);
            result.put(Constants.FAILURE, e.getMessage());
        } catch (Exception e) {
            logger.error("Error :::: "+getClass()+proposalID, e);
            result.put(Constants.FAILURE, e.getMessage());
        }
    }

Please help to resolve.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
AmolKumar
  • 223
  • 1
  • 2
  • 5

5 Answers5

19

java.net.ConnectException: Connection refused: connect

That simply means that RabbitMQ is not running on localhost (127.0.0.1) on the standard port (5672).

Did you download and install/run RabbitMQ? It is not like ActiveMQ - it can't run embedded in a java application.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • 2
    Yes. RabbitMQ (3.5.6) and ERLANG (otp_win32_18.1.exe) is installed on c:. Status of RabbitMQ service is start. – AmolKumar Nov 26 '15 at 04:02
  • 3
    Well, it's clearly not listening on port 5672 (the default). Try `netstat -ano | find "5672"` in a `cmd` window; if it's actually listening, you should see `127.0.0.1:5672 ... LISTENING`. Look at the rabbitmq logs to see if there is information there. – Gary Russell Nov 26 '15 at 06:19
  • thanks..I checked it on cmd but there in no any listening port as you said : **127.0.0.1:5672** and please tell me the path where log is generated. Rabbit mq is running on 25672 (Check in resource monitor.) – AmolKumar Nov 26 '15 at 09:15
  • I am not familiar with rabbit on Windows; sorry; but it looks like there's a problem with your installation; according to [this link](https://www.rabbitmq.com/configure.html), 25672 is used for clustering so it is strange that it is listening but not 5672; you need to find where the logs are and look at them. If all else fails; there's a google group rabbitmq-users for questions about rabbitmq itself (don't ask questions about spring-amqp there, though; this is the right place for that.) – Gary Russell Nov 26 '15 at 14:58
  • Thanks Gary.. I re-install the Rabbitmq using link which you give. still it use 25672 port and error is same. On another machine same procedure is followed for installation and all is done well.. – AmolKumar Nov 27 '15 at 04:02
8

Check the host and port value

In application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

enter image description here

See RabbitMQ site is running on port 15672 whereas in code using amqp protocol.

Atul Jain
  • 1,035
  • 2
  • 16
  • 24
2

There's one more aspect to the problem.

By default, the RabbitMQ is accessible to the local machine only. If you want to access it from some other machine, you generally create one entry in "rabbitmq.config" file. Location of this file varies from OS to OS. In Linux, you can find this at: "/etc/rabbitmq/rabbitmq.config" and in Windows machine, you can find it at: "C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.9\etc\rabbitmq.config".

There's a possibility that you don't find this file at the mentioned location. This is an optional file and you need not to worry if it is missing even. You can create your own. This entry is something like:

[{rabbit, [{tcp_listeners, [{"<IP_OF_MACHINE>", 5672}]},{loopback_users, []}]}].

With this, you can access the server from any remote machine.

If after this you get the server inaccessible you can modify the entry to:

[{rabbit, [{tcp_listeners, [{"0.0.0.0", 5672}]},{loopback_users, []}]}].

You will definitely get the server connected to any client as well as through management console (if plugin enabled)

Kos
  • 4,890
  • 9
  • 38
  • 42
Ajay Sodhi
  • 111
  • 1
  • 2
1

i'm confronted the same problem (when using docker). Above answers didn't help me. I switched to a different version rabbitmq.

was:

rabbitmq:3.9.14-management-alpine

now:

rabbitmq:3.9.8-management-alpine
0

In order to access the RabbitMQ remote , you need to allow inbound TCP traffic on ports 4369, 25672, 5671, 5672, 15672, 61613, 61614, 1883, and 8883.

sudo firewall-cmd --zone=public --permanent --add-port=4369/tcp --add-port=25672/tcp --add-port=5671-5672/tcp --add-port=15672/tcp  --add-port=61613-61614/tcp --add-port=1883/tcp --add-port=8883/tcp
sudo firewall-cmd --reload
Green Lei
  • 3,150
  • 2
  • 20
  • 25