Knowledge Seeking

HornetQ 2.1.X – How do I do Query on a Queue ? September 30, 2010

Filed under: HornetQ — nicozan @ 6:55 pm

Sometimes is useful to do a query on queue, especially when running a test on your Java Application. The HornetQ API Core provides the ClientSession class which has the queueQuery() instance method. This method allows us to get some attributes from a single Queue in the HornetQ Server.

This attributes are:

    • Number of Consumers (.getConsumerCount())

    • Number of Messages in the Queue (.getMessageCount())

    • Address (.getAddress())

    • Some Others…

Lets see the next code snippet that is inside a Test using Junit 4.0:


SimpleString simpleStr = new SimpleString(“myQueue”);

QueueQuery query = session.queueQuery(simpleStr);

Assert.assertTrue(“Message Sent SuccessFully”,query.getMessageCount() == 1);

As you can see a session has been created and a producer for the “myQueue” too. Please remember that this queue should be defined in the hornetq-configuration.xml file.

The SimpleString object will help us to pass the queue name to the queueQuery() instance method.

After we call this method we validate the test using one of the queues attribute.