我正在与java应用程序一起使用rabbitmq服务器。当应用程序收到粒子队列上的消息时,它会生成一些数据并将它们发送到另一个队列中。
当运行应用程序时,它接收消息,生成数据并将它们发送到正确的队列中。这些数据在服务器上得到了很好的接收,而且它们是正确的。但是,当应用程序试图向服务器发送确认时,我将得到一个AlreadyClosedException。
服务器的日志中有以下消息:关闭AMQP连接。
下面是handleDelivery函数在rabbitMQ使用者类中的代码:
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()方法:
public Channel getChannel() {
return channel;
}发布于 2015-12-15 15:50:06
如果要支持ACK功能,则必须将接收方队列声明为AutoDelete = false。以下是C#中的一个示例(可能与Java有很小的差异)
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;
}https://stackoverflow.com/questions/34284029
复制相似问题