All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ltsopensource.jvmmonitor.JVMCollector Maven / Gradle / Ivy

package com.github.ltsopensource.jvmmonitor;

import com.github.ltsopensource.core.domain.monitor.JvmMData;
import com.github.ltsopensource.jvmmonitor.mbean.JVMGCMBean;
import com.github.ltsopensource.jvmmonitor.mbean.JVMInfoMBean;
import com.github.ltsopensource.jvmmonitor.mbean.JVMMemoryMBean;
import com.github.ltsopensource.jvmmonitor.mbean.JVMThreadMBean;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Robert HG ([email protected]) on 9/15/15.
 */
public class JVMCollector {

    /**
     * 收集信息
     */
    public static JvmMData collect() {

        JvmMData JVMMData = new JvmMData();
        // memory
        Map memoryMap = JVMMonitor.getAttribute(JVMConstants.JMX_JVM_MEMORY_NAME,
                getAttributeList(JVMMemoryMBean.class));
        JVMMData.setMemoryMap(memoryMap);
        // gc
        Map gcMap = JVMMonitor.getAttribute(JVMConstants.JMX_JVM_GC_NAME,
                getAttributeList(JVMGCMBean.class));
        JVMMData.setGcMap(gcMap);

        // thread
        Map threadMap = JVMMonitor.getAttribute(JVMConstants.JMX_JVM_THREAD_NAME,
                getAttributeList(JVMThreadMBean.class));
        JVMMData.setThreadMap(threadMap);

        return JVMMData;
    }

    private static List getAttributeList(Class clazz) {
        Method[] methods = clazz.getDeclaredMethods();
        List attributeList = new ArrayList(methods.length);
        for (Method method : methods) {
            // 去掉 get 前缀
            attributeList.add(method.getName().substring(3));
        }
        return attributeList;
    }

    public static Map getJVMInfo() {
        return JVMMonitor.getAttribute(JVMConstants.JMX_JVM_INFO_NAME,
                getAttributeList(JVMInfoMBean.class));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy