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 IT consultant and has been working in IT since 1997. He is monitoring the latest development in new technologies (Mobile, Cloud, Big Data) closely and particularly interested in Java open source tool stacks, cloud related technologies like AWS and mobile development like building iOS apps with Swift. Specialties: Java/JEE/Spring Amazon AWS API/REST Big Data Continuous Delivery 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.