我想像这样注册 MBean:
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = null;
try {
name = ObjectNameProvider.getObjectName();
mbs.registerMBean(ApplicationTracker.getInstance(), name);
当我启动应用程序时,注册失败并且抛出了 javax.management.InstanceAlreadyExistsException,我无法使用 JMX 功能。
但是,如果我像下面这样取消注册-重新注册 MBean,一切正常,没有任何例外。为什么?
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = null;
try {
name = ObjectNameProvider.getObjectName();
if(mbs.isRegistered(name)){
mbs.unregisterMBean(name);
}
mbs.registerMBean(ApplicationTracker.getInstance(), name);