首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

监控WebLogic 9.x跟10.x(监控应用服务器系列文章四)

2012-08-10 
监控WebLogic 9.x和10.x(监控应用服务器系列文章四)前言:最近在做一个监控应用服务器(Tocmat、WebSphere、We

监控WebLogic 9.x和10.x(监控应用服务器系列文章四)

前言:最近在做一个监控应用服务器(Tocmat、WebSphere、WebLogic)的项目,目前已小有规模,回头看看,一路走来,也算是磕磕绊绊,遇到过种种问题,走过不少弯路,不过程序员最不怕的就是遇到问题——有什么问题就解决什么问题。为了留下点印记,为后来人留下点经验之谈,助之少走弯路,特意把这些经验整理出来,与大家分享。水平有限,如有疏漏,还望指正。如有疑问可以Q我:562116039。

?

?

完成监控应用服务器的项目已经有一个多月了,由于只有一人独立摸索,此项工作前后持续近四个半月,虽说花费时间较长,但对完成的这个项目还是挺满意的,自己在技术上也得到了不小的进步,而且四个半月专门写纯Java,实在是爽(呵呵,不知道用啥词来形容才算合适)。


系列文章第三篇是专门探讨的WebLogic 8.x的监控,主要是新人入手都会遇到的一个问题:就是JDK版本与WebLogic版本的问题。


而监控WebLogic 9.x和10.x的文章一直没有跟进,实在抱歉!直到最近有为朋友看到我的文章通过JavaEye找到我,说怎么没有9.x和10.x相关的文章,他也正在做一个监控WebLogic的项目,想寻求一些有用的经验,于是把几个官方文档的地址给了他,告诉他有了这几个地址监控WebLogic 9.x和10.x就没有问题了。然后就想到,赶紧来把这篇文章补上,这样就可以帮到更多有同样需求的朋友了。


好了,进入正题,首先分享几个官方文档的地址:

?

几个重要的官方文档地址——葵花宝典(放心,不需自宫)

◆ 一、使用 JMX 开发自定义管理实用工具

http://www.oraclefmw.com/wls92/jmx/index.html

这是针对WebLogic 9.2的中文版,第一好东西,里面不仅讲解有原理讲解,还有Demo和教程


◆ 二、使用JMX访问WebLogic Server MBean的Demo和教程

● WebLogic 9.x:

http://download.oracle.com/docs/cd/E13222_01/wls/docs90/jmx/accessWLS.html

● WebLogic 10.x:

http://download.oracle.com/docs/cd/E11035_01/wls100/jmx/accessWLS.html


◆ 三、WebLogic的MBean参考手册

http://download.oracle.com/docs/cd/E14571_01/apirefs.1111/e13951/core/index.html

有了MBean参考手册,想要监控什么指标,直接到里面找,纯粹是体力活,剩下的就是考虑怎么把代码写得简单可用(Clean code that works)了

?

只要有了这几个地址,开发监控WebLogic 9.x和10.x的项目就变得简单了。想当年,我就是在找资料上花费了大把大把的时间,而且那绝对是相当的痛苦。如果你不是第一时间看到了这篇文章,那么你一定感同身受吧!但是恭喜你,现在你不用那么痛苦了,因为你已经看到这篇文章了,哈哈,开个玩笑……

?

访问WebLogic 9.x和WebLogic 10.x使用的JAR包

访问WebLogic?9.x所使用的JAR包:

wlclient.jar、wljmxclient.jar

在%WL_HOME%\server\lib目录下

?

访问WebLogic?10.x所使用的JAR包:

由于10.x的API有变化,必须使用JarBuilder?Tool生成wlfullclient.jar,具体参见官方的教程,上面的“葵花宝典”里面有链接。

?

监控WebLogic9/10所依赖JAR包的存放位置

监控 WebLogic9/10的 项目 (项目导入 wlfullclient.jar ,不使用自定义ClassLoader ) 部署到 Tomcat时必须将JAR包 [wlfullclient.jar]放到Tomcat安装目录下的lib目录下!
wlfullclient.jar放在应用的WEB-INF\lib目录下会报异常:Unsupported protocol: t3

?

具体原因如下:

?

Here's the issue that I'm facing:To get a JMX connection using JMXConnectionFactory using below: JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);I have to rely on classes in weblogic.jarThat is because the implementation of JMXConnector is in weblogic.jar. However, the interface/abstract class for JMXConnectionFactory lives in rt.jar that comes with JDK1.5 or JDK1.6.Because rt.jar is (probably) used by the bootstrap classloader, I HAVE to include weblogic.jar in the CATALINA_HOME/common/lib directory. If I do so, I can get the JMX part of the solution to work because both, rt.jar and weblogic.jar are loaded by the same (system) classloader.

