为什么我的RabbitMQ频道会继续关闭?

我正在调试一些使用Apache POI从Microsoft Office文档中提取数据的Java代码。 有时,当内存不足时,它会遇到大文档和POI崩溃。 此时,它会尝试将错误发布到RabbitMQ,以便其他组件可以知道此步骤失败并采取适当的操作。 但是,当它尝试发布到队列时,它会获得com.rabbitmq.client.AlreadyClosedException (clean connection shutdown; reason: Attempt to use closed channel)

这是error handling程序代码:

 try { //Extraction and indexing code } catch(Throwable t) { // Something went wrong! We'll publish the error and then move on with // our lives System.out.println("Error received when indexing message: "); t.printStackTrace(); System.out.println(); String error = PrintExc.format(t); message.put("error", error); if(mime == null) { mime = "application/vnd.unknown"; } message.put("mime", mime); publish("IndexFailure", "", MessageProperties.PERSISTENT_BASIC, message); } 

为了完整性,这是发布方法:

 private void publish(String exch, String route, AMQP.BasicProperties props, Map message) throws Exception{ chan.basicPublish(exch, route, props, JSONValue.toJSONString(message).getBytes()); } 

我在try块中找不到任何似乎关闭RabbitMQ通道的代码。 是否存在隐含关闭渠道的情况?

编辑 :我应该注意AlreadyClosedException是由publish中的basicPublish调用引发的。

AMQP通道因通道错误而关闭。 两个可能导致通道错误的常见问题:

  • 尝试将消息发布到不存在的交换
  • 尝试发布具有立即标志集的消息,该消息没有具有活动使用者集的队列

我将研究在您尝试使用addShutdownListener()发布消息的通道上设置ShutdownListener以捕获关闭事件并查看导致它的原因。

在我的情况下另一个原因是我错误地确认了两次消息。 这会在第二次确认后导致日志中出现RabbitMQ错误。

 =ERROR REPORT==== 11-Dec-2012::09:48:29 === connection <0.6792.0>, channel 1 - error: {amqp_error,precondition_failed,"unknown delivery tag 1",'basic.ack'} 

删除重复的确认后,错误消失,通道不再关闭,AlreadyClosedException也消失了。

我想为将要搜索此主题的其他用户添加此信息

接收频道封闭exception的另一个可能原因是发布者和消费者使用不同的队列声明/设置访问频道/队列

出版者

 channel.queueDeclare("task_queue", durable, false, false, null); 

工人

 channel.queueDeclare("task_queue", false, false, false, null); 

来自RabbitMQ网站

 RabbitMQ doesn't allow you to redefine an existing queue with different parameters and will return an error to any program that tries to do that 

我也有这个问题。 我的理由是,首先我使用durable = false构建队列,并且在我将持久性切换为true时,在日志文件中出现此错误消息:

“不同等arg’持久’用于队列’logsQueue’在vhost’/’中:收到’true’但当前是’false’”

然后,我改变了队列的名称,它对我有用。 我假设RabbitMQ服务器将构建队列的记录保存在某处,并且它不能将状态从持久状态更改为非持久状态,反之亦然。

我再一次为新队列做了持久=假,这次我得到了这个错误

“vhost’/’中队列’logsQueue1’的不等效arg’持久’:收到’false’但当前为’true’”

我的假设是真的。 当我在rabbitMQ服务器中列出队列时:

 rabbitmqctl list_queues 

我在服务器中看到了两个队列。

总而言之,2个解决方案是:1。重命名队列的名称,这不是一个好的解决方案2.通过以下方式重置rabbitMQ:

 rabbitmqctl stop_app rabbitmqctl reset rabbitmqctl start_app