【问题标题】:Rabbit mq error: Getting Exception in thread "main" java.io.IOException Caused by: com.rabbitmq.client.ShutdownSignalExceptionRabbit mq 错误:在线程“main”java.io.IOException 中获取异常原因:com.rabbitmq.client.ShutdownSignalException
【发布时间】:2023-04-04 10:44:01
【问题描述】:

这是我在运行 main 时遇到的错误。我真的不明白为什么第 44 行有问题:channel.basicConsume(Q,true,consumer); 我的目标是尝试将收到的消息存储到我可以在其他文件中使用的变量中。

The error is: Exception in thread "main" java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:105)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:101)
at com.rabbitmq.client.impl.ChannelN.basicConsume(ChannelN.java:1255)
at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.basicConsume(AutorecoveringChannel.java:471)
at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.basicConsume(AutorecoveringChannel.java:461)
at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.basicConsume(AutorecoveringChannel.java:456)
at Recv.recv(Recv.java:44)
at mainLaptop.main(mainLaptop.java:11)
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'Leonardo' in vhost '/', class-id=60, method-id=20)
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:32)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:366)
at com.rabbitmq.client.impl.ChannelN.basicConsume(ChannelN.java:1253)
... 5 more

这是我的 Recv 文件代码

    public class Recv 
    {

public static String recv(String ip, String Q) throws Exception 
{

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(ip);
    factory.setUsername("test");
    factory.setPassword("test");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    MyConsumer consumer=new MyConsumer(channel);
    channel.basicConsume(Q,true,consumer);

    return consumer.getStoredMessage();
}

public static class MyConsumer extends DefaultConsumer 
{
    private String storedMessage;

    public MyConsumer(Channel channel) 
    {
        super(channel);
    }

    public String getStoredMessage() 
    {
        return storedMessage;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
        throws IOException 
    {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
        storedMessage = message; // store message here
    }
}
}

【问题讨论】:

标签: java rabbitmq message-queue rmq que


【解决方案1】:

许多异常都包含有用的信息,可以告诉您什么是错误的,以及您可以采取哪些措施来解决这些问题。

在这种情况下,您的问题是 我真的不明白为什么第 44 行有问题:channel.basicConsume(Q,true,consumer);

即使堆栈跟踪很丑陋,您也需要阅读它,因为异常包含以下文本:

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method&lt;channel.close&gt;(reply-code=404, reply-text=NOT_FOUND - no queue 'Leonardo' in vhost '/', class-id=60, method-id=20)

错误很明显。 vshost '/' 中没有队列 'Leonardo'。您尚未在 RabbitMQ 服务器中声明队列,该队列作为字符串 Q 传入。因此,尝试从不存在的队列中消费是一个例外。先声明队列,问题就迎刃而解了。

【讨论】:

  • 感谢您澄清这一点。我实际上知道“vshost '/ 中没有队列'Leonardo'”但是我认为 Java 是同时执行的。我在我的发送文件/类中声明了 que,但我在我的主文件/类之后调用它。一旦我切换了它的工作顺序!
猜你喜欢
  • 2017-03-01
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
相关资源
最近更新 更多