weblogic中通过JMX获取执行线程的详细信息

//来源:http://middlewaremagic.com/weblogic/?p=549

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.management.runtime.*;
 
public class CompleteWebLogicMonitoring {
	private static MBeanServerConnection connection;
	private static JMXConnector connector;
	private static final ObjectName service;
	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 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 h = new Hashtable();
		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[] getServerRuntimes() throws Exception {
		return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");
	}
 
	public void printNameAndState() throws Exception {
		ObjectName[] serverRT = getServerRuntimes();
		System.out.println(" Server   State");
		int length = (int) serverRT.length;
		for (int i = 0; i < length; i++) {
			String name = (String) connection.getAttribute(serverRT[i],"Name");
			String state = (String) connection.getAttribute(serverRT[i],"State");
			System.out.println( name + " : " + state);
		}
	}
	 
	public void threadDump() throws Exception
	{
		ObjectName[] serverRT = getServerRuntimes();
		int length = (int) serverRT.length;
		for (int i = 0; i < length; i++)
		{
			String name = (String) connection.getAttribute(serverRT[i],"Name");
			ObjectName executeQueueRT[] =  (ObjectName[]) connection.getAttribute(serverRT[i],"ExecuteQueueRuntimes");
			System.out.println("\n....<"+name+"> :<ExecuteQueueRuntimes>..........Length: "+executeQueueRT.length);
			 
			for(int k=0;k<executeQueueRT.length;k++)
			{
				ExecuteThread[] executeThreads=(ExecuteThread[])connection.getAttribute(executeQueueRT[k], "ExecuteThreads");
				for(int j=0;j<executeThreads.length;j++)
				{
					String currReq=executeThreads[i].getCurrentRequest() ;
					String threadName=executeThreads[i].getName();
					String appName=executeThreads[i].getApplicationName()  ;
					int servicedRequestTotalCount=executeThreads[i].getServicedRequestTotalCount();
					System.out.println("getName                      : " +threadName);
					System.out.println("getCurrentRequest            : " +currReq);
					System.out.println("getApplicationName           : " +appName);
					System.out.println("getServicedRequestTotalCount : " +servicedRequestTotalCount);
					System.out.println(".......................................\n");
				}
			}
		}
	}
	 
	public static void main(String[] args) throws Exception {
		if(args.length < 4) {
			System.out.println("<Usage>: java CompleteWebLogicMonitoring  adm-host  adm-port adm-username adm-password");
			System.exit(0);
		}
		String hostname = args[0];
		String portString = args[1];
		String username = args[2];
		String password = args[3];
		CompleteWebLogicMonitoring s = new CompleteWebLogicMonitoring();
		initConnection(hostname, portString, username, password);
		 
		s.printNameAndState();
		s.threadDump();
		connector.close();
	}
}

猜你喜欢

转载自sheng.iteye.com/blog/1984476