使用Nashorn javascript jjs查找JMX MBean

时间:2015-06-19 20:13:41

标签: java javascript jmx nashorn

我正在尝试更改Catalina / Connector / 8009 / *连接器中proxyName的值。问题是我在尝试查找MBean名称时遇到以下异常。

btn1.Attributes.Add("onclick", "return false;");

脚本是:

Exception in thread "main" java.lang.ClassCastException: Cannot cast java.lang.String to javax.management.QueryExp
        at java.lang.invoke.MethodHandleImpl.newClassCastException(MethodHandleImpl.java:361)
        at java.lang.invoke.MethodHandleImpl.castReference(MethodHandleImpl.java:356)
        at jdk.nashorn.internal.scripts.Script$jmx_test_jjs.:program(jmx-test.jjs:32)
        at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
        at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
        at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
        at jdk.nashorn.tools.Shell.apply(Shell.java:394)
        at jdk.nashorn.tools.Shell.runScripts(Shell.java:323)
        at jdk.nashorn.tools.Shell.run(Shell.java:169)
        at jdk.nashorn.tools.Shell.main(Shell.java:133)
        at jdk.nashorn.tools.Shell.main(Shell.java:112)

问题是我不知道如何在MBeanServerConnection接口的queryNames()方法中发送Java #! /usr/java/jdk1.8.0_25/bin/jjs var host="jmxremotehost" var port=7091 var serviceURL = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi" var url = new javax.management.remote.JMXServiceURL(serviceURL); var stringArrayType = Java.type("java.lang.String[]") var credentials = new stringArrayType(2) credentials[0]="jmxuser" credentials[1]="jmxpassword" var HashMapType = Java.type("java.util.HashMap") var environment = new HashMapType() environment.put("jmx.remote.credentials",credentials) var connector = javax.management.remote.JMXConnectorFactory.connect(url,environment) var mbeanServerConnection=connector.getMBeanServerConnection() var ObjectNameType = Java.type("javax.management.ObjectName") objectName = new ObjectNameType('Catalina:type=Connector,port=8009,*') print(mbeanServerConnection.queryNames(objectName, "proxyName"), null) 或其他有效的第二个参数。 请参阅:http://docs.oracle.com/javase/7/docs/api/javax/management/MBeanServerConnection.html#queryMBeans%28javax.management.ObjectName,%20javax.management.QueryExp%29

1 个答案:

答案 0 :(得分:1)

只使用 null 对我有用:

print(mbeanServerConnection.queryNames(objectName, null), null)

我稍微调整了脚本,并使用平台MBeanServer节省了一些时间:

#! /usr/lib/jvm/jdk1.8.0_45/bin/jjs
var mbeanServerConnection=java.lang.management.ManagementFactory.getPlatformMBeanServer();
var ObjectNameType = Java.type("javax.management.ObjectName")
objectName = new ObjectNameType('*:*')
print(mbeanServerConnection.queryNames(objectName, null), null)

输出结果为:

  

[java.lang中:类型=记忆池,名字=元空间,   java.lang:type = MemoryPool,name = PS Old Gen,   java.lang:type = GarbageCollector,name = PS Scavenge,   java.lang:type = MemoryPool,name = PS Eden Space,   JMImplementation:type = MBeanServerDelegate,java.lang:type = Runtime,   java.lang:type = Threading,java.lang:type = OperatingSystem,   java.lang:type = MemoryPool,name = Code Cache,   java.nio:type = BufferPool,name = direct,java.lang:type = Compilation,   java.lang中:类型=的MemoryManager,名字= CodeCacheManager,   java.lang:type = MemoryPool,name = Compressed Class Space,   java.lang:type = Memory,java.nio:type = BufferPool,name = mapped,   java.util.logging:type = Logging,java.lang:type = MemoryPool,name = PS   Survivor Space,java.lang:type = ClassLoading,   java.lang:type = MemoryManager,name = Metaspace Manager,   com.sun.management:type=DiagnosticCommand,   java.lang:type = GarbageCollector,name = PS MarkSweep,   com.sun.management:type=HotSpotDiagnostic]

稍微更新的Java版本,但我不认为这是有争议的。

<强>更新

尝试与HBase服务器建立远程连接并运行模式搜索以进行更类似的测试:

#! /usr/lib/jvm/jdk1.8.0_45/bin/jjs
var host="njwmintx"
var port=10101
var serviceURL = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi"
var url = new javax.management.remote.JMXServiceURL(serviceURL);
var connector = javax.management.remote.JMXConnectorFactory.connect(url)
var mbeanServerConnection=connector.getMBeanServerConnection()
var ObjectNameType = Java.type("javax.management.ObjectName")
objectName = new ObjectNameType('Hadoop:service=HBase,name=RegionServer,*')
print(mbeanServerConnection.queryNames(objectName, null), null)

输出是:

  

[Hadoop的:服务= HBase的,名称= RegionServer的,分=服务器,   Hadoop的:服务= HBase的,名称= RegionServer的,分=地区,   Hadoop的:服务= HBase的,名称= RegionServer的,分=复制,   Hadoop的:服务= HBase的,名称= RegionServer的,分= WAL]