英文:
How to get children of ObjectName using JMX API
问题
我需要获取应用程序服务器中资源(可用数据源列表)的所有子对象。
为此目的,我使用以下代码片段:
ObjectName name = new ObjectName("jboss.as:subsystem=datasources");
Set<ObjectInstance> instances = server.queryMBeans(name, null);
Iterator<ObjectInstance> iterator = instances.iterator();
while (iterator.hasNext()) {
ObjectInstance instance = iterator.next();
System.out.println("找到 MBean:");
System.out.println("类名:" + instance.getClassName());
System.out.println("对象名:" + instance.getObjectName());
}
然而,我只能检索到 "jboss.as:subsystem=datasources" 的对象名,但我需要找到该树下的可用数据源。我无法在 ObjectInstance 中找到任何深入的方法。
有什么帮助吗?
英文:
I need to get all children objects of a resource available in the application server (the list of available datasources).
I'm using this piece of code for this purpose:
ObjectName name = new ObjectName("jboss.as:subsystem=datasources");
Set<ObjectInstance> instances = server.queryMBeans(name, null);
Iterator<ObjectInstance> iterator = instances.iterator();
while (iterator.hasNext()) {
ObjectInstance instance = iterator.next();
System.out.println("MBean Found:");
System.out.println("Class Name:" + instance.getClassName());
System.out.println("Object Name:" + instance.getObjectName());
}
However, I'm only able to retrieve the object name for "jboss.as:subsystem=datasources" but I need to find the available datasources, which are under that tree. I cannot find any method in the ObjectInstance to dig into it.
Any help?
答案1
得分: 2
你可以查询 MBeans 树,并通过名称进行对象过滤:
Set<ObjectInstance> instances = connection.queryMBeans(null, null);
Iterator<ObjectInstance> iterator = instances.iterator();
while (iterator.hasNext()) {
ObjectInstance instance = iterator.next();
if (instance.getObjectName().toString().startsWith("jboss.as:subsystem=datasources,data-source=") &&
(!instance.getObjectName().toString().contains(",statistics")))
System.out.println("对象名称:" + instance.getObjectName());
}
如果使用远程+HTTP协议,请确保在类路径中包含 jboss-client JAR。更多详细信息请参阅:如何获取 JBoss 中的数据源列表
英文:
You can query the MBeans tree and filter through the Objects by name:
Set<ObjectInstance> instances = connection.queryMBeans(null, null);
Iterator<ObjectInstance> iterator = instances.iterator();
while (iterator.hasNext()) {
ObjectInstance instance = iterator.next();
if (instance.getObjectName().toString().startsWith("jboss.as:subsystem=datasources,data-source=") &&
(!instance.getObjectName().toString().contains(",statistics")))
System.out.println("Object Name:" + instance.getObjectName());
}
Make sure, if using the remote+http protocol to include the jboss-client JAR in your classpath.
More details: How do I know the list of Datasources in JBoss
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论