jmx监控笔记

    要求对服务器的heap内存进行监控,下面是可用的代码。如果不能连接jmx端口,检查hostname是否映射为外网IP,或者看我之前关于jconsole一篇笔记。

 

 

 
  

public class Test {

public static void main(String[] args) throws Exception { JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://119.110.120.114:8998/jmxrmi"); JMXConnector jmxc = JMXConnectorFactory.connect(url, null); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); ObjectName mbeanName = new ObjectName("java.lang:type=Memory"); System.out.println("Name = " + mbsc.getAttribute(mbeanName, "HeapMemoryUsage"));// 取值 CompositeDataSupport cds = (CompositeDataSupport)mbsc.getAttribute(mbeanName, "HeapMemoryUsage"); MemoryUsage heapMemoryUsage = MemoryUsage.from(cds); System.out.println(heapMemoryUsage.getUsed()); System.out.println(heapMemoryUsage.getInit()); System.out.println(heapMemoryUsage.getMax()); System.out.println(heapMemoryUsage.getCommitted()); jmxc.close(); } }

   不自己实现代码更好的方式,http://jmxquery.googlecode.com/files/jmxquery-1.0.zip,这个jar对jmx查询进行了封装,可以配合nagios进行告警。

 

--heipark

 

 

 

 

 

 

你可能感兴趣的:(Java)