[rabbitmq-discuss] basicGet returns null when there are still messages in a queue

Matthias Radestock matthias at lshift.net
Thu Apr 16 13:56:27 BST 2009


Kevin,

Kevin Conaway wrote:
> I have a program that polls every X seconds and tries to process N 
> messages.  For some reason, basicGet() will return null even if there 
> are messages still in the queue.
>  
> For example:
>  
> - I send 100 messages to a queue
> - I have a Thread that polls every 3 seconds and processes 30 messages 
> at a time.
>  
> Doing so will result in processing 30 messages, 30 messages, 7 messages 
> and then leaving 3 messages in the queue.  I can't figure out why those 
> 3 messages don't get processed.  If I rerun my sample program, the last 
> three messages are processed.
>  
> Attached is my sample code using the Java client library

The problem is that you are fetching *31* messages at a time, but only 
acknowledging 30.

To illustrate, here is the sequence your code would go through if 
messageCount was set to 2.

messageCount = 2, sent = 0
get
sent < messageCount == true
  sent++ -> sent = 1
  ack
  get
sent < messageCount == true
  sent++ -> sent = 2
  ack
  get
sent < messageCount == false
print sent -> '2'

So that's 3 gets, but it reports '2' and only acks two.


Matthias.




More information about the rabbitmq-discuss mailing list