通过在Java接口上反射确定其管理接口的MBean,并发出通知。
以下示例显示如何使用公共构造函数StandardEmitterMBean(implementation, mbeanInterface, emitter)创建一个发出通知的MBean,其中任何实现类名为Impl,管理接口由任何接口Intf定义(与当前标准MBean一样),并且使用接口NotificationEmitter的任何实现。 该示例使用类NotificationBroadcasterSupport作为接口NotificationEmitter的实现。
MBeanServer mbs;
...
final String[] types = new String[] {"sun.disc.space","sun.disc.alarm"};
final MBeanNotificationInfo info = new MBeanNotificationInfo(
types,
Notification.class.getName(),
"Notification about disc info.");
final NotificationEmitter emitter =
new NotificationBroadcasterSupport(info);
final Intf impl = new Impl(...);
final Object mbean = new StandardEmitterMBean(
impl, Intf.class, emitter);
mbs.registerMBean(mbean, objectName);
- 自从:
- 1.6
- 参见:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedStandardEmitterMBean(Class<?> mbeanInterface, boolean isMXBean, NotificationEmitter emitter) 创建一个MBean,其管理接口由mbeanInterface指定,并且通知由给定的NotificationEmitter处理。protectedStandardEmitterMBean(Class<?> mbeanInterface, NotificationEmitter emitter) 创建一个MBean,其管理接口由mbeanInterface指定,并且通知由给定的NotificationEmitter处理。StandardEmitterMBean(T implementation, Class<T> mbeanInterface, boolean isMXBean, NotificationEmitter emitter) 创建一个MBean,其管理接口由mbeanInterface指定,具有给定的实现,并且通知由给定的NotificationEmitter处理。StandardEmitterMBean(T implementation, Class<T> mbeanInterface, NotificationEmitter emitter) 创建一个MBean,其管理接口由mbeanInterface指定,具有给定的实现,并且通知由给定的NotificationEmitter处理。 -
Method Summary
Modifier and TypeMethodDescriptionvoidaddNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 向此MBean添加一个监听器。返回一个数组,指示此MBean可能发送的每个通知的通知的Java类名称和通知类型。void从此MBean中删除一个监听器。voidremoveNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 从此MBean中删除一个监听器。void发送一个通知。Methods declared in class javax.management.StandardMBean
cacheMBeanInfo, getAttribute, getAttributes, getCachedMBeanInfo, getClassName, getConstructors, getDescription, getDescription, getDescription, getDescription, getDescription, getDescription, getDescription, getImpact, getImplementation, getImplementationClass, getMBeanInfo, getMBeanInterface, getParameterName, getParameterName, invoke, postDeregister, postRegister, preDeregister, preRegister, setAttribute, setAttributes, setImplementation
-
Constructor Details
-
StandardEmitterMBean
创建一个MBean,其管理接口由
mbeanInterface指定,具有给定的实现,并且通知由给定的NotificationEmitter处理。生成的MBean通过将其方法转发到emitter来实现NotificationEmitter接口。对于emitter是NotificationBroadcasterSupport的实例,则MBean的sendNotification方法将调用emitter.sendNotification。新MBean上由
getNotificationInfo()返回的数组是在构造时由emitter.getNotificationInfo()返回的数组的副本。如果emitter.getNotificationInfo()返回的数组稍后更改,那将不会影响此对象的getNotificationInfo()。- 类型参数:
-
T- MBean的实现类型 - 参数:
-
implementation- MBean接口的实现。 -
mbeanInterface- 标准MBean接口。 -
emitter- 将处理通知的对象。 - 抛出:
-
IllegalArgumentException- 如果mbeanInterface不遵循管理接口的JMX设计模式,或者给定的implementation未实现指定的接口,或者emitter为null。
-
StandardEmitterMBean
public StandardEmitterMBean(T implementation, Class<T> mbeanInterface, boolean isMXBean, NotificationEmitter emitter) 创建一个MBean,其管理接口由
mbeanInterface指定,具有给定的实现,并且通知由给定的NotificationEmitter处理。此构造函数可用于创建标准MBean或MXBean。生成的MBean通过将其方法转发到emitter来实现NotificationEmitter接口。对于emitter是NotificationBroadcasterSupport的实例,则MBean的sendNotification方法将调用emitter.sendNotification。新MBean上由
getNotificationInfo()返回的数组是在构造时由emitter.getNotificationInfo()返回的数组的副本。如果emitter.getNotificationInfo()返回的数组稍后更改,那将不会影响此对象的getNotificationInfo()。- 类型参数:
-
T- MBean的实现类型 - 参数:
-
implementation- MBean接口的实现。 -
mbeanInterface- 标准MBean接口。 -
isMXBean- 如果为true,则mbeanInterface参数指定MXBean接口,生成的MBean是MXBean。 -
emitter- 将处理通知的对象。 - 抛出:
-
IllegalArgumentException- 如果mbeanInterface不遵循管理接口的JMX设计模式,或者给定的implementation未实现指定的接口,或者emitter为null。
-
StandardEmitterMBean
创建一个MBean,其管理接口由
mbeanInterface指定,通知由给定的NotificationEmitter处理。生成的MBean通过将其方法转发到emitter来实现NotificationEmitter接口。对于
emitter是NotificationBroadcasterSupport的实例,则MBean的sendNotification方法将调用emitter.sendNotification。新MBean上由
getNotificationInfo()返回的数组是在构造时由emitter.getNotificationInfo()返回的数组的副本。如果emitter.getNotificationInfo()返回的数组稍后更改,那将不会影响此对象的getNotificationInfo()。此构造函数必须从实现给定
mbeanInterface的子类中调用。- 参数:
-
mbeanInterface- 标准MBean接口。 -
emitter- 将处理通知的对象。 - 抛出:
-
IllegalArgumentException- 如果mbeanInterface不遵循管理接口的JMX设计模式,或者this未实现指定的接口,或者emitter为null。
-
StandardEmitterMBean
protected StandardEmitterMBean(Class<?> mbeanInterface, boolean isMXBean, NotificationEmitter emitter) 创建一个MBean,其管理接口由
mbeanInterface指定,通知由给定的NotificationEmitter处理。此构造函数可用于创建标准MBean或MXBean。生成的MBean通过将其方法转发到emitter来实现NotificationEmitter接口。对于
emitter是NotificationBroadcasterSupport的实例,则MBean的sendNotification方法将调用emitter.sendNotification。新MBean上由
getNotificationInfo()返回的数组是在构造时由emitter.getNotificationInfo()返回的数组的副本。如果emitter.getNotificationInfo()返回的数组稍后更改,那将不会影响此对象的getNotificationInfo()。此构造函数必须从实现给定
mbeanInterface的子类中调用。- 参数:
-
mbeanInterface- 一个StandardMBean接口。 -
isMXBean- 如果为true,则mbeanInterface参数指定了一个MXBean接口,结果MBean将是一个MXBean。 -
emitter- 将处理通知的对象。 - 抛出:
-
IllegalArgumentException- 如果mbeanInterface不遵循JMX管理接口的设计模式,或者如果this没有实现指定的接口,或者如果emitter为null。
-
-
Method Details
-
removeNotificationListener
public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException 从接口复制的描述:NotificationBroadcaster从此MBean中移除一个监听器。如果监听器已经使用不同的handback对象或通知过滤器进行注册,则将删除与该监听器对应的所有条目。- 指定者:
-
removeNotificationListener在接口NotificationBroadcaster - 参数:
-
listener- 先前添加到此MBean的监听器。 - 抛出:
-
ListenerNotFoundException- 该监听器未向MBean注册。 - 参见:
-
removeNotificationListener
public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException 从接口复制的描述:NotificationEmitter从此MBean中移除一个监听器。MBean必须具有与给定的
listener、filter和handback参数完全匹配的监听器。如果存在多个这样的监听器,则只会移除一个。如果
filter和handback参数为null,则只有在要移除的监听器中它们为null时才能为null。- 指定者:
-
removeNotificationListener在接口NotificationEmitter - 参数:
-
listener- 先前添加到此MBean的监听器。 -
filter- 添加监听器时指定的过滤器。 -
handback- 添加监听器时指定的handback。 - 抛出:
-
ListenerNotFoundException- 该监听器未向MBean注册,或者未使用给定的过滤器和handback注册。
-
addNotificationListener
public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) 从接口复制的描述:NotificationBroadcaster向此MBean添加一个监听器。- 指定者:
-
addNotificationListener在接口NotificationBroadcaster - 参数:
-
listener- 将处理广播器发出的通知的监听器对象。 -
filter- 过滤器对象。如果filter为null,则在处理通知之前不会执行任何过滤。 -
handback- 一个不透明对象,在发出通知时将被发送回监听器。该对象不能被通知广播器对象使用。在将通知发送给监听器时,应保持不变。 - 参见:
-
getNotificationInfo
从接口复制的描述:NotificationBroadcaster返回一个数组,指示此MBean可能发送的每个通知的Java类名称和通知类型。
MBean发送未在此数组中描述的通知并不违法。但是,MBean服务器的一些客户端可能依赖于该数组的完整性以确保其正确运行。
- 指定者:
-
getNotificationInfo在接口NotificationBroadcaster - 返回:
- 可能通知的数组。
-
sendNotification
发送一个通知。
如果构造函数中的
emitter参数是NotificationBroadcasterSupport的实例,则此方法将调用emitter.sendNotification。- 参数:
-
n- 要发送的通知。 - 抛出:
-
ClassCastException- 如果构造函数中的emitter参数不是NotificationBroadcasterSupport。
-