如何使用JMX API获取ObjectName的子项

huangapple go评论72阅读模式
英文:

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(&quot;jboss.as:subsystem=datasources&quot;);
Set&lt;ObjectInstance&gt; instances = server.queryMBeans(name, null);

Iterator&lt;ObjectInstance&gt; iterator = instances.iterator();

while (iterator.hasNext()) {
	ObjectInstance instance = iterator.next();

	System.out.println(&quot;MBean Found:&quot;);
	System.out.println(&quot;Class Name:&quot; + instance.getClassName());
	System.out.println(&quot;Object Name:&quot; + 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&lt;ObjectInstance&gt; instances = connection.queryMBeans(null, null);
Iterator&lt;ObjectInstance&gt; iterator = instances.iterator();

while (iterator.hasNext()) {
    ObjectInstance instance = iterator.next();
    if (instance.getObjectName().toString().startsWith(&quot;jboss.as:subsystem=datasources,data-source=&quot;) &amp;&amp;
            (!instance.getObjectName().toString().contains(&quot;,statistics&quot;)))
    System.out.println(&quot;Object Name:&quot; + 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

huangapple
  • 本文由 发表于 2020年10月7日 18:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64241932.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定