Hi
I figured out the issue. In my example code, there's a race condition
between the calls to browser.GetEnumerator() and
enumerator.MoveNext(). Internally, browser.GetEnumerator() creates a
Message Consumer and enumerator.MoveNext() uses
consumer.ReceiveNoWait() to get the messages. So the call to
ReceiveNoWait() just returns before the in-flight messages are
actually retrieved from the queue. Inserting a delay between the
GetEnumerator() and MoveNext() calls allows time for in-flight
messages to be retrieved and to function properly.
Here is an example with the delay:
var enumerator = browser.GetEnumerator();
await Task.Delay(TimeSpan.FromMilliseconds(100)) // <-- Add delay to
allow in-flight messages to be received.
while (enumerator.MoveNext())
{
System.Collections.IEnumerator enumerator1 = enumerator;
message = enumerator1.Current as IMessage;
if (message is ITextMessage textMessage)
Console.WriteLine($"Found message: {textMessage.Text}");
}
Fyi, you can observe this same race condition behavior with a regular
message consumer as well:
using IMessageConsumer consumer = session.CreateConsumer(queue);
do
{
message = consumer.ReceiveNoWait();
Console.WriteLine($"Found message: {(message as ITextMessage)?.Text}");
}
while (message is not null);
As for the browser and even a consumer, to me, this is an issue
because there are messages in the queue, yet it behaves as if there
aren't any. Instead, wouldn't it be nice to have the internal start-up
of the Consumer return after it receives its in-flight messages?
Anyway, as it stands, the functionality isn't intuitive. So be aware. ;-)
Cheers!
John
On Sat, Apr 25, 2026 at 3:20 PM John <[email protected]> wrote:
>
> Hi-
>
> I am exploring the Browser feature of the Apache NMS AMQP API with the
> latest ActiveMQ and also with the latest Artemis on Docker Desktop,
> and have been unable to iterate over messages in a queue. With both
> Message Brokers, the Browser will connect with no errors. It just
> doesn’t get or go through the messages. I’m wondering if I need to
> enable some feature on the Message Brokers. Or maybe there is
> something else I’m missing?
>
> Here is some similar example code:
>
> IConnectionFactory factory = new
> NmsConnectionFactory("amqp://localhost:5672");
> using IConnection connection = factory.CreateConnection();
> connection.Start();
> using ISession session =
> connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
> IQueue queue = session.GetQueue("FOO.BAR");
>
> using IMessageProducer producer = session.CreateProducer(queue);
>
> for (int i = 0; i < 10; i++)
> producer.Send(producer.CreateTextMessage(i.ToString()));
>
> Console.WriteLine("Browsing messages in queue: " + queue.QueueName);
> IMessage? message;
> using IQueueBrowser browser = session.CreateBrowser(queue);
> var enumerator = browser.GetEnumerator();
> while (enumerator.MoveNext())
> {
> System.Collections.IEnumerator enumerator1 = enumerator;
> message = enumerator1.Current as IMessage;
> if (message is ITextMessage textMessage)
> Console.WriteLine($"Found message: {textMessage.Text}");
> }
>
> Console.WriteLine("Consuming messages in queue: " + queue.QueueName);
> using IMessageConsumer consumer = session.CreateConsumer(queue);
> do
> {
> message = consumer.Receive(TimeSpan.FromSeconds(1));
> Console.WriteLine($"Found message: {(message as ITextMessage)?.Text}");
> }
> while (message is not null);
>
>
> Thanks for the help.
>
> John
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information, visit: https://activemq.apache.org/contact