簡體   English   中英

與Rabbit MQ建立連接的問題

[英]Issue in establishing connection with Rabbit MQ

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 )是使用持久設置true創建的:

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

你已經改變了你的代碼:

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

您需要從服務器中刪除該通道( rabbitmqctl ),或創建一個新通道(唯一名稱)。

我說你的答案解決了你的問題,因為你重命名了你的隊列,但你沒有在你的答案中反映出來。

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

這對我有用。

做一次:
1.在channel.queueDeclare中使用consumer durable = true運行app
讓它連接隊列
關閉它。
3.啟動制片人

現在你不會得到這個例外

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM