`
longgangbai
  • 浏览: 7252254 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring JMX的总结学习(二) 注解实现MBean

阅读更多

    本文采用Spring JMX 和MX4J开发的JMX,

备注必须的类库为:ms4j-tools-3.0.1.jar,mx4j-3.0.2.jar   

  具体要实现JMX MBean的 注解类:

package com.easyway.jboss.jmx.spring.service;

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;
/**
 * 采用Spring的注解方式实现JMX的开发
 * @author longgangbai
*正如你所见,除了元数据定义的语法外只有很少的变动。这个方法在后台启动时会有一点缓慢,
*因为要在类中使用Commons Attributes转换JDK5.0的注解。不过,这仅仅是一次性的开销并
*且JDK5.0的注解为你带来编译时检查的好处。 
*
 */
@ManagedResource
public class JMXAnonationMBeanManager {
	
	private int clientStatus;
	@ManagedOperation(description = "pause a single proccess")   
    @ManagedOperationParameters( { @ManagedOperationParameter(name = "Name of proccess instance", description = "Mandatory") })   
    public void pause(String n) {   
        System.out.println("pause");   
    }   
   
    @ManagedOperation(description = "shut down the proccess")   
    public void monitor() {   
        System.out.println("shutting down…");   
    }   
   
    public void publicMessage() {   
        System.out.println("public Message to monitor server");   
    }   
   
    @ManagedAttribute(description = "client status")   
    public int getClientStatus() {   
        return clientStatus;   
    }   
   
    @ManagedAttribute(description = "client status")   
    public void setClientStatus(int clientStatus) {   
        this.clientStatus = clientStatus;   
    }   
}   

 

 

配置信息如下:

<?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">
 <!-- 
        要在管理接口定义中使用JDK5.0的注解,Spring提供了一个注解集镜像到Commons Attribute参数类和一个JMX的实现来支持JmxAttributeSource, 
        AnnotationsJmxAttributeSource并允许MbeanInfoAssembler读取它们。
  -->
 <!-- 创建相关的Bean对象并设置参数 -->
 <bean id="monitorMBeanManager" class="com.easyway.jboss.jmx.spring.service.JMXAnonationMBeanManager"/>
 
 
<!-- 创建一个MBeanServer对象,
  
 -->
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
</bean>

<bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>


<bean id="assembler"  class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
   <property name="attributeSource">
     <ref local="jmxAttributeSource"/>
   </property>
</bean>

<bean id="httpAdaptor" class="mx4j.tools.adaptor.http.HttpAdaptor">
    <property name="processor">
         <bean id="xsltProcessor" class="mx4j.tools.adaptor.http.XSLTProcessor"/>
    </property>
    <property name="port">
       <value>8000</value>
    </property>
</bean>

<!--一个由 MBeanServerFactoryBean 创建的 MBeanServer 实例,它通过属性server提供给了 MBeanExporter。 
当你提供了你自己的 MBeanServer 实例后,MBeanExporter 将使用该实例,且不再查找一个运行中的 MBeanServer。
  设置相关的Bean的暴露为JMX
   关系最大的是 exporter Bean。beans 属性使得 MBeanExporter 知道要将哪个Bean输出到JMX的 MBeanServer 上去。 
   缺省配置中,beans 里的 Map 中的条目的key被用作相应条目值所引用的Bean的 ObjectName。
  -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
      <property name="assembler">
          <ref local="assembler"/>
      </property>
     <property name="beans">
       <map>
          <entry key="bean:name=monitorMBeanManager" value-ref="monitorMBeanManager">
          </entry>
          <entry key="mx4j:name=HttpAdaptor" value-ref="httpAdaptor">
          </entry>
       </map>
    </property>
     <property name="server" ref="mbeanServer"/>
</bean>

</beans>

 

测试代码如下:

package com.easyway.jboss.jmx.spring.service;

import java.io.IOException;

import mx4j.tools.adaptor.http.HttpAdaptor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 采用Mx4j测试JMX
 * @author longgangbai
 *
 */
public class JBossJMXServiceClient {
  public static void main(String[] args) throws IOException {
	  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-jmx-annotation.xml");
	  HttpAdaptor httpAdaptor=(HttpAdaptor)ctx.getBean("httpAdaptor");
	  httpAdaptor.start();
}
}

 

 

在浏览器中输入:

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

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics