Java rabbitmq AlreadyClosedException
我将rabbitmq服务器与java应用程序一起使用。当应用程序收到特定队列上的消息时,它会生成一些数据并将它们发送到另一个队列上 运行应用程序时,它接收消息,生成数据并将其发送到正确的队列中。服务器上的数据接收良好,并且正确无误。但是当应用程序尝试向服务器发送确认时,我得到一个AlreadyClosedException 我在服务器的日志中有以下消息:关闭AMQP连接 以下是rabbitMQ使用者类中handleDelivery函数的代码:Java rabbitmq AlreadyClosedException,java,rabbitmq,amqp,Java,Rabbitmq,Amqp,我将rabbitmq服务器与java应用程序一起使用。当应用程序收到特定队列上的消息时,它会生成一些数据并将它们发送到另一个队列上 运行应用程序时,它接收消息,生成数据并将其发送到正确的队列中。服务器上的数据接收良好,并且正确无误。但是当应用程序尝试向服务器发送确认时,我得到一个AlreadyClosedException 我在服务器的日志中有以下消息:关闭AMQP连接 以下是rabbitMQ使用者类中handleDelivery函数的代码: public void handleDelivery
public void handleDelivery( String consumerTag, Envelope envelope, asicProperties properties, byte[] body )
throws IOException {
actionManager.receivedSelectedData( body );
getChannel().basicAck( envelope.getDeliveryTag(), false );
}
以下是receivedSelectedData()方法中的代码,其中数据在发送前生成:
public void receivedSelectedData( byte[] body ) {
differentialEquations = differentialEquationsObjectManager.fromBytes( body );
timeSeriesCollection.removeAllSeries();
for ( int i = 0; i < differentialEquations.size(); i++ ) {
differentialEquation = differentialEquations.get( i );
for ( int j = 0; j < differentialEquation.getSystems().size(); j++ ) {
try {
generator = new NumericalMethod( differentialEquation, j );
} catch ( Exception e ) {
e.printStackTrace();
}
try {
timeSeriesCollection.addSeries( generator.equationToTimeseriesRK4( 10.0 ) );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
sender.publish( RabbitMQGenerateSender.GENERATE_DATA_QUEUE_NAME,
timeSeriesCollectionObjectMnanager.toBytes( timeSeriesCollection ) );
}
和频道声明:
protected void declareQueue( String queueName ) {
try {
channel.queueDeclare( queueName, true, false, false, null );
} catch ( IOException e ) {
e.printStackTrace();
}
}
try {
connection = factory.newConnection();
channel = connection.createChannel();
int prefetchCount = 1;
channel.basicQos( prefetchCount );
} catch ( IOException | TimeoutException e ) {
e.printStackTrace();
}
我有一些其他应用程序使用rabbitmq,它们具有相同的通道和队列声明参数,并且运行良好。我只有一个应用程序在发送确认时系统性地失败
下面是getChannel()方法:
如果要支持ACK功能,则必须将接收方队列声明为
AutoDelete=false
。
下面是C#中的一个示例(可能与Java有一些小的区别)
提供一些你如何接收消息和发送响应的代码我添加了代码,希望它能帮助你显示接收方队列是如何声明的?是否设置为“自动删除”真?如果是这样,Ack将引发异常。请同时添加getChannel()方法代码。我添加了getChannel()方法我添加了接收方队列和通道声明,看起来队列参数是正确的。redmood,您还应该执行检查连接是否未打开,如果未打开,则创建新连接,正如示例中所示,我确实实现了检查连接,我还实现了一个检查通道,它解决了这个问题。非常感谢你。
public Channel getChannel() {
return channel;
}
private bool PushDataToQueue(byte[] data, string queueName, ref string error)
{
try
{
if (_connection == null || !_connection.IsOpen)
_connection = _factory.CreateConnection();
using (IModel channel = _connection.CreateModel())
{
if (AutoCloseConnection)
_connection.AutoClose = AutoCloseConnection;
// Set the AutoDelete as false, fourth parameter!!!
channel.QueueDeclare(queueName, true, false, false, null);
channel.BasicPublish("", queueName, null, data);
if (!channel.IsClosed)
channel.Close();
}
}
catch (Exception e)
{
error = string.Format("Failed pushing data to queue name '{0}' on host '{1}' with user '{2}'. Error: {3}", queueName, _factory.HostName, _factory.UserName,
e.GetCompleteDetails());
return false;
}
return true;
}