Java 与Rabbit MQ建立连接的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31762563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:34:46  来源:igfitidea点击:

Issue in establishing connection with Rabbit MQ

javarabbitmq

提问by sougata das

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)

回答by sougata das

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

只需将行更改channel.queueDeclare(QUEUE_NAME, false, false, false, null);channel.queueDeclare(QUEUE_NAME, true, false, false, null);

This worked for me.

这对我有用。

回答by wulfgarpro

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

发生这种情况是因为您的 RabbitMQ 服务器上预先存在的通道,名为test,是使用持久集创建的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).

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

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

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

回答by Eliko

do that once:
1.run you app with consumer durable=true in channel.queueDeclare
let it connect the queue
2.close it.
3.start the producer

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

now you wont get this exception

现在你不会得到这个例外