jmx 学习

最近有个需求,需要做运行监控,想到了JMX,以前没用过也没怎么了解,这次查了查
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值