9 Replies Latest reply on Sep 23, 2002 9:22 AM by Derek Hulley

    Monitor MBean from remote location

    Kashif Rasheed Newbie

      Hi JBoss Gurus,

      I have found this forum highly impressive to solve the JBoss relevant problems. Now the Q is here;

      JBoss 3.0 Beta ia configured and I have a client running on remote machine. Now I have connected to the MBeanServer using RMIAdaptor.

      Now I want to monitor an MBean and perform some operation on changing the value . ( Using StringMonitor ).

      I have added a listener to the StringMonitor to listen to the changes. But the execution is not entering handleNotification() method if I changes the attribute value.

      Here is the code ;


      public class JMSAgent implements NotificationListener{

      public JMSAgent() {
      }
      public static void main(String[] args) {

      InitialContext initialContext = null;
      Hashtable env = new Hashtable();
      env.put(Context.PROVIDER_URL,"jnp://zealous:1099");
      env.put(Context.INITIAL_CONTEXT_FACTORY ,"org.jnp.interfaces.NamingContextFactory");
      env.put(Context.URL_PKG_PREFIXES ,"org.jboss.naming:org.jnp.interfaces");

      try{
      if (initialContext == null)
      {
      initialContext = new InitialContext(env);
      }
      RMIAdaptor server = (RMIAdaptor) initialContext.lookup("jmx:zealous:rmi");

      ObjectName mbeanObjName = new ObjectName("jboss.mq:service=JMSProviderLoader,name=JBossMQProvider");

      Object obj = new Object();

      StringMonitor stringMonitor = new StringMonitor();
      stringMonitor.setObservedObject(mbeanObjName);
      stringMonitor.setStringToCompare("Stopped");
      stringMonitor.setObservedAttribute("StateString");
      stringMonitor.setNotifyMatch(true);
      stringMonitor.setGranularityPeriod(10000);
      server.addNotificationListener(mbeanObjName , new JMSAgent(),null,obj) ;
      //stringMonitor.addNotificationListener(new JMSAgent(), null , obj);

      stringMonitor.start();
      } catch(Exception e){
      System.out.println("Error getting inital Context." + e.getMessage());

      }
      }

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


      Guys.....Help me...

      Bu Bye

        • 1. Re: Monitor MBean from remote location
          Greg Turner Apprentice

          You might want to think about upgrading to JBoss 3.0.0 Final.

          • 2. Re: Monitor MBean from remote location
            Juha Lindfors Master

            Is there a firewall between the client and the remote mbean server?

            -- Juha

            • 3. Re: Monitor MBean from remote location
              Adrian Brock Master

              Hi,

              1) You also need to create the StringMonitor
              on the remote MBeanServer using the RMIAdaptor's
              createMBean() and setAttributes().
              Roll on JSR160. :-)

              2) You need to invoke start() on the StringMonitor.

              3) You need to add the notification to the ObjectName
              of the StringMonitor.

              4) Is Object Serializable? I don't think your handback will pass over the network?

              In anycase, I don't think this worked in the beta.
              The notification source wasn't set to the ObjectName of
              the StringMonitor so the notification probably wouldn't
              come back over the network. :-(

              Regards,
              Adrian

              • 4. Re: Monitor MBean from remote location
                Kashif Rasheed Newbie

                Can you demostrate this by a piece of code which would create StringMonitor on the remote MBeanServer using RMiAdaptor's createMBean.

                I am invoking start() on the StringMonitor.

                Which object you are refering to be serialized? Please describe this in bit detail.

                What changes must I have to made in the code in order to run the app accordingly. Please assist me with the help of sample code.

                It's an urgent task..........I have already slipped my deadline.

                THANK YOU ALL FOR THE HELP IN RESOLVING THIS ISSUE.

                PS. I am new to the JBoss and JMX Agent .

                Bu Bye

                • 5. Re: Monitor MBean from remote location
                  Adrian Brock Master

                  Try this.

                  I'm no expert on the connector/adapter stuff.


                  import javax.management.ObjectName;
                  import javax.management.NotificationFilter;
                  import javax.management.NotificationListener;
                  import javax.management.Notification;
                  import javax.management.Attribute;
                  import javax.management.AttributeList;

                  import javax.naming.Context;

                  import org.jboss.jmx.connector.RemoteMBeanServer;
                  import org.jboss.jmx.connector.rmi.RMIConnectorImpl;

                  public class JMSAgent
                  implements NotificationListener
                  {
                  // The connection to the remote server

                  private static RMIConnectorImpl server = null;

                  // The string monitor

                  private static ObjectName monitorName = null;

                  public static void main(String[] args)
                  throws Exception
                  {
                  System.out.println("Setting naming context");

                  System.setProperty(Context.PROVIDER_URL,"jnp://zealous: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");

                  server = new RMIConnectorImpl(RMIConnectorImpl.NOTIFICATION_TYPE_RMI,
                  null, "zealous");

                  System.out.println("Creating the string monitor");

                  monitorName = new ObjectName("test:service=StringMonitor");
                  server.createMBean("javax.management.monitor.StringMonitor", monitorName);

                  System.out.println("Configuring the string monitor");

                  ObjectName mbeanObjName = new ObjectName("jboss.mq:service=JMSProviderLoader,name=JBossMQProvider");
                  AttributeList attributes = new AttributeList();
                  attributes.add(new Attribute("ObservedObject", mbeanObjName));
                  attributes.add(new Attribute("ObservedAttribute", "StateString"));
                  attributes.add(new Attribute("StringToCompare", "Stopped"));
                  attributes.add(new Attribute("NotifyMatch", new Boolean(true)));
                  attributes.add(new Attribute("GranularityPeriod", new Long(10000)));
                  server.setAttributes(monitorName, attributes);
                  server.invoke(monitorName, "start", new Object[0], new String[0]);

                  System.out.println("Starting to listen....");

                  server.addNotificationListener(monitorName, new JMSAgent(), null, null);
                  }

                  // This is invoked when some idiot stops the mbean

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


                  The main changes are:
                  Using the connector instead of the adaptor.
                  The monitor has to be registered in an MBeanServer that
                  can access the MBean being monitored.

                  Bugs:
                  The monitor and notification listener are not
                  removed if you stop the program.

                  Regards,
                  Adrian

                  • 6. Re: Monitor MBean from remote location
                    Kashif Rasheed Newbie

                    After executing this code; I got this error;

                    java.lang.NoClassDefFoundError: org/jboss/jmx/connector/notification/ClientNotificationListener
                    at java.lang.ClassLoader.defineClass0(Native Method)
                    at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
                    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
                    at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)

                    I have added jmx-rmi-connector-client.jar file which has ClientNotificationListener class and restarted the server but the error is still there.

                    What you think the reason of this error?

                    Bu Bye

                    • 7. Re: Monitor MBean from remote location
                      Kashif Rasheed Newbie

                      I have added required files to the Cient Classpath and this error has been overcame.

                      This is the output of the program execution . Program control doesn't enter handleNotification() even if I changes the StateString value from Started to Stopped. What would be the reason?

                      Please help me.....as did before.


                      Setting Naming Context
                      Creating RMI Connector
                      RMIClientConnectorImp.start(), got Naming Context: javax.naming.InitialContext@360be0, environment: {java.naming.provider.url=202.125.129.86:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces:org.jboss.naming:org.jnp.interfaces}, name in namespace:
                      RMIClientConnectorImpl.start(), got remote connector: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl_Stub[RemoteStub [ref: [endpoint:[202.125.129.86:1485](remote),objID:[4b6009:ef30955971:-8000, 3]]]]
                      Creating String monitor
                      Configurng string Monitor
                      Starting to listen ........

                      • 8. Re: Monitor MBean from remote location
                        Kashif Rasheed Newbie

                        Hi,

                        I have added required classes in Client's CLASSPATH and this error overcam.e

                        After executing this code, I m getting an strange type of error;

                        Setting Naming Context
                        Creating RMI Connector
                        RMIClientConnectorImp.start(), got Naming Context: javax.naming.InitialContext@360be0, environment: {java.naming.provider.url=202.125.129.86:1099, java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces:org.jboss.naming:org.jnp.interfaces}, name in namespace:
                        RMIClientConnectorImpl.start(), got remote connector: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl_Stub[RemoteStub [ref: [endpoint:[202.125.129.86:1485](remote),objID:[4b6009:ef30955971:-8000, 3]]]]
                        Creating String monitor
                        Configurng string Monitor
                        Starting to listen ........

                        What might be the cause of this error?

                        I reckon I need to add some more classes to my classpath but which classes......Please Help me.

                        Thanx

                        Bu Bye

                        • 9. Re: Monitor MBean from remote location
                          Derek Hulley Newbie

                          Hi,

                          I might just be a humble mortal, but there doesn't seem to be any error message. The RMI stub has been brought into action and the calls are being executed according to plan.