[rabbitmq-discuss] RabbitMQ JavaAPI Queueing Consumer

Jerry Kuch jerryk at vmware.com
Tue Jan 11 21:39:54 GMT 2011

Hi, Abhishek...

> I want to pass the message to a Separate Java Runnable Class (I am using Java ThreadPool Executors).
> So I want to know how can the separate thread acknowledge the message.
> I believe the message gets re-enqueued once the channel is closed.

The main thing to keep in mind is that you must avoid sharing channel instances between threads, or, if multiple threads are going to use a particular channel, then you need to handle mutual exclusion of access to that channel in your own code.  

One plausible design for your scenario would be the following.  When you pass a "unit of work" object off to your Executor, have it include a reference to the channel on which you need to ack the message once your work with its contents concludes.  Make sure that you add synchronization to ensure that you have exclusive access to that channel when you make calls on it to ack, etc.

> Also how can I handle channel closing down or going stale (due to reasons like network outage)   in the above scenario.

If the channel has closed when your executor thread comes to try to work on it, an exception should be thrown when you try to ack (or do pretty much anything else).  

In this case you've got a situation where a message has been sent to you, and received by you, and you've done the work it entailed, but not acknowledged back to Rabbit that your'e done with it.  

Since you haven't managed to transmit an ack back, as far as Rabbit knows this message is still in play and should be requeued.  

The easiest way to take care of this is for your application to be as idempotent as possible in the case where a requeued work-requiring messaging comes in for a second time.

You'll need to do this in any event, since, in addition to a channel or connection going down, your own code could have crashed after doing its work but before sending an ack to let Rabbit to know that it no longer has to worry about your message. 

Best regards,

More information about the rabbitmq-discuss mailing list