Using a JBoss MBean in your web-application

For our application which is making use of queues to achieve aynchronious processing we also have build a web-application as kind of dashboard for the system. With this web-application the system administrator can stop, start and schedule batch processes. One thing that was really handy to show on the webpage were the number of messages that are in the queue. We have defined a JMS queue in JBoss with JBoss messaging. Now to get the number of messages in the queue there were two possibilities:

  1. create a queueBrowser and browse to all messages and count them.
  2. access the queue MBean and get the attribute messageCount.

It should be no surprise that I decided to go for the second solution. Here is the (rather simple) code I used to obtain a the MBean and get the property. Remind that this code will only work if it is deployed at the same JBoss instance as were the queue’s MBean is configured!

 public int getMessageCountConsumer() {
      int messageCount = -1;
      try {
         MBeanServer server =;

         // Get the MBeanInfo for the JNDIView MBean
         Hashtable props = new Hashtable();
         props.put("service", "Queue");
         props.put("name", "MyQueue");
         ObjectName name = new ObjectName("jboss.messaging.destination", props);

         messageCount = (Integer)(server.getAttribute(name, "MessageCount"));

      } catch (Exception e) {
         // Error is logged but process is not broken. Not that big problem.
         LOG.error("Error while getting messageCount:", e);
      return messageCount;

About Pascal Alma

Pascal is a senior software developer and architect. Pascal has been designing and building applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Lately he is having great fun by building iOS apps with Swift. Specialties: JEE AWS XML/XSD/XSLT Web Services/SOA Mule ESB/ WSO2 ESB Maven Cloud Technology Swift/ iOS
This entry was posted in JBoss and tagged . Bookmark the permalink.

One Response to Using a JBoss MBean in your web-application

  1. Sunil says:

    Thanks for the tip! Can you tell me the advantages of using custom MBeans please?

Comments are closed.