java - 在 Java 中创建 MBean

标签 java jmx mbeans

我试图让一个类实现一个 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我认为强调和澄清以下已经阐明的内容可能会有所帮助:

  1. NotCompliantMBeanException 是由于未能遵循此约定 'ConcreteClassName' implements 'ConcreteClassNameMBean' 而引起的,除其他外,

  2. 我通过将我的 mbean 接口(interface)的原始名称从“OrignalNameMBean”更新为“OriginalNameMXBean”来解决这个问题,允许在不遵循约定的情况下注册 mbean

  3. 另一种解决方案是遵循约定。

关于java - 在 Java 中创建 MBean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25308744/

相关文章:

jmx - 属性和项目的 Prometheus jmx 导出器模式匹配

java - 当使用注释声明 bean 时,JMX MBean 不会显示在 JConsole 上

java - 使用异常进行控制流

java - Iterator<T> 返回对象

java - 如何以编程方式检查 JMX MBean 操作和属性?

java - JMX混淆(远程服务器控制)

java - 计算 Java Int 溢出

java - 如何从 CXF 请求拦截器中的 SoapMessage 获取 WSDL 操作名称

java - 如何创建由 jmx 公开并通过 jconsole 访问的性能计数器?

java - 列出所有已注册的 JMX Bean