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

    How to stop an MBean properly

    joejag

      Hi,

      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:

      jboss.j2ee:jndiName=local/AAFileMessage@16297593,plugin=pool,service=EJB
      jboss.j2ee:binding=message-driven-bean,jndiName=local/AAFileMessage@16297593,plugin=invoker,service=EJB
      jboss.j2ee:jndiName=local/AAFileMessage@16297593,service=EJB

      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.

      Cheers,
      Joe.

       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();
       if(
       (obj.toString().indexOf("FileMessage") > -1) &&
       obj.toString().endsWith("service=EJB")) {
       ejbToStop = (ObjectName) obj;
       }
       }
      
       mbeanServer.invoke(ejbToStop, "stop", new Object[0], new String[0]);