当消费者处于Down并且Up时,RabbitMQ预取被忽略

时间:2015-09-01 23:27:23

标签: rabbitmq prefetch consumer

当消费者瘫痪,消费者起床后,我的基本问题被忽略了。例如,假设消费者关闭并且5个消息从生产者到达。如果消费者没有运行,如果交换机/队列是持久的,这些消息将存储在磁盘中(我认为!)。

如果我将basicQos设置为channel.basicQos(0,3,true),我的消费者在启动时会收到3条以上的消息。为什么呢?!?

另一方面,如果消费者在从队列接收消息时正在运行,一切正常(只有3条消息从队列中读取)...我的代码如下:

factory = new ConnectionFactory();
factory.setHost(mRabbitMQHost); //may get server address from file configuration.
factory.setUsername(mRabbitMQUsername); 
factory.setPassword(mRabbitMQPassword);
connection = factory.newConnection();
channel = connection.createChannel();

channel.exchangeDeclare("exchangeName", "direct", true); //True enables durability
consumer = new QueueingConsumer(channel);

for (QGQueues queue : QGQueues.values()) {
    String queueName = queue.getQueueName();
    channel.queueDeclare(queueName, true, false, false, null);
    channel.queueBind(queueName, "exchangeName", queue.getRoutingKey());
    channel.basicConsume(queueName, false, consumer); //false enables ACK message to RabbitMQ server    
}

channel.basicQos(0, 3, true);

谢谢!

1 个答案:

答案 0 :(得分:1)

我打赌你需要在做其他任何事情之前设置QoS。

将您的代码更改为此订单:



channel = connection.createChannel();

// set QoS immediately
channel.basicQos(0, 3, true);

channel.exchangeDeclare("exchangeName", "direct", true); //True enables durability
consumer = new QueueingConsumer(channel);

for (QGQueues queue : QGQueues.values()) {
    String queueName = queue.getQueueName();
    channel.queueDeclare(queueName, true, false, false, null);
    channel.queueBind(queueName, "exchangeName", queue.getRoutingKey());
    channel.basicConsume(queueName, false, consumer); //false enables ACK message to RabbitMQ server    
}

这将确保在您尝试使用任何消息之前设置预取限制。