- 论坛徽章:
- 3
|
JMX监控weblogic的JVM,报java.lang.ClassCastException错误,请指教
环境:weblogic9.2 jdk1.5
代码:
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
public class PrintJvmState {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
// 实例化 RuntimeServiceMBean 对象名
// 这样可以通过类使用此对象名。
static {
try {
service = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
}catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}
/*
* 实例化与 Domain Runtime MBean Server 的连接
*/
public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.runtime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
port, jndiroot + mserver);
Hashtable<String, String> h = new Hashtable<String, String>();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}
public static ObjectName[] getJVMRuntime() throws Exception {
ObjectName serviceRT = (ObjectName) connection.getAttribute(service, "ServerRuntime");
return (ObjectName[]) connection.getAttribute(serviceRT, "JVMRuntime");
}
/*
* 迭代 ServerRuntimeMBean,获取名称和状态
*/
public void printJvmInformation() throws Exception {
ObjectName[] jvmRT = getJVMRuntime();
System.out.println("got jvm runtimes");
int length = (int) jvmRT.length;
for (int i = 0; i < length; i++) {
String name = (String) connection.getAttribute(jvmRT[i], "Name");
int HeapSizeCurrent = Integer.parseInt(String.valueOf(connection.getAttribute(jvmRT[i], "HeapSizeCurrent")));
int HeapFreeCurrent = Integer.parseInt(String.valueOf(connection.getAttribute(jvmRT[i], "HeapFreeCurrent")));
int HeapFreePercent = Integer.parseInt(String.valueOf(connection.getAttribute(jvmRT[i], "HeapFreePercent")));
System.out.println("Server name: " + name + ".HeapSizeCurrent: " + HeapSizeCurrent + "HeapFreeCurrent: " + HeapFreeCurrent + "HeapFreePercent: " + HeapFreePercent);
}
}
public static void main(String[] args) throws Exception {
String hostname = "*。*。*。*";
String portString = "7001";
String username = "weblogic";
String password = "weblogic";
PrintJvmState jvm = new PrintJvmState();
initConnection(hostname, portString, username, password);
jvm.printJvmInformation();
connector.close();
}
}
报错信息:
Exception in thread "main" java.lang.ClassCastException: javax.management.Object
Name
at PrintJvmState.getJVMRuntime(PrintJvmState.java:50)
at PrintJvmState.printJvmInformation(PrintJvmState.java:56)
at PrintJvmState.main(PrintJvmState.java:74) |
|