在不使用NotificationPublisherAware的情况下使用Spring发布JMX通知 [英] Publish JMX notifications in using Spring without NotificationPublisherAware
问题描述
我想使用Spring 3发布JMX通知,但想避免使用NotificationPublisherAware接口,因为不使用Spring的应用程序也使用该代码.使用MBeanExporter bean公开bean.我发现的替代方法需要注册mbean,我目前使用Spring配置进行注册,因此这是一个不好的选择. 有没有一种方法可以避免使用NotificationPublisherAware接口,但仍可以发布通知?
I'd like to publish JMX notifications using Spring 3, but would like to avoid using the NotificationPublisherAware interface, since the code is also used by an application that doesn't use Spring. The bean is exposed using MBeanExporter bean. The alternatives I found require registering the mbeans, which I currently do using Spring configuration, so this is a bad option. Is there a way to avoid using the NotificationPublisherAware interface but still publish notifications?
推荐答案
您不必在代码中使用任何Spring类.示例:
You don't have to use any Spring class in code. Example:
接口:
import javax.management.MXBean;
@MXBean
public interface SecurityEventsManagerMXBean {
...
@AttributeMetaData(value="UserLoginFailures", defaultValue="0", description="Total user login failures")
public int getUserLoginFailureCount() ;
...
}
Bean:
import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;
public class SecurityEventsManager extends NotificationBroadcasterSupport implements SecurityEventsManagerMXBean {
...
private void notifyUserLoginFailure(...) {
Notification notification = new Notification(...) ;
sendNotification(notification)
userLoginFailureCount++ ;
}
}
@AttributeMetaData是方便的元注释,用于定义描述符键:
Here @AttributeMetaData is a convenient meta annotation that defines descriptor keys:
import javax.management.DescriptorKey;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AttributeMetaData {
@DescriptorKey("displayName")
String value();
....
}
编辑3月8日.配置以在Mbean上方导出:
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"
p:locateExistingServerIfPossible="true" />
<bean id="jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
<bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy"
p:attributeSource-ref="jmxAttributeSource" />
<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"
p:attributeSource-ref="jmxAttributeSource" />
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="server" ref="mbeanServer"/>
<property name="assembler" ref="assembler"/>
<property name="registrationBehaviorName" value="REGISTRATION_FAIL_ON_EXISTING"/>
<property name="beans">
<map>
<entry>
<key>
<util:constant
static-field="x.y.z.SecurityEventsManager.OBJECT_NAME" />
</key>
<ref bean="securityEventsManager" />
</entry>
</map>
</property>
</bean>
<bean id="securityEventsManager" class="x.y.z.SecurityEventsManager" />
这篇关于在不使用NotificationPublisherAware的情况下使用Spring发布JMX通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!