情况是:
- 应用服务器:Weblogic 12.2.1.2
- 应用程序:企业应用程序
工作场景:
- 部署在管理服务器上的应用程序。
- JConsole 正确显示了我的 MXBean。
不工作的情况:
- 应用程序部署在 2 个托管服务器的集群上。
- 在每个受管服务器上配置 JMX。
- JConsole 不显示我的 MXBean。
下面是我的源代码相关片段。
public class JpaCacheDescriptor {
private String name;
private int size;
@ConstructorProperties({ "name", "size" })
public JpaCacheDescriptor(String name, int size) {
this.name = name;
this.size = size;
}
// Not relevant
}
public interface JpaCacheManager {
List<JpaCacheDescriptor> getCaches();
void clearAll();
}
@Slf4j
public class JpaCacheManagerImpl
extends StandardMBean
implements JpaCacheManager
{
private static ObjectName objectName;
private static MBeanServer getMBeanServer()
throws NamingException
{
InitialContext initialContext = new InitialContext();
return (MBeanServer)initialContext.lookup("java:comp/jmx/runtime");
}
public JpaCacheManagerImpl()
throws NotCompliantMBeanException
{
super(JpaCacheManager.class);
}
public static void register() {
log.info("{ }");
try {
objectName = new ObjectName(String.format(
"${wls.jmx.root}:name=${application.name},version=${application.version},node=%s,type=%s",
System.getProperty("weblogic.Name"),
JpaCacheManager.class.getSimpleName()
));
JpaCacheManagerImpl jpaCacheMBean = new JpaCacheManagerImpl();
StandardMBean standardMBean = new StandardMBean(jpaCacheMBean, JpaCacheManager.class, true); // Force MXBean which is not inferred by @MXBean and MXBean prefix!!!
getMBeanServer().registerMBean(standardMBean, objectName);
log.info("Registered as \"{}\".", objectName.getCanonicalName());
} catch (
InstanceAlreadyExistsException
| MalformedObjectNameException
| MBeanRegistrationException
| NamingException
| NotCompliantMBeanException exception
) {
objectName = null;
log.error(exception.getMessage(), exception);
}
}
public static void unregister() {
log.info("{ }");
if (null == objectName) {
log.warn("MBean not registered!");
} else {
try {
getMBeanServer().unregisterMBean(objectName);
log.info("MBean unregistered.");
}
catch (
InstanceNotFoundException
| MBeanRegistrationException
| NamingException exception
) {
log.error(exception.getMessage(), exception);
}
}
}
@Override
public List<JpaCacheDescriptor> getCaches() {
// Not relevant
}
@Override
public void clearAll() {
// Not relevant
}
}
我花了很多时间寻找解决方案,但没有运气!
先感谢您。