java获取pid和management包

import java.lang.management.ClassLoadingMXBean;
import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.PlatformManagedObject;
import java.lang.management.RuntimeMXBean;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanServer;

//java获取pid和management包
public class MyDesktop {

    public static String toSimpleJsonString( HashMap<String,String> maps) {
        if (maps == null)
            return "null";
        int iMax = maps.size() - 1;
        if (iMax == -1)
            return "{}";

        StringBuilder b = new StringBuilder();
        b.append('{');
        int count=0;
        for (Map.Entry<String, String> entry : maps.entrySet()) {
        	
			String key=entry.getKey();
			String value=entry.getValue();
			b.append("\"");
			b.append(key);
			b.append("\"");
			b.append(":");
			b.append("\"");
			b.append(value);
			b.append("\"");
			if(count!=iMax)
				b.append(",");
			count++;
		}
        
        b.append('}');
        //{"serialVersionUID":"7523967970034938905","fastTime":"1502692349983","defaultCenturyStart":"0"}
       return b.toString();
    }

    public  static String toDataString(Object course) {

        //StringBuffer sb=new StringBuffer();
        HashMap<String,String> maps=new HashMap<String, String>();
        Class<?> cls=course.getClass();
        Field[] fields=cls.getDeclaredFields();
        for (Field field : fields) {

            Class<?> type=field.getType();
            if(type==String.class|| type==int.class|| type==boolean.class
                    ||type==Integer.class|| type==Boolean.class ||type==Long.class|| type==long.class)
            {
                field.setAccessible(true);
                Object obj="";
                try {
                    obj = field.get(course);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                String name=field.getName();
                String val=obj+"";
                maps.put(name,val);

            }
        }
        return toSimpleJsonString(maps);
        //return JSON.toJSONString(maps);
    }
    //http://blog.csdn.net/yin_jw/article/details/32198177
	public static void main(String[] args) {
		
		// get name representing the running Java virtual machine.  
		RuntimeMXBean runtimeMXBean=ManagementFactory.getRuntimeMXBean();
		String name = ManagementFactory.getRuntimeMXBean().getName();  
		System.out.println(name);  
		// get pid  
		String pid = name.split("@")[0];  
		System.out.println("Pid is:" + pid); 
		System.out.println(runtimeMXBean.getLibraryPath());
		System.out.println(runtimeMXBean.getManagementSpecVersion());
		System.out.println(runtimeMXBean.getUptime());
		System.out.println(runtimeMXBean.getVmName());
		
		OperatingSystemMXBean systemMXBean=ManagementFactory.getOperatingSystemMXBean();
		
		System.out.println(systemMXBean.getArch());
		System.out.println(systemMXBean.getObjectName());
		System.out.println(systemMXBean.getAvailableProcessors());
		System.out.println(systemMXBean.getName());
		System.out.println(systemMXBean.getSystemLoadAverage());
		System.out.println(systemMXBean.getVersion());
		
		ClassLoadingMXBean classLoadingMXBean=ManagementFactory.getClassLoadingMXBean();
		System.out.println(classLoadingMXBean.getLoadedClassCount());
		System.out.println(classLoadingMXBean.getTotalLoadedClassCount());
		System.out.println(classLoadingMXBean.getObjectName());
		
		CompilationMXBean compilationMXBean=ManagementFactory.getCompilationMXBean();
		System.out.println(compilationMXBean.getTotalCompilationTime());
		
		 Set<Class<? extends PlatformManagedObject>> cls=ManagementFactory.getPlatformManagementInterfaces();
		 for (Class<? extends PlatformManagedObject> class1 : cls) {
			
		}
		 
		 MBeanServer mBeanServer=ManagementFactory.getPlatformMBeanServer();
		 System.out.println(mBeanServer.getDefaultDomain());
		 
		 ThreadMXBean threadMXBean=ManagementFactory.getThreadMXBean();
		
		 System.out.println(threadMXBean.getCurrentThreadCpuTime());
		 
		 System.out.println(threadMXBean.getThreadCount());
		 System.out.println(threadMXBean.getObjectName());
		 System.out.println(Arrays.toString(threadMXBean.getAllThreadIds()));
		 
		// threadMXBean.dumpAllThreads(true, true);
		 System.out.println(Thread.currentThread().getState().getDeclaringClass());
		 
		 List<GarbageCollectorMXBean>  garbageCollectorMXBeans=ManagementFactory.getGarbageCollectorMXBeans();
		 for (GarbageCollectorMXBean garbageCollectorMXBean2 : garbageCollectorMXBeans) {
			 System.out.println(garbageCollectorMXBean2.getName());
			 System.out.println(Arrays.toString(garbageCollectorMXBean2.getMemoryPoolNames()));
		}
		 
		 MemoryMXBean memoryMXBean=ManagementFactory.getMemoryMXBean();
		 System.out.println(memoryMXBean.getNonHeapMemoryUsage().getUsed());
		 System.out.println(memoryMXBean.getHeapMemoryUsage().getUsed());
		 
		 System.out.println(toDataString(new Date()));
		 
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值