`
zhangwei_david
  • 浏览: 474780 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

JMX之将Spring Bean 输出为带通知的MBean

    博客分类:
  • JMX
 
阅读更多

 

Spring集成JMX是很简单的,这里通过注解发方式暴露JMX,有的时序我们需要监听JMX属性的改变,下面我们在Spring配置文件中配置监听器。

 

涉及到三个重要的annotation:@ManagedResource @ManagedAttribute 和 @ManagedOperation。

用途 Commons Attributes属性 JDK 5.0注解 属性/注解类型

将类的所有实例标识为JMX受控资源 ManagedResource @ManagedResource Class 类
将方法标识为JMX操作 ManagedOperation @ManagedOperation Method方法
将getter或者setter标识为部分JMX属性 ManagedAttribute @ManagedAttribute Method (only getters and setters) 方法(仅getters和setters)
定义操作参数说明 ManagedOperationParameter @ManagedOperationParameter@ManagedOperationParameters Method 方法

 

Parameter Description Applies to
ObjectName Used by MetadataNamingStrategy to determine the ObjectName of a managed resource ManagedResource
description Sets the friendly description of the resource, attribute or operation ManagedResource, ManagedAttribute, ManagedOperation, ManagedOperationParameter
currencyTimeLimit Sets the value of the currencyTimeLimit descriptor field ManagedResource, ManagedAttribute
defaultValue Sets the value of the defaultValue descriptor field ManagedAttribute
log Sets the value of the log descriptor field ManagedResource
logFile Sets the value of the logFile descriptor field ManagedResource
persistPolicy Sets the value of the persistPolicy descriptor field ManagedResource
persistPeriod Sets the value of the persistPeriod descriptor field ManagedResource
persistLocation Sets the value of the persistLocation descriptor field ManagedResource
persistName Sets the value of the persistName descriptor field ManagedResource
name Sets the display name of an operation parameter ManagedOperationParameter
index Sets the index of an operation parameter ManagedOperationParameter

 AttributeChangeNotification,这个类是javax.management.Notification的子类,而javax.management.Notification

这个类又是java.util.EventObject的子类,由此可以证实上边所说的,JMX通知机制使用了观察者设计模式.

 

javax.management.Notification是一个JMX的通知核心类,将来需要扩展或者其他JMX自带的消息,均集成自此类.

 

AttributeChangeNotification根据类名可知,是一个属性改变的通知,造方法参数如下:

 

 

Object source,                 // 事件源,一直传递到java.util.EventObject的source

long sequenceNumber,   // 通知序号,标识每次通知的计数器

long timeStamp,              // 通知发出的时间戳 

String msg,                     // 通知发送的message

String attributeName,     // 被修改属性名

String attributeType,      // 被修改属性类型

Object oldValue,             // 被修改属性修改以前的值

Object newValue            // 被修改属性修改以后的值

 

 

根据观察者模式,由事件与广播组成,所以这里继承了NotificationBroadcasterSupport,来提供广播机制,

 

调用NotificationBroadcasterSupportr的sendNotification(notification) 发送广播,广播会根据注册的观察者

 

来对观察者进行逐一通知.

/**
 *
 * @author Lenovo
 * @version $Id: HelloMBean.java, v 0.1 2014年9月26日 下午4:28:17 Lenovo Exp $
 */
@Component
@ManagedResource(description = "hello demo", objectName = "bean:name=helloTest")
public class Hello extends NotificationBroadcasterSupport implements HelloMBean {

    private final String     name               = "Reginald";

    private int              cacheSize          = DEFAULT_CACHE_SIZE;

    private static final int DEFAULT_CACHE_SIZE = 200;

    private long             sequenceNumber     = 1;

    /**
     * @see com.cathy.demo.jmx.notifications.HiMBean#sayHello()
     */
    @ManagedOperation(description = "say hello")
    public void sayHello() {
        System.out.println("Hello,Word");
    }

    /**
     * @see com.cathy.demo.jmx.notifications.HiMBean#add(int, int)
     */
    @ManagedOperation(description = "add")
    @ManagedOperationParameters({
        @ManagedOperationParameter(name = "x", description = "fist param"),
        @ManagedOperationParameter(name = "y", description = "second param") })
    public int add(int x, int y) {
        return x + y;
    }

    /**
     * 获取 name 属性,通过gatter 方法获取私有的成员属性,在这个例子中属性值是永远不会改变的;但是对于其他的属性可能在程序的运行期间进行
     * 改变。然而一些属性代表统计数据,如,如运行时间,内存使用情况等是只读到 也就是不能通过接口改变的
     * @see com.cathy.demo.jmx.notifications.HiMBean#getName()
     */
    @ManagedAttribute
    public String getName() {
        return name;
    }

    /**
     * 获取私有成员变量cacheSize 的值
     * @see com.cathy.demo.jmx.notifications.HiMBean#getCacheSize()
     */
    @ManagedAttribute
    public int getCacheSize() {
        return cacheSize;
    }

    /**
     * 设置私有成员变量cacheSize 的值
     * @see com.cathy.demo.jmx.notifications.HiMBean#setCacheSize(int)
     */
    @ManagedAttribute
    public void setCacheSize(int size) {
        int oldSize = cacheSize;
        System.out.println("Cache size now is " + oldSize);
        /**
         * 构建一个介绍属性改变的通知,
         */
        Notification notification = new AttributeChangeNotification(this, sequenceNumber++,
            System.currentTimeMillis(), "CacheSize changed", "cacheSize", "int", oldSize, size);

        /**
         * 发送通知
         */
        sendNotification(notification);
        cacheSize = size;
    }

    /**
     * @see javax.management.NotificationBroadcasterSupport#getNotificationInfo()
     */
    @Override
    public MBeanNotificationInfo[] getNotificationInfo() {
        String[] types = new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE };
        String name = AttributeChangeNotification.class.getName();
        String description = "An attribute of this MBean has changed";
        MBeanNotificationInfo info = new MBeanNotificationInfo(types, name, description);
        return new MBeanNotificationInfo[] { info };
    }

}

通知监听器:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.0.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/jee 
		http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
		http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-3.1.xsd  
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
		">
	<aop:aspectj-autoproxy />

	<context:annotation-config />
	<context:component-scan base-package="com.cathy.demo.jmx.*" />
	<bean id="jmxAttributeSource"
		class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />

	<bean id="assembler"
		class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
		<property name="attributeSource" ref="jmxAttributeSource" />
	</bean>
	<!-- ObjectName命名策略 -->
	<bean id="namingStrategy"
		class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
		<property name="attributeSource" ref="jmxAttributeSource" />
	</bean>
	<bean id="mbeanExporter" class="org.springframework.jmx.export.MBeanExporter">
		<!-- 指定messageInfo装配类 -->
		<property name="assembler" ref="assembler" />
		<!-- 指定ObjectName命名策略 -->
		<property name="namingStrategy" ref="namingStrategy" />
		<!-- 配置自动检测MBean -->
		<property name="autodetect" value="true" />
		<property name="notificationListenerMappings">
			<map>
				<entry key="*">
					<bean class="com.cathy.demo.jmx.listener.ConfigNotificationListener" />
				</entry>
			</map>
		</property>

	</bean>
	<bean id="fileReplicator" class="com.cathy.demo.jmx.annotation.FileReplicatorImpl" />

</beans>

 

 

/**
 *
 * @author zhangwei_david
 * @version $Id: ConfigNotificationListener.java, v 0.1 2015年6月19日 下午4:37:09 zhangwei_david Exp $
 */

public class ConfigNotificationListener implements NotificationListener, NotificationFilter {

    /**  */
    private static final long serialVersionUID = 1099818764372891903L;

    /**
     * @see javax.management.NotificationListener#handleNotification(javax.management.Notification, java.lang.Object)
     */
    public void handleNotification(Notification notification, Object handback) {
        log("SequenceNumber:" + notification.getSequenceNumber());
        log("Type:" + notification.getType());
        log("Message:" + notification.getMessage());
        log("Source:" + notification.getSource());
        log("TimeStamp:" + notification.getTimeStamp());
    }

    private void log(String message) {
        System.out.println(message);
    }

    /**
     * @see javax.management.NotificationFilter#isNotificationEnabled(javax.management.Notification)
     */
    public boolean isNotificationEnabled(Notification notification) {
        return true;
    }
}

 通过Jconsole控制台可以看到如下信息:



 修改cacheSize 在eclipse的控制台可以看到如下日志:

 

 

log4j:WARN custom level class [# 输出DEBUG级别以上的日志] not found.
2015-06-21 13:42:55  [ main:0 ] - [ INFO ]  @TestExecutionListeners is not present for class [class com.cathy.demo.jmx.AutoDetectJmxTest]: using defaults.
2015-06-21 13:42:56  [ main:137 ] - [ INFO ]  Loading XML bean definitions from URL [file:/H:/Alipay.com/workspace4alipay/demo/target/classes/META-INF/spring/jmx-annotation-beans.xml]
2015-06-21 13:42:56  [ main:369 ] - [ INFO ]  JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
2015-06-21 13:42:56  [ main:421 ] - [ INFO ]  Refreshing org.springframework.context.support.GenericApplicationContext@15f7ae5: startup date [Sun Jun 21 13:42:56 CST 2015]; root of context hierarchy
2015-06-21 13:42:56  [ main:580 ] - [ INFO ]  Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@40cd94: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,hello,jmxAttributeSource,assembler,namingStrategy,mbeanExporter,fileReplicator,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
2015-06-21 13:42:56  [ main:693 ] - [ INFO ]  Registering beans for JMX exposure on startup
2015-06-21 13:42:56  [ main:695 ] - [ INFO ]  Bean with name 'hello' has been autodetected for JMX exposure
2015-06-21 13:42:56  [ main:705 ] - [ INFO ]  Bean with name 'fileReplicator' has been autodetected for JMX exposure
2015-06-21 13:42:56  [ main:707 ] - [ INFO ]  Located MBean 'hello': registering with JMX server as MBean [bean:name=helloTest]
2015-06-21 13:42:56  [ main:709 ] - [ INFO ]  Located managed bean 'fileReplicator': registering with JMX server as MBean [com.cathy.demo.jmx.annotation:name=fileReplicator,type=FileReplicatorImpl]
Cache size now is 200
SequenceNumber:1
Type:jmx.attribute.change
Message:CacheSize changed
Source:bean:name=helloTest
TimeStamp:1434865528473

 

  • 大小: 32.8 KB
3
0
分享到:
评论

相关推荐

    JMX与Spring 结合

    Spring 提供了`PlatformMBeanServer`的自动配置,可以方便地将Spring Bean暴露为JMX资源。例如,你可以通过`@ManagedResource`注解标记一个类,然后使用`@ManagedAttribute`和`@ManagedOperation`来定义可管理和操作...

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

    Spring通过`MBeanServer`接口和`MBeanExporter`类,允许开发者将Spring Bean自动转换为MBeans。 在Spring中配置JMX整合通常涉及以下几个步骤: 1. **启用JMX支持**:在Spring配置文件中,我们需要添加`...

    基于Spring+JMX+Tomcat实现资源动态管理

    3. 使用Spring的JMX支持:Spring提供了`PlatformMBeanServerBeanPostProcessor`,它可以自动检测带有MBean注解的bean并将其注册到MBean服务器。 4. 连接和管理:通过JConsole或其他JMX客户端工具,如VisualVM或JMC...

    spring框架下jmx入门例子

    本教程将引导你入门Spring框架下的JMX应用。 首先,JMX是一种Java标准,它允许开发者创建可管理的Java组件,并提供了一种统一的方式来管理和监控这些组件。在Spring中,我们可以利用JMX来创建MBeans(Managed Beans...

    JMX SPRING

    Spring通过`MBeanExporter`类将Spring Bean注册到MBean服务器,这些Bean可以是标准的JMX MBean,也可以是自动转换的Spring Bean。此外,Spring还支持自定义的MBean信息,如MBean的名称、描述、属性和操作。 **JMX的...

    lanlan2017#JavaReadingNotes#18.0 第18章 使用JMX监控Spring1

    第18章 使用JMX监控Spring本章内容:使用Actuator端点的MBean将Spring bean暴露为MBean发布通知JMX(Java Manage

    spring jmx

    Spring JMX的核心概念包括MBean(Managed Bean)、MBeanServer和代理(MBeanProxy)。MBean是JMX中的基本管理单元,代表一个可管理的资源,可以是一个对象、服务或任何其他需要管理的组件。MBeanServer则是管理这些...

    JMX(一)-------MBean server

    在JMX中,MBean(Managed Bean)是核心概念,它是具有特定管理接口的对象,可以代表任何可管理的资源。 **MBean Server详解** MBean Server是JMX架构的核心,它负责注册、管理和访问MBean。MBean Server就像一个...

    jmx开发例子,包括与spring结合例子

    这个压缩包中的例子很可能是创建了一个简单的Spring应用,并且配置了JMX支持,展示如何将Spring的bean暴露为MBeans进行管理和监控。通过研究这些示例,新人可以更好地理解JMX和Spring如何协同工作,提升他们的Java...

    mx4j管理jmx的jar与spring结合使用

    将MX4J与Spring结合,可以充分利用JMX的功能,并借助Spring的便利性来管理和监控应用。 在标题“mx4j管理jmx的jar与spring结合使用”中,我们关注的重点是MX4J如何与Spring协作来实现JMX的功能。MX4J提供的`mx4j-...

    书籍JMX-IN-ACTION

    书中首先介绍了JMX的基本概念,包括MBean(管理bean)、MBean服务器、代理和通知等核心元素,以及它们在JMX架构中的作用。MBean是JMX的核心,它代表了可以被管理的资源或服务,而MBean服务器则作为运行这些MBean的...

    JMX小例子以及介绍

    - **Notification**: JMX允许MBean发送通知,这使得其他MBeans或管理工具可以订阅并接收有关系统状态变化的信息。 - **JMX连接器**: 连接器是JMX与外部世界交互的桥梁,它们允许管理应用程序通过JMX API连接到MBean...

    Spring in Action(第二版 中文高清版).part2

    12.4.1 将Spring Bean输出为MBean 12.4.2 远程访问MBean 12.4.3 处理通知 12.5 小结 第三部分 Spring客户端 第13章 处理Web请求 13.1 开始Spring MVC之旅 13.1.1 请求生命中的一天 13.1.2 配置...

    jmx所需的jar

    MBean(Managed Bean)是JMX中的核心概念,它代表了管理的对象,可以是系统的一个组件、服务或者任何需要被管理的资源。 2. `jmx_remote-1_0_1_03-ri.zip`: 这个文件包含了JMX的远程访问支持。JMX远程接口允许管理...

    Spring in Action(第2版)中文版

    12.4.1将springbean输出为mbean 12.4.2远程访问mbean 12.4.3处理通知 12.5小结 第三部分spring客户端 第13章处理web请求 13.1开始springmvc之旅 13.1.1请求生命中的一天 13.1.2配置dispatcherservlet ...

    Spring in Action(第二版 中文高清版).part1

    12.4.1 将Spring Bean输出为MBean 12.4.2 远程访问MBean 12.4.3 处理通知 12.5 小结 第三部分 Spring客户端 第13章 处理Web请求 13.1 开始Spring MVC之旅 13.1.1 请求生命中的一天 13.1.2 配置...

    java of core jmx

    7. **JMX与Spring的集成**:Spring框架提供了与JMX的集成,可以方便地将Spring Bean注册为MBeans,简化了JMX的使用。 8. **安全性**:JMX支持安全管理,可以通过设置权限来控制哪些客户端可以访问特定的MBeans,...

    JMX.rar_jmx_osgi

    Spring的`@ManagedResource`注解可以自动将bean暴露为MBean,而`@ManagedAttribute`和`@ManagedOperation`可以分别标记可管理的属性和操作。 9. **JMX与OSGi的集成**:在OSGi环境中,可以通过实现特定的服务接口...

    Spring-Reference_zh_CN(Spring中文参考手册)

    1. 简介 1.1. 概览 1.2. 使用场景 2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 ... 将 Spring Beans 注入到 Tapestry ...

Global site tag (gtag.js) - Google Analytics