Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Java run-time monitoring, Part 1: Run-time performance and availability monitoring for Java systems

Techniques and patterns


Return to article



JMX collector's collect() method
                

public void collect() {
   CompositeData compositeData = null;
   String type = null;
   try {
      log("Starting JMX Collection");
      long start = System.currentTimeMillis();
      ObjectName on = null;
      // Class Loading Monitoring
      on = objectNameCache.get(CLASS_LOADING_MXBEAN_NAME);
      tracer.traceDeltaSticky((Long)jmxServer.getAttribute(on, "TotalLoadedClassCount"), 
        hostName, "JMX", on.getKeyProperty("type"), "TotalLoadedClassCount");
      tracer.traceDeltaSticky((Long)jmxServer.getAttribute(on, "UnloadedClassCount"), 
        hostName, "JMX", on.getKeyProperty("type"), "UnloadedClassCount");
      tracer.traceSticky((Integer)jmxServer.getAttribute(on, "LoadedClassCount"), 
        hostName, "JMX", on.getKeyProperty("type"), "LoadedClassCount");         
      // Compilation Monitoring
      on = objectNameCache.get(COMPILATION_MXBEAN_NAME);
      tracer.traceDeltaSticky((Long)jmxServer.getAttribute(on, "TotalCompilationTime"), 
        hostName, "JMX", on.getKeyProperty("type"), "CompilationTime");         
      // Thread Monitoring
      on = objectNameCache.get(THREAD_MXBEAN_NAME);
      tracer.traceDeltaSticky((Long)jmxServer.getAttribute(on,"TotalStartedThreadCount"), 
        hostName, "JMX", on.getKeyProperty("type"), "StartedThreadRate");
      tracer.traceSticky((Integer)jmxServer.getAttribute(on, "ThreadCount"), hostName, 
        "JMX", on.getKeyProperty("type"), "CurrentThreadCount");
      // Memory Usage
      on = objectNameCache.get(MEMORY_MXBEAN_NAME);
      compositeData = (CompositeData)jmxServer.getAttribute(on, "HeapMemoryUsage");
      for(Object key: compositeData.getCompositeType().keySet()) {
         tracer.traceSticky((Long)compositeData.get(key.toString()), hostName, "JMX", 
          "Memory", "HeapMemory", key.toString());
      }
      compositeData = (CompositeData)jmxServer.getAttribute(on, "NonHeapMemoryUsage");
      for(Object key: compositeData.getCompositeType().keySet()) {
         tracer.traceSticky((Long)compositeData.get(key.toString()), hostName, "JMX", 
          "Memory", "NonHeapMemory", key.toString());
      }         
      // Memory Pool Monitoring
      if(!objectNameQueryCache.containsKey("MP_MBEAN_QUERY")) {
         objectNameQueryCache.put("MP_MBEAN_QUERY",(Set<ObjectName>)
             jmxServer.queryNames(objectNameCache.get("MP_MBEAN_QUERY"), null));
      }         
      for(ObjectName mpOn: objectNameQueryCache.get("MP_MBEAN_QUERY")) {
         compositeData = (CompositeData)jmxServer.getAttribute(mpOn, "Usage");
         type = (String)jmxServer.getAttribute(mpOn, "Type");
         for(Object key: compositeData.getCompositeType().keySet()) {
           tracer.traceSticky((Long)compositeData.get(key.toString()), hostName, 
           "JMX","MemoryPools", type, 
           mpOn.getKeyProperty("name"), key.toString());
         }         
      }         
      // Garbage Collection Monitoring.
      if(!objectNameQueryCache.containsKey("GC_MBEAN_QUERY")) {
         objectNameQueryCache.put("GC_MBEAN_QUERY", 
            (Set<ObjectName>)jmxServer.queryNames(
            objectNameCache.get("GC_MBEAN_QUERY"), null));
      }         
      for(ObjectName gcOn: objectNameQueryCache.get("GC_MBEAN_QUERY")) {            
         tracer.traceDeltaSticky((Long)jmxServer.getAttribute(gcOn, "CollectionCount"), 
            hostName, "JMX", gcOn.getKeyProperty("type"), 
            gcOn.getKeyProperty("name"), "CollectionRate");
         tracer.traceDeltaSticky((Long)jmxServer.getAttribute(gcOn, "CollectionTime"), 
            hostName, "JMX", gcOn.getKeyProperty("type"), 
            gcOn.getKeyProperty("name"), "CollectionTimeRate");            
      }
      // Done
      long elapsed = System.currentTimeMillis()-start;
      tracer.trace(elapsed, hostName, "JMX", "JMX Collector", 
         "Collection", "Last Elapsed Time");
      tracer.trace(new Date(), hostName, "JMX", "JMX Collector", 
         "Collection", "Last Collection");         
      log("Completed JMX Collection in ", elapsed, " ms.");         
   } catch (Exception e) {
      log("Failed:" + e);
      tracer.traceIncident(hostName, "JMX", "JMX Collector", 
         "Collection", "Collection Errors");
   }
}


Return to article