我试图让一个类实现一个 MBean 接口(interface),这样我就可以在运行时查询属性。我要查询的类如下
public class ProfileCache implements ProfileCacheInterfaceMBean{
private Logger logger = Logger.getLogger(ProfileCache.class);
private ConcurrentMap<String, Profile> cache;
public ProfileCache(ConcurrentMap<String, Profile> cache){
this.cache = cache;
}
/**
* Update the cache entry for a given user id
* @param userid the user id to update for
* @param profile the new profile to store
* @return true if the cache update
*/
public boolean updateCache(String userid, Profile profile) {
if (cache == null || cache.size() == 0) {
throw new RuntimeException("Unable to update the cache");
}
if (cache.containsKey(userid)) {
if (profile != null) {
cache.put(userid, profile);
logger.info("Updated the cache for user: "
+ userid + " profile: " + profile);
return true;
}
}
return false;
}
@Override
public ConcurrentMap<String, Profile> getCache() {
if(cache == null){
cache = new ConcurrentHashMap<String, Profile>();
}
return cache;
}
}
界面是这样的
import com.vimba.profile.Profile;
public interface ProfileCacheInterfaceMBean {
ConcurrentMap<String, Profile> getCache();
}
然后我像这样启动 MBean
cacheImpl = new ProfileCache(factory.createCacheFromDB());
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName profileCache = new ObjectName("org.javalobby.tnt.jmx:type=ProfileCacheInterfaceMBean");
mbs.registerMBean(cacheImpl, profileCache);
但是我不断收到以下异常,我不确定我需要更改什么
javax.management.NotCompliantMBeanException: MBean class com.vimba.cache.ProfileCache does not implement DynamicMBean, and neither follows the Standard MBean conventions (javax.management.NotCompliantMBeanException: Class com.vimba.cache.ProfileCache is not a JMX compliant Standard MBean) nor the MXBean conventions (javax.management.NotCompliantMBeanException: com.vimba.cache.ProfileCache: Class com.vimba.cache.ProfileCache is not a JMX compliant MXBean)
我认为这可能是因为它返回了一个 Map?
最佳答案
刚遇到这个异常,看了当前的答案还有https://blogs.oracle.com/jmxetc/entry/javax_management_standardmbean_when_and我认为强调和澄清以下已经阐明的内容可能会有所帮助:
NotCompliantMBeanException 是由于未能遵循此约定 'ConcreteClassName' implements 'ConcreteClassNameMBean' 而引起的,除其他外,
我通过将我的 mbean 接口(interface)的原始名称从“OrignalNameMBean”更新为“OriginalNameMXBean”来解决这个问题,允许在不遵循约定的情况下注册 mbean
另一种解决方案是遵循约定。
关于java - 在 Java 中创建 MBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25308744/