2015-06-19 77 views
0

我試圖更改Catalina/Connector/8009/*連接器中的proxyName的值。問題是,在嘗試查找MBean名稱時出現以下異常。使用Nashorn查找JMX MBeans javascript jjs

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) 

的腳本是:

#! /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) 

的問題是,我不知道如何發送了Java null或其他任何有效的queryNames(第二個參數的MBeanServerConnection接口)方法。 見:http://docs.oracle.com/javase/7/docs/api/javax/management/MBeanServerConnection.html#queryMBeans%28javax.management.ObjectName,%20javax.management.QueryExp%29

回答

1

只需使用作品對我來說:

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:t ype = MemoryPool,name = Metaspace, java.lang:type = MemoryPool,name = PS Old Gen, java.lang:type = GarbageCollector,name = PS Scavenge, java.lang:type = MemoryPool,name = PS Eden空間, 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:type = MemoryManager,name = 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,name = RegionServer,sub = Server, Hadoop:service = HBase,name = RegionServer,sub = Regions, Hadoop:service = HBase,名稱= RegionServer的,分=複製, 的Hadoop:服務= HBase的,名稱= RegionServer的,分= WAL]

+0

那' 「的proxyName」'是不是好的....複製/粘貼編碼....謝謝你,尼古拉斯 –

+0

第二個',null'是不需要的。 –

+0

你如何轉換爲JavaScript數組的結果?我正在使用'mbeanServerConnection.queryNames(objectName,null).toArray()[0]'來收集第一個元素。但它看起來不夠優雅。 –