Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 874
AmolKumar Avatar asked Nov 25 '15 11:11

AmolKumar


3 Answers

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.

like image 84
Gary Russell Avatar answered Nov 06 '22 17:11

Gary Russell


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.

like image 5
Atul Jain Avatar answered Nov 06 '22 16:11

Atul Jain


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)

like image 2
Ajay Sodhi Avatar answered Nov 06 '22 16:11

Ajay Sodhi