我有超过 5 个 spring web 应用程序,它们都在使用另一个公共(public)库。这个公共(public)库有它自己的 MBean。由于强制的唯一对象名称约束,我的应用程序无法部署在同一台服务器上。
我使用MBean的方式是这样的:
@ManagedResource(objectName = "com.org.city:name=City", description = "City related operations")
我想为所有应用程序使用具有不同对象名称的相同 MBean 类。在不复制我的 MBean 的情况下使用它的正确方法是什么。
谢谢
最佳答案
我遇到了同样的问题,并建立了 Cemo's solution .这是一个示例实现。
上下文.xml
<!-- Set up jmx bean auto scanning -->
<!-- Note: we're not using <context:mbean-export /> because we need to provide our own naming strategy -->
<bean id="mbeanExporter" class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
<property name="namingStrategy">
<bean class="com.foo.MultiAppMetadataNamingStrategy">
<property name="applicationName" value="${application.name}" />
</bean>
</property>
</bean>
MultiAppMetadataNamingStrategy.java
public class MultiAppMetadataNamingStrategy implements ObjectNamingStrategy, InitializingBean {
private String applicationName;
public MultiAppMetadataNamingStrategy() {
}
public MultiAppMetadataNamingStrategy(String applicationName) {
this.applicationName = Preconditions.checkNotNull(applicationName, "applicationName must not be null");
}
public void setApplicationName(String applicationName) {
this.applicationName = Preconditions.checkNotNull(applicationName, "applicationName must not be null");
}
@Override
public void afterPropertiesSet() throws Exception {
if (applicationName == null) {
throw new IllegalArgumentException("Property 'applicationName' is required");
}
}
@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
Class managedClass = AopUtils.getTargetClass(managedBean);
String domain = ClassUtils.getPackageName(managedClass);
Hashtable<String, String> properties = new Hashtable<>();
properties.put("type", ClassUtils.getShortName(managedClass));
properties.put("name", beanKey);
// ensure the application name is included as a property in the object name
properties.put("app", applicationName);
return ObjectNameManager.getInstance(domain, properties);
}
}
这允许设置一个 mbean,如:
package com.foo;
@ManagedResource(description = "Bean description")
public class MyBean {
...
}
这将使用对象名称 com.foo:name=myBean,type=MyBean,app=CustomAppName
注册一个 mbean
关于java - 同一服务器上的多个应用程序使用相同的 JMX Mbean 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7075007/