首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

JMX (3)-spring整合JMX

2013-07-16 
JMX (三)--------spring整合JMXhttp://90haofang-163-com.iteye.com/blog/1901416http://90haofang-163-co

JMX (三)--------spring整合JMX

http://90haofang-163-com.iteye.com/blog/1901416
http://90haofang-163-com.iteye.com/blog/1902211

JMX的Server和connetor的编写在之前的博客中已经写了。现在谈谈如何在用spring整合JMX,首先看看spring,JMX的优点。

spring提供了很简便的方式整合JMX到应用程序中,它提供了一下四个特性:
1.自动注册Spring bean为JMX MBean
2.灵活的管理beans 接口机制
3.声明MBean通过远程连接
4.给本地或者远程资源提供了简单代理
这些特性使用起来,与应用程序其他模块没有耦合性,这些模块也不需要知道Spring和JMX的存在。接下来通过一个例子来开始我们的整合,

首先了解MBeanExporter,这个类是spring JMX中的核心类,用于创建好的Spring beans 注册到JMX MBean? server上。使之成为MBean。

先创建一个spring Bean 实现一个接口:

?

package git.tudou.spring.integration.jmx;/** * Date: 13-7-11 * Time: 上午10:38 */public interface IJmxTestBean {    public int add(int x,int y);    public long operation();    public int getAge();    public void setAge(int age);    public void setName(String name);    public String getName();}
?实现这个接口:

?

?

package git.tudou.spring.integration.jmx;/** * Date: 13-7-11 * Time: 上午10:43 */public class JmxTestBean implements IJmxTestBean{    private String name;    private int age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public int add(int x, int y) {        return x+y;    }    public long operation() {        return 100;    }    public void dontExportOperation(){        throw  new RuntimeException("do not export me");    }}
把这个类的方法和属性导出为MBean的方法和属性,只需要在spring配置一个MBeanExporter类, 并把这个类配置到MBeanExporter中。

?

?

<!--导出spring bean所有的方法和属性 不使用赖加载--><bean id="exporter" lazy-init="false">    <!--需要导出为MBean的spring 类 -->    <property name="beans">        <map>            <entry key="bean:name=testBean" value-ref="testBean"/>        </map>    </property>    <!--<property name="autodetect" value="true"/>-->    <!--指定export的MBean server -->    <property name="server" ref="mbeanServer"/></bean><bean id="testBean" value="tudou"/>    <property name="age"  value="10"/></bean><bean id="mbeanServer" name="code"><?xml version="1.0" encoding="GBK"?><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.xsd"       default-autowire="byName"><!-- --><!--导出spring bean所有的方法和属性 不使用赖加载--><bean id="exporter" lazy-init="false">    <!--需要导出为MBean的spring 类 -->    <property name="beans">        <map>            <entry key="bean:name=testBean" value-ref="testBean"/>        </map>    </property>    <!--<property name="autodetect" value="true"/>-->    <!--指定export的MBean server -->    <property name="server" ref="mbeanServer"/></bean><bean id="testBean" value="tudou"/>    <property name="age"  value="10"/></bean><!--MBean server 实例--><bean id="mbeanServer" destroy-method="destroy">        <property name="port" value="9992"/></bean><!--配置服务连接connector via rmi --><bean id="serverConnector" depends-on="registry">    <property name="objectName"  value="connector:name=rmi"/>    <property name="serviceUrl" value="service:jmx:rmi:///jndi/rmi://10.12.145.18:9992/jmxrmi"/>    <property name="server" ref="mbeanServer"/></bean></beans>

?这样远程就可以通过rmi访问该MBean了.先编写一个测试类启动MBean server

