[探究JMX] 4、Spring2.x集成MX4J

一、简介

      Spring的JMX支持提供了一些特性,让你能透明地将Spring应用程序集成到JMX基础实施中去。

确切地说,Spring的JMX支持提供了四种核心特性:

  • 将任意Spring Bean自动注册为JMX MBean
  • 灵活操纵Bean管理接口的机制
  • 通过远程JSR-160连接器对MBean的声明式暴露
  • 对本地和远程MBean资源的简单代理

二、准备工作

      1、 引入Spring2.5所需开发包

      2、 引入mx4j-tools.jar包

      3、 开发环境MyEclipse8.0

 

三、代码实例

	<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

     <!-- MBeanExporter -->  
     <bean id="exporter"  
         class="org.springframework.jmx.export.MBeanExporter"  
         depends-on="mbeanServer">  
         <property name="beans">  
             <map>  
               <entry key="LuisFigo:name=config"  
                     value-ref="config" />  
                 <entry key="MX4J:name=HttpAdaptor"  
                     value-ref="httpAdaptor" />  
             </map>  
         </property>  
         <property name="server" ref="mbeanServer" />  
         <property name="assembler">  
             <bean id="assembler"  
                 class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">  
                 <property name="attributeSource">  
                     <bean id="attributeSource"  
                         class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />  
                 </property>  
           </bean>  
        </property>  
   </bean>  
    <!-- MBeanServerFactoryBean -->  
     <bean id="mbeanServer"  
         class="org.springframework.jmx.support.MBeanServerFactoryBean">  
     </bean>  
   
     <!-- HttpAdaptor & XSLTProcessor -->  
     <bean id="httpAdaptor"  
         class="mx4j.tools.adaptor.http.HttpAdaptor">  
         <property name="processor">  
             <!-- XSLTProcessor -->  
             <bean id="xsltProcessor"  
                 class="mx4j.tools.adaptor.http.XSLTProcessor" />  
         </property>  
         <property name="port" value="8000" />  
     </bean>  
     <bean id="config"  
         class="com.muyu.jmx.Config">  
     </bean>  
</beans>  

 

      Spring的JMX框架的核心类是MBeanExporter,这个类负责获取Spring Bean,然后将其注册到一个JMX MBeanServer上,要将一个Bean中的属性和方法暴露成为一个JMX MBean中的属性和操作,你只要在配置文件中简单的配置MBeanExporter一个实例,并根据上面的配置文件中的配置方法将bean传入即可。 Assembler用于管理bean暴露的接口,Spring JMX提供了一套全面的以及可扩展的机制来控制bean的管理接口。我们这个例子使用MetadataMBeanInfoAssembler,能够用源码级元数据给你的Bean定义管理接口。AnnotationJmxAttributeSource说明我们用注解的方式来管理接口。

 

 

package com.muyu.jmx;

import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(description = "config")
public class Config {

    private String configLocation;
    
    @ManagedAttribute(description = "configLocation 对应的值")
    public String getConfigLocation() {
        return configLocation;
    }

    @ManagedOperation(description = "控制台输出configLocation信息")
    public void printConfigLocation() {
        System.out.println(configLocation);
    }

    @ManagedOperation(description = "控制台输出i_ConfigLocation信息")
    @ManagedOperationParameters(@ManagedOperationParameter(name="i_ConfigLocation", description="第一个参数"))
    public void printConfigLocation(String i_ConfigLocation) {
        System.out.println(i_ConfigLocation);
    }
    
    @ManagedAttribute(description = "赋值给configLocation")
    public void setConfigLocation(String i_ConfigLocation) {
        this.configLocation = i_ConfigLocation;
    }

}

 

package com.muyu.jmx;

import java.io.IOException;
import javax.management.MalformedObjectNameException;
import mx4j.tools.adaptor.http.HttpAdaptor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JMXTest {
	public static void main(String[] args) throws IOException,
			MalformedObjectNameException, Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml"});
		HttpAdaptor httpAdaptor = (HttpAdaptor) ctx.getBean("httpAdaptor");
		httpAdaptor.start();
	}
}

 

说明:

 

       applicationContext.xml配置文件中已经设置httpAdaptor端口号为8000,运行JMXTest后,通过web浏览器登录http://localhost:8000 图1所示,点击LuisFigo:name=config进入图2页面,就可以访问或者通过暴露的接口操作config了。

 

四、总结

 

      本节只是介绍了Spring怎样集成MX4J来管理注册的bean(当然这里的bean只是普通的Spring bean,Spring框架采用dynamicMBean来实现JMX),Spring还可以通过JSR-160连接器来构建服务器端和客户端的连接。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值