示例#1
0
  public static void main(String[] args)
      throws MalformedURLException, IOException, MalformedObjectNameException {
    try {
      // Get MbeanServerConnection
      MBeanServerConnection mbc;
      JMXServiceURL url = new JMXServiceURL(JMX_URI);
      JMXConnector jmxcon = JMXConnectorFactory.connect(url, null);
      mbc = jmxcon.getMBeanServerConnection();

      // Get GC Collectors
      Iterator<ObjectName> itr = mbc.queryNames(null, null).iterator();
      ArrayList gcList = new ArrayList();

      while (itr.hasNext()) {
        String matchedName = itr.next().toString();
        if (matchedName.indexOf("GarbageCollector") > 0) {
          gcList.add(matchedName);
        }
      }
      System.out.println(gcList);

      // Fetch GC type collection time
      for (int i = 0; i < gcList.size(); i++) {
        ObjectName queryName = new ObjectName(gcList.get(i).toString());
        System.out.print(queryName + ":");
        System.out.println(mbc.getAttribute(queryName, "CollectionTime"));
      }

      ObjectName uptimeName = new ObjectName("java.lang:type=Runtime");
      System.out.print("Server Uptime" + ":");
      System.out.print(mbc.getAttribute(uptimeName, "Uptime"));
      System.out.println(" Seconds");

      // Fetch heap Memory usage stat
      // committed=523501568, max=523501568, used=106156848
      CompositeDataSupport cdsHeap;
      ObjectName heapMemoryUsage = new ObjectName("java.lang:type=Memory");
      System.out.println(
          cdsHeap = (CompositeDataSupport) mbc.getAttribute(heapMemoryUsage, "HeapMemoryUsage"));
      System.out.println(cdsHeap.get("used"));

      // Fetch nonheap Memory usage stat
      // committed=523501568, max=523501568, used=106156848
      CompositeDataSupport cdsNonHeap;
      ObjectName nonHeapMemoryUsage = new ObjectName("java.lang:type=Memory");
      System.out.println(
          cdsNonHeap =
              (CompositeDataSupport) mbc.getAttribute(nonHeapMemoryUsage, "NonHeapMemoryUsage"));
      System.out.println(cdsNonHeap.get("used"));

    } catch (MBeanException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (AttributeNotFoundException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstanceNotFoundException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ReflectionException ex) {
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
示例#2
0
  public static TaskStatus getTaskStatus(CompositeDataSupport compositeData) {

    TaskStatus status = new TaskStatus((Status) compositeData.get("status"));
    status._reason = compositeData.get("reason");
    status._statusChangeTime = (Date) compositeData.get("statusChangeTime");
    status._percentageDone = ((Integer) compositeData.get("percentage")).intValue();
    return status;
  }
示例#3
0
 private void output(String name, Object attr, String key) {
   if (attr instanceof CompositeDataSupport) {
     CompositeDataSupport cds = (CompositeDataSupport) attr;
     if (key == null) throw new IllegalArgumentException("Key is null for composed data " + name);
     System.out.println(name + ".value " + format(cds.get(key)));
   } else System.out.println(name + ".value " + format(attr));
 }
示例#4
0
 private void output(String name, Object attr) {
   if (attr instanceof CompositeDataSupport) {
     CompositeDataSupport cds = (CompositeDataSupport) attr;
     for (Iterator it = cds.getCompositeType().keySet().iterator(); it.hasNext(); ) {
       String key = it.next().toString();
       System.out.println(name + "." + key + ".value " + format(cds.get(key)));
     }
   } else System.out.println(name + ".value " + format(attr));
 }
 private final long getComposite(String name)
     throws StatUnreachableException, AttributeNotFoundException, InstanceNotFoundException,
         MBeanException, ReflectionException, MalformedObjectNameException,
         NullPointerException {
   final ObjectName objName = new ObjectName(_objectName + name);
   final CompositeDataSupport cds =
       (CompositeDataSupport) mBeanServer.getAttribute(objName, _attrName);
   final long val = ((Number) cds.get(_valProp)).longValue();
   failures = 0;
   return val;
 }
 /**
  * create result as property with name from property prefix When result is an array and
  * isSeparateArrayResults is true, resultproperty used as prefix (<code>
  * resultproperty.0-array.length</code> and store the result array length at <code>
  * resultproperty.length</code>. Other option is that you delimit your result with a delimiter
  * (java.util.StringTokenizer is used).
  *
  * @param propertyPrefix
  * @param result
  */
 protected void createProperty(String propertyPrefix, Object result) {
   if (propertyPrefix == null) propertyPrefix = "";
   if (result instanceof CompositeDataSupport) {
     CompositeDataSupport data = (CompositeDataSupport) result;
     CompositeType compositeType = data.getCompositeType();
     Set<String> keys = compositeType.keySet();
     for (Iterator<String> iter = keys.iterator(); iter.hasNext(); ) {
       String key = iter.next();
       Object value = data.get(key);
       OpenType<?> type = compositeType.getType(key);
       if (type instanceof SimpleType<?>) {
         setProperty(propertyPrefix + "." + key, value);
       } else {
         createProperty(propertyPrefix + "." + key, value);
       }
     }
   } else if (result instanceof TabularDataSupport) {
     TabularDataSupport data = (TabularDataSupport) result;
     for (Iterator<Object> iter = data.keySet().iterator(); iter.hasNext(); ) {
       Object key = iter.next();
       for (Iterator<?> iter1 = ((List<?>) key).iterator(); iter1.hasNext(); ) {
         Object key1 = iter1.next();
         CompositeData valuedata = data.get(new Object[] {key1});
         Object value = valuedata.get("value");
         OpenType<?> type = valuedata.getCompositeType().getType("value");
         if (type instanceof SimpleType<?>) {
           setProperty(propertyPrefix + "." + key1, value);
         } else {
           createProperty(propertyPrefix + "." + key1, value);
         }
       }
     }
   } else if (result.getClass().isArray()) {
     if (isSeparatearrayresults()) {
       int size = 0;
       for (int i = 0; i < Array.getLength(result); i++) {
         if (setProperty(propertyPrefix + "." + size, Array.get(result, i))) {
           size++;
         }
       }
       if (size > 0) {
         setProperty(propertyPrefix + ".Length", Integer.toString(size));
       }
     }
   } else {
     String delim = getDelimiter();
     if (delim != null) {
       StringTokenizer tokenizer = new StringTokenizer(result.toString(), delim);
       int size = 0;
       for (; tokenizer.hasMoreTokens(); ) {
         String token = tokenizer.nextToken();
         if (setProperty(propertyPrefix + "." + size, token)) {
           size++;
         }
       }
       if (size > 0) setProperty(propertyPrefix + ".Length", Integer.toString(size));
     } else {
       setProperty(propertyPrefix, result.toString());
     }
   }
 }