【问题标题】:Issue in establishing connection with Rabbit MQ与 Rabbitmq 建立连接的问题
【发布时间】:2015-08-01 13:49:08
【问题描述】:

代码

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();

    }

}
}

我收到以下异常:-

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)

【问题讨论】:

    标签: java rabbitmq


    【解决方案1】:

    发生这种情况是因为您在 RabbitMQ 服务器上名为 test 的预先存在的通道是使用持久集 true 创建的:

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

    您已经像这样更改了代码:

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

    您需要从服务器中删除频道 (rabbitmqctl),或创建一个新频道(唯一名称)。

    我会说你的答案解决了你的问题,因为你重命名了你的队列,但你没有在你的答案中反映这一点。

    【讨论】:

    • 是的,确实如此,但这里恰恰相反。现有队列设置为durable = true。但后来用户尝试将其创建为耐用=假。
    【解决方案2】:

    只需换行channel.queueDeclare(QUEUE_NAME, false, false, false, null); 给channel.queueDeclare(QUEUE_NAME, true, false, false, null);

    这对我有用。

    【讨论】:

    • 是的,队列声明参数不能在调用之间更改。
    • 这里的错误信息解释了这个问题:协议方法:#method(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'test' in vhost '/': 收到'false',但当前是'true'
    【解决方案3】:

    这样做一次:
    1.在 channel.queueDeclare
    中使用耐用消费品=true 运行您的应用 让它连接队列
    2.关闭它。
    3.启动生产者

    现在你不会得到这个异常了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多