package git.tudou.spring.jmx.test;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Date: 13-7-11 * Time: 下午2:34 */public class BaseTest {    protected  static ApplicationContext applicationContext;    @BeforeClass    public static  void initApplicationContext() {         try{             long start = System.currentTimeMillis();             System.out.println("正在加载配置文件.....");             applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-jmx-server.xml"});             System.out.println("加载配置文件完成...时间:" + (System.currentTimeMillis() - start));         }catch (Exception e){             e.printStackTrace();         }    }    @Before    public void setAutoWire(){        applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, DefaultListableBeanFactory.AUTOWIRE_BY_NAME,false);    }    @AfterClass    public static void afterClass(){        System.out.println("类加载完成......");    }}

?

package git.tudou.spring.jmx.test;import org.junit.Test;import java.util.concurrent.CountDownLatch;/** * Date: 13-7-11 * Time: 下午2:29 */public class TestSpringJmxServer extends  BaseTest {    @Test    public  void testCreateServer() throws Exception{        CountDownLatch lock = new CountDownLatch(1);        System.out.println("MBean rmi 服务启动....");        lock.await();    }}

?启动测试类:

?

INFO - JMX connector server started: javax.management.remote.rmi.RMIConnectorServer@2b071e12DEBUG - Finished creating instance of bean 'serverConnector'DEBUG - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@209daa17]DEBUG - Returning cached instance of singleton bean 'lifecycleProcessor'加载配置文件完成...时间:5408MBean rmi 服务启动....

?可以使用jconsole连接MBean server先看看MBean,本来想上个截图的,总是传不上去。



JMX (3)-spring整合JMX
?

然后用代码连接MBean,在spring配置文件中,配置JMXConnector,我使用代理方式

spring-config-jmx-client.xml

?

<?xml version="1.0" encoding="GBK"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:osgi="http://www.springframework.org/schema/osgi"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"       default-autowire="byName"><!-- --><bean id="testBean2" value="true"/>    <property name="objectName" value="bean:name=testBean"/>    <property name="proxyInterface" value="git.tudou.spring.integration.jmx.IJmxTestBean"/>    <property name="serviceUrl" value="service:jmx:rmi:///jndi/rmi://10.12.145.18:9992/jmxrmi"/></bean></beans>
?proxyInterface是导出MBean的接口,serverUrl为MBean通过rmi发布是使用的地址,可以通过代理访问MBean,实际上是作为IJmxTestBean的代理,编写测试类测试

?

?

?

package git.tudou.spring.jmx.test;import org.junit.AfterClass;import org.junit.Before;import org.junit.BeforeClass;import org.springframework.beans.factory.support.DefaultListableBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Date: 13-7-11 * Time: 下午4:08 */public class BaseTestClient {    protected  static ApplicationContext applicationContext;    @BeforeClass    public static  void initApplicationContext() {         try{             long start = System.currentTimeMillis();             System.out.println("正在加载client配置文件.....");             applicationContext = new ClassPathXmlApplicationContext(new String[]{"spring-config-jmx-client.xml"});             System.out.println("加载配置文件完成...时间:" + (System.currentTimeMillis() - start));         }catch (Exception e){             e.printStackTrace();         }    }    @Before    public void setAutoWire(){        applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(this, DefaultListableBeanFactory.AUTOWIRE_BY_NAME,false);    }    @AfterClass    public static void afterClass(){        System.out.println("类加载完成......");    }}
?以下类在从spring容器中获得一个代理,并执行MBean的一些方法
package git.tudou.spring.jmx.test;import git.tudou.spring.integration.jmx.IJmxTestBean;import org.junit.Test;import java.util.concurrent.CountDownLatch;/** * User: luochao * Date: 13-7-11 * Time: 下午4:09 */public class TestSpringJmxClient extends  BaseTestClient {    IJmxTestBean proxy;    @Test    public void testClient() {        try{        System.out.println("客户端启动");        CountDownLatch statrt = new CountDownLatch(1);        System.out.println("MBean name:"+proxy.getName());        System.out.println("调用Mbean的方法:"+proxy.add(1,2));        CountDownLatch lock = new CountDownLatch(1);        }catch (Exception e){            System.out.println(e);        }    }    public void setProxy(IJmxTestBean proxy) {        this.proxy = proxy;    }}
?以下是执行结果,从结果中可以看出,调用了MBean方法getName,add方法并输出了相应的结果
DEBUG - Finished creating instance of bean 'proxy'DEBUG - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@690ff62a]DEBUG - Returning cached instance of singleton bean 'lifecycleProcessor'加载配置文件完成...时间:8855DEBUG - Returning cached instance of singleton bean 'proxy'DEBUG - Added autowiring by name from bean name 'git.tudou.spring.jmx.test.TestSpringJmxClient' via property 'proxy' to bean named 'proxy'客户端启动MBean name:tudou调用Mbean的方法:3类加载完成......
??

出现了以上结果,说明整合成功,spring jmx还提供了很多方式去导出bean,还提供了assembler用于过滤掉一部分方法和属性,有基于annotation,基于接口的,而在以上例子中,spring bean中的所有方法和属性都被导出到MBean中。搭建了第一个实例,就可以在这基础上面做一些应用,我们可以编写简单程序监测tomcat,jvm等等。加油土豆。。


?

?

?

?

?

?

?

?

热点排行