前言:做了一个监控应用服务器的项目(支持Tocmat、WebSphere、WebLogic各版本), 过程也算是磕磕绊绊,由于网上缺少相关资料,或者深陷于知识的海洋难以寻觅到有效的资料,因而走过不少弯路,遇过不少困难。为了留下点印记,给后来人留下 点经验之谈,助之少走弯路,故将这些经验整理出来,与大家分享。水平有限,难免疏漏,还望指正。如有疑问,欢迎留言,或者加入Q群参与讨 论:35526521。
其实学习一项新的技术,刚开始最困难就是难以找到有效的资料,OK,首先分享几个官方文档的地址:
几个重要的官方文档地址
——相当于葵花宝典的功效(放心,不需自宫)
一、使用 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使用的JAR包
◆ 访问WebLogic 9.x所使用的JAR包:
wlclient.jar、wljmxclient.jar
在%WL_HOME%\server\lib目录下
◆ 访问WebLogic 10.x所使用的JAR包:
由于10.x的API有变化,必须使用JarBuilder Tool生成wlfullclient.jar,具体参见官方的教程,上面的“葵花宝典”里面有链接。
◆ 监控所依赖的JAR包的存放位置
我们会通过这种方式获取和MBean Server的连接:
------------------------------------------------------------------------------------------------------------------------------
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
------------------------------------------------------------------------------------------------------------------------------
代码在Eclipse下通过Java程序调用没有任何问题,作为Web应用部署到Tocmat就出问题了,会报如下异常:
------------------------------------------------------------------------------------------------------------------------------
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.jar
That 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.
------------------------------------------------------------------------------------------------------------------------------
解决办法很简单: 将wlfullclient.jar放到Tomcat安装目录下的lib目录下即可。
------------------------------------------------------------------------------------------------------------------------------
刚发现这个问题时很是诧异,始终想不通,后来在老外的一个论坛上找到类似问题,原来, JMXConnector接口是在JDK1.5/JDK1.6的rt.jar里面,而具体实现是在wlfullclient.jar里面。问题就在这里, rt.jar是由系统类加载器加载的, wlfullclient.jar如果放在自己的WEB-INFO/lib目录下,就是由WebappClassLoader去加载了。所以我们将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); Hashtableh = 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(); } /** * 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")); } } } } } } }