tomcat如何使用jmx查看webapp运行情况呢?
下文笔者讲述查看tomcat中webapp的运行情况简介说明,如下所示
在Tomcat5中,可以在bin目录下找点jmx.jar,4.1.X版本,server/lib/mx4j-jmx.jar
使用以下代码,即可输出webapp的运行情况
实现思路: 借助JMX即可实现查看tomcat中程序的运行情况例:
在Tomcat5中,可以在bin目录下找点jmx.jar,4.1.X版本,server/lib/mx4j-jmx.jar
使用以下代码,即可输出webapp的运行情况
<%@page contentType=”text/plain;charset=GBK”%> <%@page import=”java.util.Iterator”%> <%@page import=”java.util.Set”%> <%@page import=”javax.management.MBeanServerFactory”%> <%@page import=”javax.management.MBeanServer”%> <%@page import=”javax.management.ObjectName”%> <%@page import=”javax.management.MBeanInfo”%> <%@page import=”javax.management.MBeanAttributeInfo”%> <% out.clear(); MBeanServer mBeanServer = null; if(MBeanServerFactory.findMBeanServer(null).size() > 0){ mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0); }else{ mBeanServer = MBeanServerFactory.createMBeanServer(); } Set names = null; try { names=mBeanServer.queryNames(new ObjectName(“*:j2eeType=WebModule,*”), null); out.println(“OK – Number of results: ” + names.size()); out.println(); } catch (Exception e) { out.println(“Error – ” + e.toString()); return; } Iterator it=names.iterator(); while( it.hasNext()) { ObjectName oname=(ObjectName)it.next(); out.println( “Name: ” + oname.toString()); try { MBeanInfo minfo=mBeanServer.getMBeanInfo(oname); // can’t be null – I thinl String code=minfo.getClassName(); if (“org.apache.commons.modeler.BaseModelMBean”.equals(code)) { code=(String)mBeanServer.getAttribute(oname, “modelerType”); } out.println(“modelerType: ” + code); MBeanAttributeInfo attrs[]=minfo.getAttributes(); Object value=null; for( int i=0; i< attrs.length; i++ ) { if( ! attrs[i].isReadable() ) continue; //if( ! isSupported( attrs[i].getType() )) continue; String attName=attrs[i].getName(); if( attName.indexOf( “=”) >=0 || attName.indexOf( “:”) >=0 || attName.indexOf( ” “) >=0 ) { continue; } try { value=mBeanServer.getAttribute(oname, attName); } catch( Throwable t) { System.out.println(“Error getting attribute ” + oname + ” ” + attName + ” ” + t.toString()); continue; } if( value==null ) continue; if( “modelerType”.equals( attName)) continue; String valueString=value.toString(); //out.println( attName + “: ” + escape(valueString)); out.println( attName + “: ” + valueString); } } catch (Exception e) { } out.println(); } %>
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。