C# - Number of messages in a queue

Posted by Admin on 19-Jul-2010 09:27

I am developing in C# and am trying to find out the number of messages in a certain queue. Is there a method or attribute in Queue or QueueBrowser class that will give me this information? If not, is there a sample code that will allow me to do this? Thanks,

All Replies

Posted by Admin on 22-Jul-2010 05:43

Yes I also share interest in this question.  I developed a C# service (JMS client) for SonicMQ, because my company is keen on a .net dev environment and we were able to use the API's without many issues

But doubts have arisen, when reading the comparison (java client vs .net client) section of the .net app guide. missing features which may be useful to us such as asynchronous send, stream topics, CAA message forward and SocketConnectTimeout.

We can try to work around missing features programatically, but one thing my company insists on is high performance. At the beginning of the project I was in the belief that actually receiving, processing and sending messages back to the broker using JMS

and XML messages would have the same performance..(java/.net). Now a reseller company is telling us this is not the case. The documentation speaks about it being a native C# client.. with JMS message creation almost replicating that of Java version.

can someone shed light on this?

with regards to the queue size question, my reason for wanting to discover this using the API, I suspect is the same as the first poster. I want to dynamically add broker connections with respect to demand (incoming messages).

The only way I have been able to imagine doing this without queue information.. is to set timers on each of my messagelisteners.

for example:

1. you set a maximum number of connections, say 200 and an idle connection number, say 20

2. you have each messageListener that receives a message in under 1 second communicate that another connection should be added

3. as soon as one messageListener thread has a 1 second delay in message receiving it suicides (closes its connection) but only if the total number of connections is NOT less than the idle number (20)

with this strategy you can quite closely follow demand, without too much connection overhead.

I am sure there would be some concurrency issues with keeping connection counts.. you would need a read lock object ...but I think its possible

Posted by Bill Wood on 30-Jul-2010 13:34

There are two variations for this, but both require access to the Management API.  This is not a JMS connection, it is management as as such needs to be Java.

If you can't run Java from your .net client, you could do this:

a) crate a Java service (in the ESB, for example) that uses the Management  API to get queue size.

b) send a JMS message with ReplyTo to this service and it can respond with metrics etc.

Posted by Admin on 02-Aug-2010 09:44

Ok, I looked through the .Net API and you are right it is possible to calculate the queue size using the QueueBrowser

heres the code i used:

          int count = 0;

           object o = null;


           ConnectionFactory factory = (new Sonic.Jms.Cf.Impl.ConnectionFactory());

            ((Sonic.Jms.Cf.Impl.ConnectionFactory)factory).setConnectionURLs("YOURURL");

            ((Sonic.Jms.Cf.Impl.ConnectionFactory)factory).setConnectID("YOURID");

            Connection conn = factory.createConnection("Administrator", "Administrator");

            Session _sSession = conn.createSession(false, SessionMode.AUTO_ACKNOWLEDGE);

            Queue sQ = _sSession.createQueue("YOURQUEUENAME");

            QueueBrowser qB = _sSession.createBrowser(sQ);

            System.Collections.IEnumerator messEnum = qB.getEnumeration();

       

            while (messEnum.MoveNext())

            {

                o = messEnum.Current;

                count++;

            }

            o = null;

                  

            log.Info("message count is: "+count);

            qB.close();

            conn.close();

its not so clean as you have to assign the Current object in IEnumerator for it to work... but you get access to the whole message object if you want.

please ignore my earlier post.. its not so helpful... but always keen to know of examples where you CAN'T use .Net as a JMS client and have to use Java

This thread is closed