此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0spring-doc.cadn.net.cn

通知

Spring 的 JMX 产品包括对 JMX 通知的全面支持。spring-doc.cadn.net.cn

为通知注册侦听器

Spring 的 JMX 支持使得注册任意数量的NotificationListeners具有任意数量的 MBean(这包括由 Spring的MBeanExporter以及通过其他机制注册的 MBean)。为 例如,考虑这样一个场景:您希望被告知(通过Notification) 每次目标 MBean 的属性发生更改时。以下内容 示例将通知写入控制台:spring-doc.cadn.net.cn

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(在前面的 example) 更改为notificationListenerMappings:spring-doc.cadn.net.cn

<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>

完成上述配置后,每次使用 JMXNotification广播自 目标 MBean (bean:name=testBean1)、ConsoleLoggingNotificationListener豆 通过notificationListenerMappingsproperty 为 通知。这ConsoleLoggingNotificationListener然后 Bean 可以执行任何作 它认为响应Notification.spring-doc.cadn.net.cn

你也可以使用直接的 bean 名称作为导出的 bean 和侦听器之间的链接。 如下例所示:spring-doc.cadn.net.cn

<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>

如果要注册单个NotificationListenerinstance 的所有 bean 封闭的MBeanExporterexports,您可以使用特殊的通配符 () 作为*notificationListenerMappings财产 map,如下例所示:spring-doc.cadn.net.cn

<property name="notificationListenerMappings">
	<map>
		<entry key="*">
			<bean class="com.example.ConsoleLoggingNotificationListener"/>
		</entry>
	</map>
</property>

如果需要执行相反的作(即,针对 一个 MBean),则必须改用notificationListenerslist 属性(在 preference 设置为notificationListenerMappings属性)。这一次,不是 配置NotificationListener对于单个 MBean,我们配置NotificationListenerBean实例。一个NotificationListenerBean封装一个NotificationListenerObjectName(或ObjectNames) 在MBeanServer.这NotificationListenerBean还封装 许多其他属性,例如NotificationFilter和任意的交还 对象。spring-doc.cadn.net.cn

使用NotificationListenerBeaninstances 不是 wildly 与前面介绍的内容不同,如下例所示:spring-doc.cadn.net.cn

<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>

前面的示例等效于第一个通知示例。那么,假设 我们希望每次Notification被提高,并且 我们还希望过滤掉无关的Notifications通过提供NotificationFilter.以下示例可实现这些目标:spring-doc.cadn.net.cn

<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>

(有关什么是 handback 对象的完整讨论,以及 确实,多么棒的NotificationFilter是,请参阅 JMX 的 规范 (1.2) 标题为“JMX 通知模型”。spring-doc.cadn.net.cn

发布通知

Spring 不仅支持注册接收Notifications而且还 用于出版Notifications.spring-doc.cadn.net.cn

本节实际上仅与 Spring 管理的 bean 相关,这些 bean 具有 通过MBeanExporter.任何现有的用户定义的 MBean 都应该 使用标准 JMX API 发布通知。

Spring 的 JMX 通知发布支持中的关键接口是NotificationPublisher接口(在org.springframework.jmx.export.notification包)。任何将要 通过MBeanExporterinstance 可以实现相关的NotificationPublisherAware接口访问NotificationPublisher实例。这NotificationPublisherAwareinterface 提供NotificationPublisher通过简单的 setter 方法实现 bean, 然后 Bean 可以使用该 bean 来发布Notifications.spring-doc.cadn.net.cn

NotificationPublisher接口中,通过NotificationPublisher机制不负责通知监听器的状态管理。 Spring 的 JMX 支持负责处理所有 JMX 基础结构问题。 作为应用程序开发人员,您需要做的就是实现NotificationPublisherAware界面,并使用 提供NotificationPublisher实例。请注意,NotificationPublisher在托管 Bean 注册到MBeanServer.spring-doc.cadn.net.cn

使用NotificationPublisher实例非常简单。您创建一个 JMXNotification实例(或适当的Notificationsubclass)、 使用与要发生的事件相关的数据填充通知 发布,并调用sendNotification(Notification)NotificationPublisher实例中,传入Notification.spring-doc.cadn.net.cn

在以下示例中,导出的JmxTestBean发布NotificationEvent每次add(int, int)作:spring-doc.cadn.net.cn

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;
	}

}

NotificationPublisher界面和让它正常工作的机制是其中之一 Spring 的 JMX 支持的更好功能。然而,它的价格标签确实是 将你的类耦合到 Spring 和 JMX。一如既往,这里的建议是 务实。如果您需要NotificationPublisher和 你可以接受到 Spring 和 JMX 的耦合,然后这样做。spring-doc.cadn.net.cn