gpt4 book ai didi

java - 与 Rabbitmq 建立连接的问题

转载 作者:搜寻专家 更新时间:2023-10-30 21:22:08 25 4
gpt4 key购买 nike

代码

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)

最佳答案

这是因为您在 RabbitMQ 服务器上预先存在的名为 test 的 channel 是使用持久集 true 创建的:

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

你已经像这样改变了你的代码:

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

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

我会说你的答案解决了你重命名队列后的问题,但你没有在你的答案中反射(reflect)这一点。

关于java - 与 Rabbitmq 建立连接的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com