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()));
}
}
java获取pid和management包
最新推荐文章于 2024-07-08 02:58:35 发布