2 Replies Latest reply on Jul 20, 2011 6:16 AM by Franz Developer

    MBeans anzeigen

    Franz Developer Newbie

      Hello everybody!

       

      When i navigate at my local pc to that adress: "127.0.0.1:8180/jmx-console/" i'm able to see all processes (Are that processes or mbeans?).

      For example i can see:

       

       

      etc,...

       

      Now i want to make my own JMX. I want to display the MBeans from 127.0.0.1:8180/jmx-console in my own java program.

      So i worte this code.

       

      ----------------------------------------------------------------------------------------------------------------------------------------------------

       

      package at.system;

      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.Map;
      import java.util.Set;
      import java.util.TreeMap;

      import javax.management.InstanceNotFoundException;
      import javax.management.MBeanInfo;
      import javax.management.MBeanServerConnection;
      import javax.management.ObjectName;
      import javax.management.remote.JMXConnector;
      import javax.management.remote.JMXConnectorFactory;
      import javax.management.remote.JMXServiceURL;
      import javax.naming.Context;
      import com.liferay.util.bridges.mvc.MVCPortlet;

      public class Process extends MVCPortlet
      {
      public Process()
      {
        Initialize();
      }

       

      private void Initialize()
      {
        System.out.println("Programm erfolgreich gestartet");
        String attribName = "ProcessCpuTime";
        String objName = "java.lang:type=OperatingSystem";
        String url = "127.0.0.1:1234";

        String user = null;
        String password = null;
       
        JMXConnector jmxCon = null;
        try
        {  
         jmxCon = ConnectToJMX(url, user, password);
         MBeanServerConnection mBScon = jmxCon.getMBeanServerConnection();
         ShowAllObjects(mBScon);
         createJRockitConsoleMBean(objName, mBScon);
         Set<ObjectName> objectNames = mBScon.queryNames(new ObjectName(objName), null);
         for(ObjectName on : objectNames)
         {
          Object attr = mBScon.getAttribute(on, attribName);
         }
        }
        catch(Exception ex)
        {
         ex.printStackTrace();
         System.out.println("Die Verbindung konnte nicht hergestellt werden. Fehlermeldung: " + ex.getMessage());
        }
      }

       

      /*
        * Diese Methode gibt alle Objekte aus
        */
      private void ShowAllObjects(MBeanServerConnection Mbscon) throws Exception
      {
        Print("----------");
        Print("Anzahl der MBeans: " + Mbscon.getMBeanCount());
        Iterator<?> itr = Mbscon.queryMBeans(null, null).iterator();
        while(itr.hasNext())
         System.out.println(itr.next());
        Print("----------");
       
        Print("Beginne mit dem auslesen aller Prozesse mit Methode 2");
        TreeMap domainData = new TreeMap();
        ObjectName filterName = null;
        Set objectNames = Mbscon.queryNames(filterName, null);
        Iterator objectNamesIter = objectNames.iterator();
        while(objectNamesIter.hasNext())
        {
         ObjectName name = (ObjectName)objectNamesIter.next();
         MBeanInfo info = Mbscon.getMBeanInfo(name);
         String domainName = name.getDomain();
        
         Print(name.toString());
         //Print(info.toString());
         //Print(domainName);
        } 
      }

       

      /*
        * Initialisierung des JRockit um die Attribute abfragen zu können
        */
      private void createJRockitConsoleMBean(String objectName, MBeanServerConnection mBeanServerConn) throws Exception
      {
        if(objectName != null || !objectName.startsWith("oracle.jrockit.management"))
         return;
        try
        {
         mBeanServerConn.getMBeanInfo(new ObjectName("oracle.jrockit.management:type=JRockitConsole"));
        } catch(InstanceNotFoundException ex)
        {
         mBeanServerConn.createMBean("oracle.jrockit.management.JRockitConsole", null);
        }
      }

       

      /*
        * Diese Methode gibt eine Connection zurück.
        */
      private JMXConnector ConnectToJMX(String url, String user, String password) throws Exception
      {
        String serviceUrl = "service:jmx:rmi:///jndi/rmi://" + url + "/jmxrmi";
        if(user == null || password == null || user == "" || password == "")
         return JMXConnectorFactory.connect(new JMXServiceURL(serviceUrl));
       
        Map<String, Object> envMap = new HashMap<String, Object>();
        envMap.put("jmx.remote.credentials", new String[] { user, password });
        envMap.put(Context.SECURITY_PRINCIPAL, user);
        envMap.put(Context.SECURITY_CREDENTIALS, password);
        return JMXConnectorFactory.connect(new JMXServiceURL(serviceUrl), envMap);
      }

       

      private void Print(String message)
      {
        System.out.println(message);
      }
      }

       

       

      ----------------------------------------------------------------------------------------------------------------------------------------------------

       

      But the problem is, that i only get a small list of MBeans when i start the program like that:

       

       

       

       

      sun.management.CompilationImpl[java.lang:type=Compilation]

      java.util.logging.Logging[java.util.logging:type=Logging]

      com.sun.management.UnixOperatingSystem[java.lang:type=OperatingSystem]

      sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=PS Old Gen]

      sun.management.MemoryImpl[java.lang:type=Memory]

      javax.management.MBeanServerDelegate[JMImplementation:type=MBeanServerDelegate]

      sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=PS Perm Gen]

      sun.management.RuntimeImpl[java.lang:type=Runtime]

      sun.management.GarbageCollectorImpl[java.lang:type=GarbageCollector,name=PS MarkSweep]

      sun.management.ThreadImpl[java.lang:type=Threading]

      sun.management.GarbageCollectorImpl[java.lang:type=GarbageCollector,name=PS Scavenge]

      sun.management.HotSpotDiagnostic[com.sun.management:type=HotSpotDiagnostic]

      sun.management.ClassLoadingImpl[java.lang:type=ClassLoading]

      sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=PS Survivor Space]

      sun.management.MemoryManagerImpl[java.lang:type=MemoryManager,name=CodeCacheManager]

      sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=Code Cache]

      sun.management.MemoryPoolImpl[java.lang:type=MemoryPool,name=PS Eden Space]

       

      but that are only 17 beans,.... but at the jmx jboss console i can see over 100,...

       

      Why i only get this small list?
      How can i read more information out?

       

      Best regards,

      Thomas Christof