我在带有 XML 配置的 Spring 应用程序中使用 JMX:
<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="bean:name=bean1" value-ref="bean1"/>
<entry key="bean:name=bean2" value-ref="bean2"/>
<entry key="bean:name=bean3" value-ref="bean3"/>
</map>
</property>
<property name="notificationListenerMappings">
<map>
<entry key="*">
<bean class="com.test.listener"/>
</entry>
</map>
</property>
</bean>
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
<property name="port" value="1099" />
</bean>
<bean id="serverConnector"
class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName" value="connector:name=rmi" />
<property name="serviceUrl"
value="service:jmx:rmi://localhost/jndi/rmi://localhost:1099/jmxrmi" />
</bean>
我从各种文档中了解到,我们可以用@EnableMBeanExport
和用@ManagedResource
bean 来注释它,而不是这个 XML 配置。
但我怀疑如何ConnectorServerFactoryBean
配置这些注释。或者是否有任何注释可用于配置RMI
和connectorServerFactoryBean
?
我还需要知道如何注释、notificationListenerMappings
配置?
PS:我的代码在 XML 配置下为发布者和侦听器工作。我计划完全基于注释移动它,因为我不想干扰 PROD 中已经存在的 XML 配置。
已编辑
找到以下一段代码:打算尝试一下:
@Bean
public RmiRegistryFactoryBean registry() {
return new RmiRegistryFactoryBean();
}
@Bean
@DependsOn("registry")
public ConnectorServerFactoryBean connectorServer() throws MalformedObjectNameException {
ConnectorServerFactoryBean connectorServerFactoryBean = new ConnectorServerFactoryBean();
connectorServerFactoryBean.setObjectName("connector:name=rmi");
connectorServerFactoryBean.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/connector");
return connectorServerFactoryBean;
}
编辑 2: 我正在继续上述方法,我能够配置 MBean 并能够发布通知。但不幸的是,我坚持通过 Annotation 配置 NotificationListener。
我尝试添加以下内容:
@Bean
@DependsOn("registry")
public ConnectorServerFactoryBean connectorServer() throws MalformedObjectNameException {
ConnectorServerFactoryBean connectorServerFactoryBean = new ConnectorServerFactoryBean();
connectorServerFactoryBean.setObjectName("connector:name=rmi");
connectorServerFactoryBean.setServiceUrl("service:jmx:rmi://localhost/jndi/rmi://localhost:1099/connector");
//TestListener is my NotificationListener class
ObjectName objectName = new ObjectName("bean:name=bean1");
connectorServerFactoryBean.getServer().addNotificationListener(objectName,
new TestListener(), null,null);
return connectorServerFactoryBean;
}
我收到 instanceNotFoundException 声明bean:name=bean1
未找到。但我@ManagedResource(objectName="bean:name=bean1")
在我的bean1
.
请对我所缺少的有任何帮助吗?