我想使用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); Setinstances = 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"));
由于类型“类型”不是属性。另外,如果您尝试按的一部分来查询“类型”,则ObjectName
可以这样构造对象名称:
String objectName = "*:type=Hello";
然后像以前一样进行查询:
Setinstances = server.queryMBeans(new ObjectName(objectName), null);
参见http://codeidol.com/java/java-jmx/The-MBeanServer/Finding-MBeans/