String jmxURL ="service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi";
JMXServiceURL serviceURL = newJMXServiceURL(jmxURL);
Map map = new HashMap();
String[] credentials = new String[] { "monitorRole" , "QED"};
map.put("jmx.remote.credentials",credentials);
JMXConnector connector = JMXConnectorFactory.connect(serviceURL,map);
MBeanServerConnection mbsc =connector.getMBeanServerConnection();
//堆使用率
ObjectName heapObjName = newObjectName("java.lang:type=Memory");
MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName,"HeapMemoryUsage"));
long maxMemory =heapMemoryUsage.getMax();//堆最大
long commitMemory =heapMemoryUsage.getCommitted();//堆当前分配
long usedMemory =heapMemoryUsage.getUsed();
本地:
List<MBeanServer> mBeanServers =MBeanServerFactory.findMBeanServer(null);
ClassLoadingMXBean classBean =ManagementFactory.getClassLoadingMXBean();
MemoryMXBean mxBean =ManagementFactory.getMemoryMXBean();
OperatingSystemMXBean osMBean =ManagementFactory.getOperatingSystemMXBean();
ThreadMXBean thrBean =ManagementFactory.getThreadMXBean();
即直接用 MBeanServerFactory获取MBean.
若使用远程一样的方法,用localhost连接jmx再包装发布在jmx中,会连不上