0 Replies Latest reply on Mar 15, 2005 4:54 AM by Sridhar Vishwanathan

    Monitoring an MBean

    Sridhar Vishwanathan Newbie

      I wrote an MBean to get CPU usage stats. Then I had the JMx's Gauge monitor ping that MBean of mine for data. I did the same thing for the system MBean that JVM provides and that works fine. But the Gauge Monitor tracking my CPU Mbean does not sends me any notification. Apparently there might be something wrong in the way i'm implementing the MBean. I'm attaching the following code snippets

      CPUUsage.java (implemntation of CPUUsageMBean)
      public double getCurrentCPUUsage()
      {
      System.out.println (" Invoked getCurrentCPUUsage");
      //Here is the logic for gettin the CPU Usage ...since it wasnt workin so for //simplicity i 'm just returning a random number here
      CPUUsage = Math.random () * 10;
      return CPUUsage;
      }

      Now i have set the threshold for the CPUUsage MBean to 2.00. Although my JMX console shows values greater than this value, i dont get the notified by gauge monitor....
      The following snippet is for the gaugemonitor:

      public class JMXMonitor implements DataMonitor, NotificationListener {

      //private DataListener dataListener;

      public static void main(String[] args) {
      Map mbeanMap = new HashMap ();

      mbeanMap.put ("MonitorMBeanName", "XYZ:service=CPUGaugeMonitor5");
      mbeanMap.put ("MonitorType", "Gauge");
      mbeanMap.put ("ObservedObject", "com.xyz.mbean:service=CPUUsage");
      mbeanMap.put ("ObservedAttribute", "CurrentCPUUsage");
      mbeanMap.put ("NotifyHigh", new Boolean (true));
      mbeanMap.put ("NotifyLow", new Boolean (true));
      mbeanMap.put ("DifferenceMode", new Boolean (true));
      mbeanMap.put ("GranularityPeriod", new Long (1000));
      mbeanMap.put ("ThresholdHigh", new Double (2.0));
      mbeanMap.put ("ThresholdLow", new Double(0.0));

      JMXMonitor dm = new JMXMonitor ();
      dm.execute((HashMap) mbeanMap);


      mbeanMap.put ("MonitorMBeanName", "XYZ:service=JVMGaugeMonitor4");
      mbeanMap.put("MonitorType", "Counter");
      mbeanMap.put ("ObservedObject", "jboss.system:type=ServerInfo");
      mbeanMap.put ("ObservedAttribute", "FreeMemory");
      mbeanMap.put ("GranularityPeriod", new Long (5000));
      mbeanMap.put ("Threshold", new Long (16101010));
      mbeanMap.put ("Notify", new Boolean (true));

      JMXMonitor dm1 = new JMXMonitor ();
      dm1.execute((HashMap) mbeanMap);


      }
      public void execute(HashMap map) {
      // Get all the attributes from the map and set the mbeans appropriately.
      // Register the mbean
      // Register notification
      RMIConnectorImpl server = getMbeanServer();
      String mt = (String) map.get("MonitorType");

      if (mt.equalsIgnoreCase("Gauge")) {
      ObjectName monitorName;
      try {
      monitorName = new ObjectName((String) map.get ("MonitorMBeanName"));
      server.createMBean("javax.management.monitor.GaugeMonitor",
      monitorName);
      ObjectName mbeanObjName = new ObjectName((String) map
      .get("ObservedObject"));

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

      AttributeList attributes = new AttributeList();
      attributes.add(new Attribute("ObservedObject", mbeanObjName));
      attributes.add(new Attribute("ObservedAttribute", map
      .get("ObservedAttribute")));
      attributes.add(new Attribute("NotifyHigh", map
      .get("NotifyHigh")));
      attributes.add(new Attribute("DifferenceMode", map
      .get("DifferenceMode")));
      attributes
      .add(new Attribute("NotifyLow", map.get("NotifyLow")));
      attributes.add(new Attribute("GranularityPeriod", map
      .get("GranularityPeriod")));
      server.setAttributes(monitorName, attributes);

      Double tl = (Double) map.get("ThresholdLow");
      Double th = (Double) map.get("ThresholdHigh");

      System.out.println ("High = " + th + "Low = " + tl);

      server.invoke(monitorName, "setThresholds", new Object[] { th,
      tl }, new String[] { "java.lang.Number",
      "java.lang.Number" });

      server.invoke(monitorName, "start", new Object[0],new String[0]);
      System.out.println("Starting to listen Guage Monitoring ....");
      server.addNotificationListener(monitorName, this, null, null);

      } catch (MalformedObjectNameException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (InstanceAlreadyExistsException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (MBeanRegistrationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (NotCompliantMBeanException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (ReflectionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (MBeanException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (InstanceNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      }else {
      if (mt.equalsIgnoreCase("Counter")) {
      ObjectName monitorName = null;
      try {
      monitorName = new ObjectName((String) map.get ("MonitorMBeanName"));
      } catch (MalformedObjectNameException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      try {

      server.createMBean("javax.management.monitor.CounterMonitor",
      monitorName);
      } catch (InstanceAlreadyExistsException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (MBeanRegistrationException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (NotCompliantMBeanException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (ReflectionException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      } catch (MBeanException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }
      ObjectName mbeanObjName=null;
      try {
      mbeanObjName = new ObjectName((String) map
      .get("ObservedObject"));
      } catch (MalformedObjectNameException e2) {
      // TODO Auto-generated catch block
      e2.printStackTrace();
      }
      System.out.println("Configuring the Counter monitor");

      AttributeList attributes = new AttributeList();
      attributes.add(new Attribute("ObservedObject", mbeanObjName));
      attributes.add(new Attribute("ObservedAttribute", map
      .get("ObservedAttribute")));
      attributes.add ( new Attribute ("Notify", map.get ("Notify")));
      attributes.add ( new Attribute ("Threshold", map.get ("Threshold")));
      attributes.add(new Attribute("GranularityPeriod", map
      .get("GranularityPeriod")));
      try {
      server.setAttributes(monitorName, attributes);
      } catch (InstanceNotFoundException e4) {
      // TODO Auto-generated catch block
      e4.printStackTrace();
      } catch (ReflectionException e4) {
      // TODO Auto-generated catch block
      e4.printStackTrace();
      }
      System.out.println("Starting to listen Counter Monitoring ....");
      try {

      server.invoke(monitorName, "start", new Object[0],
      new String[0]);
      server.addNotificationListener(monitorName, this, null, null);
      } catch (InstanceNotFoundException e3) {
      // TODO Auto-generated catch block
      e3.printStackTrace();
      } catch (MBeanException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (ReflectionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      }

      }

      }
      private RMIConnectorImpl getMbeanServer() {
      // TODO Auto-generated method stub
      InitialContext ic;
      try {
      ic = new InitialContext();
      RMIAdaptor rmiAdaptor = (RMIAdaptor) ic
      .lookup("jmx/rmi/RMIAdaptor");
      return (new RMIConnectorImpl(rmiAdaptor));

      } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      return null;

      }

      public void RegisterNotification(DataListener listener) {
      // TODO Auto-generated method stub
      dataListener = listener;

      } */

      public void handleNotification(Notification event, Object handback) {
      // Handle the event and call the application registered method.
      System.out.print ("TS:" + new Date ());
      System.out.println(" handleNotification, event: " + event);
      }
      }


      Although the JVMGaugeMonitor works fine but the CPUGaugeMonitor fails....
      someone please help....

      Sridhar