gpt4 book ai didi

java - JMX MBeanServer.queryMBeans 与查询 exp 不起作用

转载 作者:行者123 更新时间:2023-12-03 02:55:28 26 4
gpt4 key购买 nike

我想使用 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<ObjectInstance> 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 的正确方法吗?

谢谢

最佳答案

改变

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

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

因为类型“type”不是一个属性。或者,如果您尝试按“类型”(ObjectName 的一部分)进行查询,则可以像这样构造对象名称:

String objectName = "*:type=Hello";

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

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

关于java - JMX MBeanServer.queryMBeans 与查询 exp 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34508184/

26 4 0