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

AmolKumar picture AmolKumar · Nov 25, 2015 · Viewed 37.9k times · Source

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.

Answer

Gary Russell picture Gary Russell · Nov 25, 2015

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.