JMXBean条目未显示 [英] JMXBean entries are not showing up

查看:57
本文介绍了JMXBean条目未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JMXBean的实现

PerformanceMetadata jmxBean = new PerformanceMetadata();                        
responseDocument = (Document) serviceOperation.invoke(serviceComponent,RequestDocument);
jmxBean.setNumOfRequests(1);

JMXBean类:

public class PerformanceMetadata implements PerformanceMetadataMBean{
    private int numOfRequests;
        public int getNumOfRequests() {
        return numOfRequests;
    }

    public void setNumOfRequests(int numOfRequests) {
        this.numOfRequests = numOfRequests;
    }

注册JMXBean的类:我在启动服务器时调用该类.

Class Registering the JMXBean: I call this class while booting up the server.

public class JMXBeans {
    public void registerJMXBeans() 
    {
        MBeanServer mbs = null;
        PerformanceMetadata metadataObj = null;
        ObjectName name;
        try 
        {
            metadataObj = new PerformanceMetadata();
            mbs = ManagementFactory.getPlatformMBeanServer();
            name = new ObjectName("test.performace:type=PerformanceMetadataMBean");
            mbs.registerMBean(metadataObj, name);
        }

但是我看不到JMXBean.NumberOfRequests的任何值

But I don't see any value for JMXBean.NumberOfRequests

推荐答案

此处的问题是,正在注册的Bean实例与您要为其设置值的实例不同.因此,您需要协调传递已注册的同一实例(可以选择实现为单例),也可以使用JMX操作简单地更新MBean.通过创建代理调用程序,还可以简化执行JMX操作的复杂性.我建议这种方法:

The issue here is that the instance of the bean being registered is not the same as the instance you are setting the values on. So you need to coordinate passing around the same instance that was registered (optionally implementing as a singleton) or you could simply update the MBean using a JMX operation. The complexity of executing a JMX operation can also be simplified by creating a proxy invoker. I suggest this approach:

  • 将您的 NumOfRequests 字段更改为

  • Change your NumOfRequests field to an AtomicInteger since you want to make the MBean thread-safe[er]. ie.

您的MBean和接口应该具有属性访问器(获取器)和增量器.

Your MBean and the interface should have an attribute accessor (a getter) and an incrementor.

界面

public int getNumOfRequests();
public void incrementNumOfRequests(int requests);

实施

private final AtomicInteger numOfRequests = new AtomicInteger(0);
public int getNumOfRequests() {
    return numOfRequests.get();
}
public void incrementNumOfRequests(int requests) {
   numOfRequests.addAndGet(requests);
}

现在,您可以注册一次bean实例,然后通过可即时生成的JMX代理对其进行更新.这是通过 MBeanServerInvocationHandler 完成的.

Now you can register the bean instance once, and update it through the JMX proxies which you can generate on the fly. This is done using a MBeanServerInvocationHandler.

PerformanceMetadataMBean proxy = (PerformanceMetadataMBean)MBeanServerInvocationHandler.newProxyInstance(ManagementFactory.getPlatformMBeanServer(),
                                                   new ObjectName("test.performace:type=PerformanceMetadataMBean"),
                                                   PerformanceMetadataMBean.class,
                                                   false);

现在,当您调用proxy.incrementNumOfRequests(14)时,它实际上是在调用bean的 incrementNumOfRequests 操作.

Now, when you invoke proxy.incrementNumOfRequests(14), under the covers, it is invoking the bean's incrementNumOfRequests operation.

旋转一下.

这篇关于JMXBean条目未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