2 Replies Latest reply on Mar 14, 2004 9:23 AM by dimat

    RMIConnectorImpl problem

      I am trying to do a test application that creates a ServiceMBean and that I can connect to from a client application to read and invoke methods and as a listener. I am using JBoss 3.2.3.

      I was successful connecting to the MBean Server with the RMIAdaptor to read info on the MBean and invoke methods.

      I created a seperate application for the listener using the RMIConnectorImpl class. Here is the code:

      import javax.management.ObjectName;
      import javax.management.NotificationListener;
      import javax.management.Notification;

      import javax.naming.Context;
      import org.jboss.jmx.connector.rmi.RMIConnectorImpl;

      public class JmxListener
      implements NotificationListener {

      public static void main(String[] args) throws Exception {
      JmxListener jmxListener = new JmxListener();
      jmxListener.runTest();
      while (true) {

      }
      }

      public void runTest() throws Exception {
      System.out.println("Setting naming context");

      System.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
      System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      System.setProperty(Context.URL_PKG_PREFIXES,
      "org.jboss.naming:org.jnp.interfaces");

      System.out.println("Creating an RMI connector");
      RMIConnectorImpl server = new RMIConnectorImpl(RMIConnectorImpl.
      NOTIFICATION_TYPE_RMI, null, "localhost");

      if (server != null) {
      ObjectName name = new ObjectName("JBossSar:service=JBossTestService");

      System.out.println("Starting to listen....");
      server.addNotificationListener(name, this, null, null);
      }

      }

      public void handleNotification(Notification notification, Object HandBackObj) {
      System.out.println("Notification occured ......");
      }

      }


      I am getting an error
      javax.naming.NameNotFoundException: jmx:localhost:rmi not bound
      when creating new RMIConnectorImpl

      When I look in the Agent view there is no RMIConnector service only the RMIAdaptor service.
      In my client application when I run the code to check if the RMIConnector is registered I get a false.
      boolean rmiConnectorIsRegistered = server.isRegistered(new ObjectName(
      "DefaultDomain:service=RMIConnector"));

      Is the problem that I have to configure the RMIConnector and if so, how do I do that or is there some other problem? Thanks.

        • 1. Re: RMIConnectorImpl problem

          I finally got this working. I am posting the code for others that might have problems. I could not get the RMIConnectorImpl to work with the constructor shown in my first code example but got it to work with an alternate constructor that took an RMIAdaptor as an argument.

          After this had some problems with Serialization. Put my Listener implementation in a seperate class that implements Serializable and NotificationListener.

          Last problem was that I had to put a copy of the Listener class on the server because got ClassLoaderError when a notification was sent. The solution has 3 classes: JmxListenerClient, JmxListener, and RMIAdaptorFactory. The JmxListenerClass also has to be put on the server.

          Still to do is add the removeListener when the client is stopped. For each time the client is stopped and run again, I get that many notifications for each event.

          The JBossSar:service=JBossTestService MBean in this example is just a counter MBean that has methods to increment and reset the count and an attribute to display the count. This was coded and deployed in a sar file seperately.

          import javax.management.ObjectName;
          import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
          import org.jboss.jmx.connector.rmi.RMIConnectorImpl;
          
          public class JmxListenerClient {
          
           JmxListener jmxListener = null;
          
           public static void main(String[] args) throws Exception {
           JmxListenerClient jmxListener = new JmxListenerClient();
           jmxListener.runTest();
           while (true) {
          
           }
           }
          
           public void runTest() throws Exception {
           jmxListener = new JmxListener();
           ObjectName name = null;
           RMIAdaptor rmiAdaptor = RMIAdaptorFactory.getRMIAdaptor("localhost", "1099");
           RMIConnectorImpl remoteServer = new RMIConnectorImpl(rmiAdaptor);
           if (remoteServer != null) {
           name = new ObjectName("JBossSar:service=JBossTestService");
           System.out.println("Starting to listen....");
           remoteServer.addNotificationListener(name, jmxListener, null, null);
           }
          
           }


          import javax.management.Notification;
          import javax.management.NotificationListener;
          import java.io.Serializable;
          
          public class JmxListener implements Serializable, NotificationListener {
          
           public void handleNotification(Notification notification, Object HandBackObj) {
           System.out.println("Notification occured ......");
           }
          
          }


          import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
          import javax.naming.InitialContext;
          import javax.naming.Context;
          import javax.naming.NamingException;
          
          import java.util.Properties;
          
          public class RMIAdaptorFactory {
           public static RMIAdaptor getRMIAdaptor(String serverId, String port) {
           String jndiName = "/jmx/rmi/RMIAdaptor";
           RMIAdaptor rmiAdaptor = null;
           Context context = initContext(serverId, port);
           if (context != null) {
           try {
           rmiAdaptor = (RMIAdaptor) context.lookup(jndiName);
           } catch (Exception e) {
           System.out.println("Exception getting RMIAdaptor");
           e.printStackTrace();
           }
           }
           return rmiAdaptor;
           }
          
           public static Context initContext(String serverId, String port) {
           Context context = null;
           String jndiUrl = "jnp://" + serverId + ":" + port;
           try {
           Properties props = new Properties();
           props.setProperty(Context.PROVIDER_URL,
           jndiUrl);
          
           props.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
           "org.jnp.interfaces.NamingContextFactory");
           props.setProperty
           (Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
           context = new InitialContext(props);
           }
           catch (NamingException e) {
           System.out.println("Exception getting intial context");
           e.printStackTrace();
           }
           return context;
           }
          
          }


          • 2. Re: RMIConnectorImpl problem
            dimat

            Hi Parisila,

            I've tried your code on jboss-3.2.3 but I don't get any notification from my MBean.
            The bean is a simple one that contains only one attribute, named Message and extends the ServiceMBeanSupport.

            import org.jboss.system.ServiceMBeanSupport;
            
            public class SimpleTest extends ServiceMBeanSupport
             implements SimpleTestMBean {
            
             private String message = null;
            
             public String getMessage(){
             return message;
             }
            
             public void setMessage(String s){
             message = s;
             }


            To test if a notification occurs I use beside your client another one that change the Message attribute value.

            import org.jboss.jmx.adaptor.rmi.RMIAdaptor;
            import javax.management.ObjectName;
            import javax.management.Attribute;
            
            public class SendNotification {
            
             public static void main(String[] args) throws Exception {
             for (int i = 0; i < 10; i++) {
             RMIAdaptor server = RMIAdaptorFactory.getRMIAdaptor("localhost", "1099");
             ObjectName name = new ObjectName("JBossSar:service=JBossTestService");
             server.setAttribute(name, new Attribute("Message", "value" + i));
             }
             }
            }


            Now when I start the JmxListenerClient I don't get any "Notification occured ......" message.

            What I'm doing wrong?

            Thank you in advance for you help.