1 Reply Latest reply on Mar 27, 2006 5:25 AM by Joe Wright

    How to stop an MBean properly

    Joe Wright Newbie


      I have a MDB called FileMessage. I have to make a simple servlet for the admins to be able to start and stop the MDB easily (i.e. not having to learn to use the JMX console). To do this I've made the code below. I feel there must be a better way to start and stop MBeans at random. For instance, I have to use queryNames() to get the uniqueIdentifier for the MDB as it has a hashcode appended to it to make it impossible to hardcode.

      This also returns 3 ObjectNames as well:


      The last one seems to be the one I need to invoke the stop(). Ideally I want to be able to specify the ObjectName by hand.

      If anyone can see a way to improve the code below could you let me know please.


       MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
       java.util.Set s =
       mbeanServer.queryNames(new ObjectName(""), new ObjectName(""));
       java.util.Iterator iter = s.iterator();
       ObjectName ejbToStop = null;
       while(iter.hasNext()) {
       Object obj = iter.next();
       (obj.toString().indexOf("FileMessage") > -1) &&
       obj.toString().endsWith("service=EJB")) {
       ejbToStop = (ObjectName) obj;
       mbeanServer.invoke(ejbToStop, "stop", new Object[0], new String[0]);