通过weblogic 9 mbean监控其应用状态

通过weblogic开放的jmx mbean可以对部署其上的应用\jms\datasource。。。很多内容进行查看和管理,下面这个demo只是借鉴了一下weblogic提供的例子作一个简单的测试。如果想通过这个实现监控,可以为这段代码加上定时设置(通过TimerTask或者quartz)。

下面这段代码主要查看应用的状态以及jms相关情况(需要依赖weblogic.jar)。

 

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 PrintServerState {

	private static MBeanServerConnection connection;
	private static JMXConnector connector;
	private static final ObjectName service;

	// Initializing the object name for DomainRuntimeServiceMBean
	// so it can be used throughout the class.
	static {
		try {
			service = new ObjectName(
					"com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
		} catch (MalformedObjectNameException e) {
			throw new AssertionError(e.getMessage());
		}
	}

	/*
	* Initialize connection to the 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.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();

	}

	/* 
	* Print an array of ServerRuntimeMBeans.
	* This MBean is the root of the runtime MBean hierarchy, and
	* each server in the domain hosts its own instance.
	*/
	public static ObjectName[] getServerRuntimes() throws Exception {
		return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
	}

	/* 
	* Iterate through ServerRuntimeMBeans and get the name and state
	*/
	public void printNameAndState() throws Exception {
		ObjectName[] serverRT = getServerRuntimes();
		System.out.println("got domain runtimes");
		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], "Type");
			System.out.println("Server name: " + name + ".   Server state: " + state);
		}
	}

	public void printAppNameAndState() throws Exception {
		ObjectName[] serverRT = getServerRuntimes();
		for (int k = 0; k < serverRT.length; k++) {
			ObjectName[] appRT = (ObjectName[]) connection.getAttribute(serverRT[k], "ApplicationRuntimes");
			int length = appRT.length;

			for (int i = 0; i < length; i++) {
				String appName = (String) connection.getAttribute(appRT[i], "Name");

				ObjectName[] compRT = (ObjectName[]) connection.getAttribute(appRT[i], "ComponentRuntimes");
				for (int j = 0; j < compRT.length; j++) {
					int appState = ((Integer) connection.getAttribute(compRT[j], "DeploymentState")).intValue();
					String type = (String) connection.getAttribute(compRT[j], "Type");
					System.out.println(k + "|" + j + "|Server name: " + appName + ".    Server type: " + type
							+ "   Server state: " + appState);

				}

			}
		}

	}

	public void printJMS() throws Exception {
		ObjectName[] serverRT = getServerRuntimes();

		ObjectName JMSRT = (ObjectName) connection.getAttribute(serverRT[0], "JMSRuntime");

		ObjectName[] JMSServers = (ObjectName[]) connection.getAttribute(JMSRT, "JMSServers");
		int JMSServer_Length = (int) JMSServers.length;

		for (int x = 0; x < JMSServer_Length; x++) {
			String JMSServer_name = (String) connection.getAttribute(JMSServers[x], "Name");

			ObjectName[] JMSDests = (ObjectName[]) connection.getAttribute(JMSServers[x], "Destinations");
			int JMSdest_Length = (int) JMSDests.length;

			for (int y = 0; y < JMSdest_Length; y++) {
				String queue_name = (String) connection.getAttribute(JMSDests[y], "Name");

				long pendingmcount = (Long) connection.getAttribute(JMSDests[y], "MessagesPendingCount");
				long currentcount = (Long) connection.getAttribute(JMSDests[y], "MessagesCurrentCount");

				System.out.println(y + "|jms server name: " + JMSServer_name + "  jms name: " + queue_name
						+ "   pending: " + pendingmcount + "   current: " + currentcount);

			} //for y

		} //for  x

	}

	public static void main(String[] args) throws Exception {
		String hostname = "127.0.0.1";
		String portString = "8001";
		String username = "weblogic";
		String password = "weblogic";

		PrintServerState s = new PrintServerState();
		initConnection(hostname, portString, username, password);
		s.printNameAndState();
		s.printAppNameAndState();
		s.printJMS();
		connector.close();
	}
}
 

更详细的内容,可以查看weblogic相关文档http://download.oracle.com/docs/cd/E13222_01/wls/docs90/wlsmbeanref/core/index.html

 

你可能感兴趣的:(oracle,quartz,weblogic,jms,Security)