【发布时间】:2014-08-14 13:04:20
【问题描述】:
我正在尝试让一个类实现一个 MBean 接口,这样我就可以在运行时查询属性。我要询问的班级如下
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)
我认为可能是因为它返回了一个地图?
【问题讨论】:
-
感谢您提供如此有用的评论,如果我明白我的意思,我就不会在这里发布。毫无疑问,您的讽刺应该可以帮助您获得更多分数 - 干得好
-
这是一个真正的问题。无论如何,您不了解异常的哪一部分?一开始,用“不实现” --> 表示你的类需要实现接口。
-
这是我困惑的根源,因为我遵循的教程没有实现这个接口 - 也许我应该去 rtfm
-
将托管 Bean 类名重命名为“YourClasssNameMXBean”对我有用。