最近有个需求,需要做运行监控,想到了JMX,以前没用过也没怎么了解,这次查了查
jmx是什么?
jmx是java下的资源管理规范。泛指的资源可以代表任何对象和事务,如网络,设备,应用程序。如果是java当然是监控javabean了。
由于编写JMX代码比较麻烦,我借助spring快速实现了一下,如下,
启动程序,登录到http://localhost:9092
可以看到受管理的MBEAN:
我动态可以改变hello 的属性,在程序里打印出来
name:=szn
name:=szn
name:=szn
name:=szn
name:=szn
name:=wl
name:=wl
jmx是什么?
jmx是java下的资源管理规范。泛指的资源可以代表任何对象和事务,如网络,设备,应用程序。如果是java当然是监控javabean了。
由于编写JMX代码比较麻烦,我借助spring快速实现了一下,如下,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="jmxMBeanExport" class="org.springframework.jmx.export.MBeanExporter">
<property name="server">
<bean class="javax.management.MBeanServerFactory" factory-method="createMBeanServer"/>
</property>
<property name="beans">
<map>
<entry key="MyAgent:name=htmladapter,port=9092">
<bean class="com.sun.jdmk.comm.HtmlAdaptorServer" init-method="start">
<property name="port">
<value>9092</value>
</property>
</bean>
</entry>
<entry key="MyAgengt:name=hello">
<ref bean="hello"></ref>
</entry>
</map>
</property>
</bean>
<bean id="hello" class="test.Hello"></bean>
</beans>
package test;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
/**
* <p>[接口/类的功能描述] <br>
* <br>
* Created by [William] on 2011-7-27 <br>
* Revised by [修改人] on [修改日期] for [修改说明]<br>
* </p>
*/
public class JmxTest {
/**
* [方法功能说明]
* @param args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("jmx.xml");
Hello hello = (Hello)context.getBean("hello");
hello.setName("szn");
while(true){
try {
Thread.sleep(1000);
System.out.println("name:="+hello.getName());
} catch (InterruptedException e) {
// TODO
}
}
}
}
启动程序,登录到http://localhost:9092
可以看到受管理的MBEAN:
我动态可以改变hello 的属性,在程序里打印出来
name:=szn
name:=szn
name:=szn
name:=szn
name:=szn
name:=wl
name:=wl