?

我们会通过这种方式获取和MBean?Server的连接:

JMXConnector?connector?=?JMXConnectorFactory.connect(serviceURL,?h);

但是项目在Eclipse下通过Java程序调用没有任何问题,作为Web应用部署到Tocmat就出问题了,会报异常:Unsupported?protocol:?t3。你是不是也发现了这个问题?如果是,那恭喜你,哈哈,往下看。

?

解决方法很简单:将wlfullclient.jar放到Tomcat安装目录下的lib目录下即可。

?

刚发现这个问题时很是诧异,想不通,后来在老外的一个论坛上找到类似问题,原来,JMXConnector接口是在JDK1.5/JDK1.6的rt.jar里面,而具体实现是在wlfullclient.jar里面,问题就在这里,rt.jar是由bootstrap?classloader加载的,wlfullclient.jar如果放在自己的WEB-INFO/lib目录下,就不是由bootstrap?classloader加载了。所以我们把wlfullclient.jar放到Tomcat安装目录下的lib目录下,这样wlfullclient.jar跟rt.jar就是由同一个classloader加载的了,所以问题就迎刃而解了。

?

附上一个Demo,用于采集Web应用的Servlet信息
public class MonitorServlets {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());}}/** * 测试本地的WebLogic10.3 */public static void main1(String[] args) throws Exception {String hostname = "localhost";String portString = "7001";String username = "weblogic";String password = "weblogic123";MonitorServlets monitor = new MonitorServlets();initConnection(hostname, portString, username, password);monitor.getServletData();connector.close();}/** * 测试192.168.1.5服务器上的WebLogic9.2 */public static void main(String[] args) throws Exception {String hostname = "192.168.1.5";String portString = "7001";String username = "weblogic";String password = "weblogic";MonitorServlets monitor = new MonitorServlets();initConnection(hostname, portString, username, password);monitor.getServletData();connector.close();}/** * Initialize connection to the Domain Runtime MBean Server *  * @param hostname * @param portString * @param username * @param password * @throws IOException * @throws MalformedURLException */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<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();}/** * Get an array of ServerRuntimeMBeans *  * @return * @throws Exception */public static ObjectName[] getServerRuntimes() throws Exception {return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");}/** * Get an array of WebApplicationComponentRuntimeMBeans *  * @throws Exception */public void getServletData() throws Exception {ObjectName[] serverRT = getServerRuntimes();int length = (int) serverRT.length;for (int i = 0; i < length; i++) {System.out.println("===========================================ServerRuntimes");String name = (String) connection.getAttribute(serverRT[i], "Name");String state = (String) connection.getAttribute(serverRT[i],"State");System.out.println("Server name: " + name + ".Server state: "+ state);ObjectName[] appRT = (ObjectName[]) connection.getAttribute(serverRT[i], "ApplicationRuntimes");int appLength = (int) appRT.length;for (int x = 0; x < appLength; x++) {System.out.println("-------------------------------------------ApplicationRuntimes");System.out.println("Application name: "+ (String) connection.getAttribute(appRT[x], "Name"));ObjectName[] compRT = (ObjectName[]) connection.getAttribute(appRT[x], "ComponentRuntimes");int compLength = (int) compRT.length;for (int y = 0; y < compLength; y++) {String componentName = (String) connection.getAttribute(compRT[y], "Name");String componentType = (String) connection.getAttribute(compRT[y], "Type");System.out.println("  Component name: " + componentName+ " , Component type: " + componentType);if (componentType.toString().equals("WebAppComponentRuntime")) {ObjectName[] servletRTs = (ObjectName[]) connection.getAttribute(compRT[y], "Servlets");int servletLength = (int) servletRTs.length;for (int z = 0; z < servletLength; z++) {System.out.println("    Servlet name: "+ (String) connection.getAttribute(servletRTs[z], "Name"));System.out.println("       Servlet context path: "+ (String) connection.getAttribute(servletRTs[z], "ContextPath"));System.out.println("       Invocation Total Count : "+ (Object) connection.getAttribute(servletRTs[z],"InvocationTotalCount"));}}}}}}}
?

?

?

?

后记:此文本早已发表,后删掉了,然后很多人看到我写的监控WebLogic 8、9的博文,就加Q问我关于监控WebLogic 9和10的问题,所以现在把这篇博文修改下重新发上来,与大家共享。

?

热点排行