5 Replies Latest reply on Mar 31, 2011 2:58 AM by Sergiu Pienar

    How to stop MDB programmatically

    Sergiu Pienar Expert

      Hi,

       

      I would like to stop some of my MDB's programmatically at JBoss shutdown.

      I'm using JBoss 5.1.0GA.

      I have the following code  :

       

       

      public static void stopAllMDB()
              throws InterruptedException {
              for (String name : Util.MDB_NAMES) {
      
                  if (Util.LOGGER.isDebugEnabled()) {
                      Util.LOGGER.debug("Stop MDB " + name);
                  }
                  System.out.println("Stop MDB " + name);
                  try {
      
                      if (ViBeanUtil.isServerStart()) {
                          Context ctx = new InitialContext();
                          RMIAdaptor server = (RMIAdaptor)ctx.lookup("jmx/rmi/RMIAdaptor");
      
                          String currentVersion = "1.0";
      
                          Hashtable<String, String> ht = new Hashtable<String, String>();
                          ht.put("ear", "myApp" + currentVersion + ".ear");
                          ht.put("jar", "myApp-all-ejbs.jar");
                          ht.put("name", name);
                          ht.put("service", "EJB3");
                          ObjectName objectName = new ObjectName("jboss.j2ee", ht);
      
                          server.invoke(objectName, "stopDelivery", new Object[] {}, null);
      
                      }
                  } catch (Exception e) {
                      ViBeanUtil.LOGGER.error("Failed to stop MDB " + name, e);
                  }
              }
             }
      

       

      but it doesn't seem to work as I can not instantiate InitialContext.

      javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory 
      [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
      

       

       

      Any ideas?

       

      Thx,

           Sergiu

        • 1. Re: How to stop MDB programmatically
          Sergiu Pienar Expert

          After some suggestions by Jaikiran I found this :

           

          public static void stopAllMDB()
                  throws InterruptedException, MalformedObjectNameException, NullPointerException {
          
                  String currentVersion = "1.0";
          
                  for (String name : Util.MDB_NAMES) {
                      String mbean = "jboss.j2ee:ear=myEar" + currentVersion + ".ear,jar=myJar.jar,name=myMDB,service=EJB3";
                      ObjectName objName = new ObjectName(mbean);
          
                      System.out.println("Stop MDB " + name);
                      try {
                          MBeanServer mbeanServer = MBeanServerLocator.locateJBoss();
                          MessagingDelegateWrapperMBean invoker = (MessagingDelegateWrapperMBean)MBeanProxy.get(MessagingDelegateWrapperMBean.class,
                                  objName, mbeanServer);
                          invoker.stopDelivery();
                          }
                      } catch (Exception e) {
                      }
          
          
          

           

          However this fails at

          MessagingDelegateWrapperMBean invoker = (MessagingDelegateWrapperMBean)MBeanProxy.get(MessagingDelegateWrapperMBean.class,
                                  objName, mbeanServer);
          

          with IllegalArgumentsException.

           

          Thx.

          • 2. How to stop MDB programmatically
            Elias Ross Master

            Check (print out) the arguments you're passing in. If they make sense, then print out the entire stack trace.

            1 of 1 people found this helpful
            • 3. Re: How to stop MDB programmatically
              Sergiu Pienar Expert

              I've changed

              MessagingDelegateWrapperMBean invoker = (MessagingDelegateWrapperMBean)MBeanProxy.get(MessagingDelegateWrapperMBean.class,
                                      objName, mbeanServer);
              

              to

              mbeanServer.invoke(objName, "stopDelivery", new Object[] {}, null);
              

              and it does seem to work.

               

              This method returns null ... and I'm a bit confused because the API specifies this :

              Returns:
              any result of the operation
              

              How can I properly check if the MBD's are stopped after the call ?

               

              Thx.

              • 4. Re: How to stop MDB programmatically
                jaikiran pai Master

                I haven't looked at that method, but null is OK I guess.

                • 5. How to stop MDB programmatically
                  Sergiu Pienar Expert

                  Thx jaikiran.I was just curios if null is good.

                  1 of 1 people found this helpful