当前位置:  开发笔记 > 编程语言 > 正文

带有查询exp的JMX MBeanServer.queryMBeans不起作用

如何解决《带有查询exp的JMXMBeanServer.queryMBeans不起作用》经验,为你挑选了1个好方法。

我想使用MBeanServer.queryMBeans(ObjectName name,QueryExp query)来获取带有查询表达式的MBean,但似乎它不适用于QueryExp查询。例如,我有这样的测试代码:

public class CreateAndRegisterMBeanInMBeanServer {

/**
 * @param args
 */
public static void main(String[] args) throws Exception {

    String objectName = "com.javacodegeeks.snippets.enterprise:type=Hello";
    QueryExp exp = Query.eq(Query.attr("type"), Query.value("Hello"));


    MBeanServer server = ManagementFactory.getPlatformMBeanServer();

    // Construct the ObjectName for the Hello MBean we will register
    ObjectName mbeanName = new ObjectName(objectName);

    Hello mbean = new Hello();

    server.registerMBean(mbean, mbeanName);

    Set instances = server.queryMBeans(new ObjectName(objectName), null);
    System.out.println(instances.size());

    for (int i = 0; i < instances.size(); i++) {
        ObjectInstance instance = (ObjectInstance) instances.toArray()[i];

        System.out.println("Class Name:t" + instance.getClassName());
        System.out.println("Object Name:t" + instance.getObjectName());
    }

}

static class Hello implements HelloMBean {

    private String message = "Hello World";

    @Override
    public String getMessage() {
        return this.message;
    }

    @Override
    public void sayHello() {
        System.out.println(message);
    }

    @Override
    public void setMessage(String message) {
        this.message = message;
    }

}

static interface HelloMBean {

    // operations

    public void sayHello();

    // attributes

    // a read-write attribute called Message of type String
    public String getMessage();

    public void setMessage(String message);

}

}

我可以期望得到:1类名称:tCreateAndRegisterMBeanInMBeanServer $ Hello对象名称:tcom.javacodegeeks.snippets.enterprise:type = Hello

但是,如果我更改代码以使用定义的QueryExp exp为:

设置实例= server.queryMBeans(new ObjectName(objectName),exp);

我看不到任何选定的MBean,这是使用此类Query Exp的正确方法吗?

谢谢,



1> 小智..:

更改

QueryExp exp = Query.eq(Query.attr("type"), Query.value("Hello"));

QueryExp exp = Query.eq(Query.attr("Message"), Query.value("Hello World"));

由于类型“类型”不是属性。另外,如果您尝试按的一部分来查询“类型”,则ObjectName可以这样构造对象名称:

String objectName = "*:type=Hello";

然后像以前一样进行查询:

Set instances = server.queryMBeans(new ObjectName(objectName), null);

参见http://codeidol.com/java/java-jmx/The-MBeanServer/Finding-MBeans/

推荐阅读
手机用户2402851335
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有