| 7 May 2007 |
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 (see this post). Now to get the number of messages in the queue there were two possibilities:
- create a queueBrowser and browse to all messages and count them.
- 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 = org.jboss.mx.util.MBeanServerLocator.locateJBoss();
-
-
// Get the MBeanInfo for the JNDIView MBean
-
props.put("service", "Queue");
-
props.put("name", "MyQueue");
-
ObjectName name = new ObjectName("jboss.messaging.destination", props);
-
-
-
// Error is logged but process is not broken. Not that big problem.
-
LOG.error("Error while getting messageCount:", e);
-
}
-
return messageCount;
-
}


1 comment to 'Using a JBoss MBean in your web-application'
5 April 2010
Thanks for the tip! Can you tell me the advantages of using custom MBeans please?