此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.1.10! |
Spring 的 JMX 产品包括对 JMX 通知的全面支持。
注册通知侦听器
Spring 的 JMX 支持使得向任意数量的 MBean 注册任意数量的 (这包括由
Spring 和 MBeans 通过其他机制注册)。为
例如,考虑这样一种场景:每次目标 MBean 的属性发生更改时,都希望(通过 )收到通知。以下
示例将通知写入控制台:NotificationListeners
MBeanExporter
Notification
package com.example;
import javax.management.AttributeChangeNotification;
import javax.management.Notification;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
public class ConsoleLoggingNotificationListener
implements NotificationListener, NotificationFilter {
public void handleNotification(Notification notification, Object handback) {
System.out.println(notification);
System.out.println(handback);
}
public boolean isNotificationEnabled(Notification notification) {
return AttributeChangeNotification.class.isAssignableFrom(notification.getClass());
}
}
以下示例添加(在前面定义的
示例) 更改为:ConsoleLoggingNotificationListener
notificationListenerMappings
<beans>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=testBean1" value-ref="testBean"/>
</map>
</property>
<property name="notificationListenerMappings">
<map>
<entry key="bean:name=testBean1">
<bean class="com.example.ConsoleLoggingNotificationListener"/>
</entry>
</map>
</property>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
</beans>
使用上述配置后,每次从 JMX 广播时
目标 MBean ()、Bean
通过属性注册为侦听器的是
通知。然后,Bean 可以执行任何操作
它认为对 .Notification
bean:name=testBean1
ConsoleLoggingNotificationListener
notificationListenerMappings
ConsoleLoggingNotificationListener
Notification
您还可以使用直 Bean 名称作为导出的 Bean 和侦听器之间的链接, 如以下示例所示:
<beans>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=testBean1" value-ref="testBean"/>
</map>
</property>
<property name="notificationListenerMappings">
<map>
<entry key="testBean">
<bean class="com.example.ConsoleLoggingNotificationListener"/>
</entry>
</map>
</property>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
</beans>
如果要为所有 Bean 注册单个实例
封存的导出,可以使用特殊的通配符()
作为属性中条目的键
map,如以下示例所示:NotificationListener
MBeanExporter
*
notificationListenerMappings
<property name="notificationListenerMappings">
<map>
<entry key="*">
<bean class="com.example.ConsoleLoggingNotificationListener"/>
</entry>
</map>
</property>
如果您需要做相反的事情(即,注册多个不同的侦听器
MBean),则必须改用 list 属性(在
对物业的偏好)。这一次,而不是
为单个 MBean 配置 a,我们配置实例。A 封装 a 和它要成为的 (或)
在 .还封装了
许多其他属性,例如 A 和任意回传
可在高级 JMX 通知方案中使用的对象。notificationListeners
notificationListenerMappings
NotificationListener
NotificationListenerBean
NotificationListenerBean
NotificationListener
ObjectName
ObjectNames
MBeanServer
NotificationListenerBean
NotificationFilter
使用实例时的配置并不疯狂
与前面介绍的内容不同,如以下示例所示:NotificationListenerBean
<beans>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=testBean1" value-ref="testBean"/>
</map>
</property>
<property name="notificationListeners">
<list>
<bean class="org.springframework.jmx.export.NotificationListenerBean">
<constructor-arg>
<bean class="com.example.ConsoleLoggingNotificationListener"/>
</constructor-arg>
<property name="mappedObjectNames">
<list>
<value>bean:name=testBean1</value>
</list>
</property>
</bean>
</list>
</property>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
</beans>
前面的示例等同于第一个通知示例。那么,假设
我们希望每次提出 A 时都得到一个交还对象,并且
我们还希望通过提供 .以下示例实现以下目标:Notification
Notifications
NotificationFilter
<beans>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=testBean1" value-ref="testBean1"/>
<entry key="bean:name=testBean2" value-ref="testBean2"/>
</map>
</property>
<property name="notificationListeners">
<list>
<bean class="org.springframework.jmx.export.NotificationListenerBean">
<constructor-arg ref="customerNotificationListener"/>
<property name="mappedObjectNames">
<list>
<!-- handles notifications from two distinct MBeans -->
<value>bean:name=testBean1</value>
<value>bean:name=testBean2</value>
</list>
</property>
<property name="handback">
<bean class="java.lang.String">
<constructor-arg value="This could be anything..."/>
</bean>
</property>
<property name="notificationFilter" ref="customerNotificationListener"/>
</bean>
</list>
</property>
</bean>
<!-- implements both the NotificationListener and NotificationFilter interfaces -->
<bean id="customerNotificationListener" class="com.example.ConsoleLoggingNotificationListener"/>
<bean id="testBean1" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
<bean id="testBean2" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="ANOTHER TEST"/>
<property name="age" value="200"/>
</bean>
</beans>
(有关什么是交还对象的完整讨论,以及
确实,这是什么,请参阅 JMX 的部分
标题为“JMX 通知模型”的规范 (1.2)。NotificationFilter
发布通知
Spring 不仅支持注册接收,还支持
用于发布。Notifications
Notifications
本节实际上只与具有 Spring 管理的 Bean 相关
通过 .任何现有的用户定义的 MBean 都应
使用标准 JMX API 进行通知发布。MBeanExporter |
Spring 的 JMX 通知发布支持中的关键接口是接口(在包中定义)。任何将要
通过实例导出为 MBean 可以实现相关接口来获取对实例的访问权限。该接口通过一个简单的 setter 方法向实现 Bean 提供 a 的实例,
然后 Bean 可以使用它来发布 .NotificationPublisher
org.springframework.jmx.export.notification
MBeanExporter
NotificationPublisherAware
NotificationPublisher
NotificationPublisherAware
NotificationPublisher
Notifications
如 NotificationPublisher
接口的 javadoc 中所述,通过该机制发布事件的托管 Bean 不负责通知侦听器的状态管理。
Spring 的 JMX 支持负责处理所有 JMX 基础架构问题。
作为应用程序开发人员,您需要做的就是实现接口并使用
提供的实例。请注意,在向 .NotificationPublisher
NotificationPublisherAware
NotificationPublisher
NotificationPublisher
MBeanServer
使用实例非常简单。创建一个 JMX 实例(或相应子类的实例),
使用与要发生的事件相关的数据填充通知
published,并调用 on 实例,传入 .NotificationPublisher
Notification
Notification
sendNotification(Notification)
NotificationPublisher
Notification
在以下示例中,每次调用操作时导出的 publish a 实例:JmxTestBean
NotificationEvent
add(int, int)
package org.springframework.jmx;
import org.springframework.jmx.export.notification.NotificationPublisherAware;
import org.springframework.jmx.export.notification.NotificationPublisher;
import javax.management.Notification;
public class JmxTestBean implements IJmxTestBean, NotificationPublisherAware {
private String name;
private int age;
private boolean isSuperman;
private NotificationPublisher publisher;
// other getters and setters omitted for clarity
public int add(int x, int y) {
int answer = x + y;
this.publisher.sendNotification(new Notification("add", this, 0));
return answer;
}
public void dontExposeMe() {
throw new RuntimeException();
}
public void setNotificationPublisher(NotificationPublisher notificationPublisher) {
this.publisher = notificationPublisher;
}
}
让这一切正常工作的界面和机制是其中之一
Spring 的 JMX 支持的更好功能。然而,它确实带有
将您的类与 Spring 和 JMX 耦合。与往常一样,这里的建议是
务实。如果您需要 和
您可以接受与 Spring 和 JMX 的耦合,然后这样做。NotificationPublisher
NotificationPublisher
本节实际上只与具有 Spring 管理的 Bean 相关
通过 .任何现有的用户定义的 MBean 都应
使用标准 JMX API 进行通知发布。MBeanExporter |