jmx监听weblogic10的文档:http://download.oracle.com/docs/cd/E12840_01/wls/docs103/wlsmbeanref/core/index.html
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;
import weblogic.health.HealthState;
public class WeblogicJmxTest {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
/*
* 实例化 DomainRuntimeServiceMBean 对象名,这样可以通过类使用此对象名.
*/
static {
try {
service = new ObjectName(
"com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
} catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}
public static void main(String[] args) throws Exception {
String hostname = ip;
String portString = port;
String username = "weblogic";
String password = "weblogic";
WeblogicJmxTest demo = new WeblogicJmxTest();
demo.initConnection(hostname, portString, username, password);
demo.printNameAndState(demo.getServerRuntimes());
connector.close();
}
/*
* 实例化与 Domain Runtime MBean Server 的连接。
*/
private 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.domainruntime";
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();
}
/*
* 打印一组 ServerRuntimeMBeans.此 MBean 是运行时 MBean 层次的根,此域中的每个服务器承载自己的实例.
*/
public ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
}
/*
* 迭代 ServerRuntimeMBean,获取名称和状态
*/
public void printNameAndState(ObjectName[] p_objNames) throws Exception {
ObjectName[] serverRT = p_objNames;
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) {
print("=============Weblogic运行信息===============", "");
// 域名称
String name = (String) connection.getAttribute(serverRT[i], "Name");
System.out.println("域名称: " + name);
// 运行状态
String state = (String) connection.getAttribute(serverRT[i], "State");
System.out.println("运行状态: " + state);
System.out.println("监听端口: " + connection.getAttribute(serverRT[i], "ListenPort"));
{
ObjectName ob1 = (ObjectName) connection.getAttribute(serverRT[i], "ThreadPoolRuntime");
ObjectName obj1_ob2 = (ObjectName) connection.getAttribute(ob1, "Parent");
if(obj1_ob2!=null){
System.out.println(connection.getAttribute(obj1_ob2, "Type"));
int threadPool_health = ((HealthState) connection.getAttribute(obj1_ob2, "HealthState")).getState() ;
print("ob2_HealthState", HealthState.mapToString(threadPool_health));
}
}
}
}
public void print(String prefix, String content) {
System.out.println(prefix + ": " + content);
}
}