Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue in establishing connection with Rabbit MQ

Tags:

java

rabbitmq

code

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;


public class Send {
private final static String QUEUE_NAME = "test";

public static void main(String[] argv) throws java.io.IOException {
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        Connection connection = factory.newConnection();
        System.out.println(connection.getPort());
        System.out.println(connection.getAddress());

        Channel channel = connection.createChannel();
        System.out.println("opening channel");
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    } catch (Exception ex) {
    ex.printStackTrace();

    }

}
}

I am getting the below exception:-

1. java.io.IOException  at
    com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106)   at
    com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102)   at
    com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124)
        at
    com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:844)
        at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:61)
        at com.in.test.Send.main(Send.java:24) Caused by:
    com.rabbitmq.client.ShutdownSignalException: channel error; protocol
    method: #method<channel.close>(reply-code=406,
    reply-text=PRECONDITION_FAILED - inequivalent arg 'durable' for
    queue 'test' in vhost '/': received 'false' but current is 'true',
    class-id=50, method-id=10)  at
    com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67)
        at
    com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33)
        at
    com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:361)
        at
    com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:226)
        at
    com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118)
        ... 3 more Caused by: com.rabbitmq.client.ShutdownSignalException:
    channel error; protocol method:
    #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'durable' for
    queue 'test' in vhost '/': received 'false' but current is 'true',
    class-id=50, method-id=10)  at
    com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:484)
        at
    com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:321)
        at
    com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)
        at
    com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91)
        at
    com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:554)
        at java.lang.Thread.run(Thread.java:745)
like image 569
sougata das Avatar asked Aug 01 '15 13:08

sougata das


People also ask

How do I establish a connection with RabbitMQ?

In order for a client to interact with RabbitMQ it must first open a connection. This process involves a number of steps: Application configures the client library it uses to use a certain connection endpoint (e.g. hostname and port) The library resolves the hostname to one or more IP addresses.

Can not connect to RabbitMQ?

Make sure the node is running using rabbitmq-diagnostics status. Verify config file is correctly placed and has correct syntax/structure. Inspect listeners using rabbitmq-diagnostics listeners or the listeners section in rabbitmq-diagnostics status. Inspect effective configuration using rabbitmq-diagnostics environment.

Why is RabbitMQ connection closed?

A common cause for connections being abruptly closed as soon as they're started is a TCP load balancer's heartbeat. If this is the case you should see these messages at very regular intervals, and the generally accepted practice seems to be to ignore them.

What ports need to be open for RabbitMQ?

To connect to RabbitMQ from a different machine, you must open ports 5672 and 5672 for remote access.


2 Answers

This is happening since your pre-existing channel on your RabbitMQ server, named test, was created with durable set true:

channel.queueDeclare(QUEUE_NAME, true, false, false, null);
                                 ----

You've since changed your code like so:

channel.queueDeclare(QUEUE_NAME, false, false, false, null);
                                 -----

You need to remove the channel from your server (rabbitmqctl), or create a new channel (unique name).

I'd say your answer solved your problem since you renamed your queue, but you didn't reflect this in your answer.

like image 53
wulfgarpro Avatar answered Sep 18 '22 11:09

wulfgarpro


Just change the line channel.queueDeclare(QUEUE_NAME, false, false, false, null); to channel.queueDeclare(QUEUE_NAME, true, false, false, null);

This worked for me.

like image 34
sougata das Avatar answered Sep 16 '22 11:09

sougata das