Wednesday, March 17, 2010

QueueBrowser now supported in NMS.ActiveMQ

After finishing up the QueueBrowser support in ActiveMQ-CPP I went back and reworked a patch that had been submitted to NMS to fully support the QueueBrowser in NMS.ActiveMQ. Wasn't to hard to do since the use was nice enough to submit unit tests along with the patch (love it when they do that) so I just had to tweak some things for all the recent changes that went in to get NMS 1.2.0.

Using the NMS version of the QueueBrowser is pretty simple, you can get the code from NMS.ActiveMQ's trunk. Here's a small snippet of code that shows how to use the QueueBrowser



IQueueBrowser browser = session.CreateBrowser((IQueue)destination);
IEnumerator enumeration = browser.GetEnumerator();

while(enumeration.MoveNext())
{
IMessage message = (IMessage) enumeration.Current();

// ...Do something with the message
}



Since the QueueBrowser returns a .NET IEnumerator instance it also supplies a Reset method that essentially just recreates the Browser session and restarts the Browse from the beginning.

Let us know if you find any bugs!

4 comments:

e.semog said...

Great job, Tim. I'd like to work on some syntactic sugar for this feature that would enable the following syntax usage:

foreach(IQueueBrowser queueBrowser in session.Queues)
{
// Do something with queueBrowser
}

I like using the foreach command for iterating over a group of items. It's very convenient. I'll take a look at it later.

I'm a bit confused about the Close() method. It seems important that this function is called for every instantiated QueueBrowser object. I would have thought that the QueueBrowser object would implement IDisposable and ensure that the Close() method is called when the object is destroyed. Wouldn't this be a better way of handling resource cleanup, or am I missing something?

Tim said...

I'm sure there's still more work to be done to make this implementation better but I wanted to get it in just to get the ball rolling. If you have improvement to make go for it, just reopen the issue and add you changes or make suggestions and I will work on it.

I'm no .NET expert so I don't always think of everything when implementing this stuff, so code reviews are welcome.

Natalia said...

Hi Tim,

I have problem with QueueBrowser in NMS.ActiveMQ. Despite the fact that there are messages in the queue I get empty enumeration all the time. Do you have any idea what may be wrong?

It would be also useful to add functionality to delete selected messages in the queue.

Tim said...

You'd need to post some sample code in order for me to have any idea why it would not be returning any messages.

Deleting Messages from a Queue isn't possible through a Queue browser. You'd need to use JMX or the Webconsole for that. You can do it via a consumer with the individual ack mode and only ack the ones that you want to be taken out of the Queue.