3 Replies Latest reply on Jul 20, 2003 8:20 PM by Adrian Brock

    How to invoke method of MBean from EJB

    Bensen Chiou Newbie

      Please help. I have spent several days trying to invoke the method of a custom MBean.

      Can EJB invoke the method of custom standard MBean in Jboss?

      I made a ConversionMBean and checked all the JBoss forum messages, but I still can't make
      EJB to invoke the method of ConversionMBean.

      After JBoss started up, I can use JMX Console to verify that ConversionMBean method is working.
      However, when I tried to invoke the method of ConversionMBean from EJB, I got a ReflectionException
      with null error message.

      These are part of the messages logged in server.log:
      [STDOUT] Conversion Class: com.mycom.Conversion
      [STDOUT] ReflectionException. ERROR=null

      Can you please point me in the right direction to:

      1. make it work in JBoss server
      2. Other way portable to any J2EE server

      I am appreciated of any help in advance.



      By the way, I am using jboss_3.0.6_tomcat_4.1.18

      ========= source code in EJB trying to invoke MBean method ======
      try {
      ArrayList svrs = MBeanServerFactory.findMBeanServer(null);
      Iterator iter = svrs.iterator();
      while (iter.hasNext()) {
      MBeanServer svr = (MBeanServer)iter.next();
      String domain = svr.getDefaultDomain();
      if (domain.equals("jboss")) {
      mbeanSvr = svr;

      ObjectName name = new ObjectName( "user:service=ConversionService" );
      //testing - the following statement works.
      MBeanInfo info = mbeanSvr.getMBeanInfo( name );
      System.out.println( "Conversion Class: " + info.getClassName() );

      //Invoke convertTo op
      String opName = "convertTo";
      String sig[] = { "String" };
      Object opArgs[] = { "testString" };

      //The following code throws ReflectionException
      Object result = mbeanSvr.invoke( name, opName, opArgs, sig );

      System.out.println( "invoked Conversion.convertTo()");
      } catch (ReflectionException e) {
      System.out.println("ReflectionException. ERROR=" + e.getMessage());
      } catch (....

      =============== Source code of ConversionMBean.java: ==========================

      public interface ConversionMBean
      public String convertTo( String orgString )
      throws Exception;
      public void start( )
      throws Exception;
      public void stop( )
      throws Exception;

      =========== Source code of Conversion.java ========================

      public class Conversion
      implements ConversionMBean
      public static final String myService = "user:service=ConversionService";
      public static boolean wasInitialized = false;

      public Conversion() {


      //Return null if not initialized
      public String convertTo( String orgString )
      throws Exception
      String rtnString = null;
      StringBuffer sb = new StringBuffer();

      if ( !wasInitialized )
      return ( rtnString );

      sb.append(myService).append(" echo ").append(orgString);
      rtnString = sb.toString();

      return ( rtnString );

      public void start( )
      throws Exception
      //do initialization here
      wasInitialized = true;

      public void stop( )
      throws Exception
      wasInitialized = false;

      ================== user-service.xml in deploy directory ==========

      ================ message logged when jboss starts up ========
      Starting deployment of package: .... conversion.sar
      Deployed package: ... conversion.sar