如何使用tomcat的jmx服务

来源:互联网 发布:member.php 编辑:程序博客网 时间:2024/06/05 07:13

1 基本信息

摘要:本文介绍了如何查看、使用Tomcat的JMX服务,并调用Tomcat的JMX,停止和启动Web应用。

作者:陈光耀

2 正文

  Tomcat 5.5.20可以使用JMX服务进行管理操作。下面介绍如何查看Tomcat提供哪些JMX服务,并如何使用这些JMX服务。

  1. 使用JDK1.5自带的JConsole程序查看Tomcat的JMX服务

  要让JConsole能查看到Tomcat的JMX服务,需要Tomcat启动一个管理口。由于tomcat5.5.20缺省不提供bat启动文件(提供exe启动文件),因此需要自己写一个启动脚本。在tomcat5.5.20/bin目录下建立一个run.bat文件,加入下面内容:

  1. set CURRENT_DIR=%cd%
  2. set CATALINA_BASE=%cd ..%
  3.  
  4. set JAVA_OPTS=%JAVA_OPTS% -Dcom.sun.management.jmxremote.port=1090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file="%CATALINA_BASE%/conf/logging.properties"
  5.  
  6. "D:/jre1.5.0_10/bin/java.exe" %JAVA_OPTS% -Xms256m -Xmx512m -Duser.dir="%CURRENT_DIR%" -jar "D:/JavaEEServer/tomcat/AdminServer/bin/bootstrap.jar" start

  运行run.bat启动Tomcat5.5.20。

  再运行jdk1.5的jconsole程序

d:/jdk1.5/bin/jconsole 2512          (2512 是tomcat的进程号)

  即可进入tomcat的jmx服务的管理界面,可以看到Tomcat提供的所有jmx的mbean service。

  2. 在tomcat 的web应用上用jsp查看tomcat的web应用的状态信息

  建立一个jmxdemo.jsp文件,如下,部署到webapps的应用目录中。  在浏览器上打入 http://localhost:8080/jmxdemo.jsp, 可以查看所有web应用的状态信息

  1. <%@page contentType="text/plain;charset=GBK"%>
  2. <%@page import="java.util.Iterator"%>
  3. <%@page import="java.util.Set"%>
  4. <%@page import="javax.management.MBeanServerFactory"%>
  5. <%@page import="javax.management.MBeanServer"%>
  6. <%@page import="javax.management.ObjectName"%>
  7. <%@page import="javax.management.MBeanInfo"%>
  8. <%@page import="javax.management.MBeanAttributeInfo"%>
  9. <%
  10. out.clear();
  11.  
  12. MBeanServer mBeanServer = null;
  13. if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
  14. mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(
  15. null).get(0);
  16. } else {
  17. mBeanServer = MBeanServerFactory.createMBeanServer();
  18. }
  19.  
  20. Set names = null;
  21. try {
  22. names = mBeanServer.queryNames(new ObjectName(
  23. "*:j2eeType=WebModule,*"), null);
  24. out.println("OK - Number of results: " + names.size());
  25. out.println();
  26. } catch (Exception e) {
  27. out.println("Error - " + e.toString());
  28. return;
  29. }
  30.  
  31. Iterator it = names.iterator();
  32. while (it.hasNext()) {
  33. ObjectName oname = (ObjectName) it.next();
  34. out.println("Name: " + oname.toString());
  35.  
  36. try {
  37. MBeanInfo minfo = mBeanServer.getMBeanInfo(oname);
  38. // can't be null - I thinl
  39. String code = minfo.getClassName();
  40. if ("org.apache.commons.modeler.BaseModelMBean"
  41. .equals(code)) {
  42. code = (String) mBeanServer.getAttribute(oname,
  43. "modelerType");
  44. }
  45. out.println("modelerType: " + code);
  46.  
  47. MBeanAttributeInfo attrs[] = minfo.getAttributes();
  48. Object value = null;
  49.  
  50. for (int i = 0; i < attrs.length; i++) {
  51. if (!attrs[i].isReadable())
  52. continue;
  53. //if( ! isSupported( attrs[i].getType() )) continue;
  54. String attName = attrs[i].getName();
  55. if (attName.indexOf("=") >= 0
  56. || attName.indexOf(":") >= 0
  57. || attName.indexOf(" ") >= 0) {
  58. continue;
  59. }
  60.  
  61. try {
  62. value = mBeanServer.getAttribute(oname, attName);
  63. } catch (Throwable t) {
  64. System.out.println("Error getting attribute "
  65. + oname + " " + attName + " "
  66. + t.toString());
  67. continue;
  68. }
  69. if (value == null)
  70. continue;
  71. if ("modelerType".equals(attName))
  72. continue;
  73. String valueString = value.toString();
  74. //out.println( attName + ": " + escape(valueString));
  75. out.println(attName + ": " + valueString);
  76. }
  77. } catch (Exception e) {
  78.  
  79. }
  80. out.println();
  81. }
  82. %>

 

  3. 调用Tomcat的JMX服务,如停止、启动web应用

  写一个JavaBean,用来调用Tomcat的JMX服务,关键方法如下:

  1. public static boolean callWebModuleMBeanMethod(String appName,String methodName) throws Exception{
  2. MBeanServer mBeanServer = null;
  3.  
  4. if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
  5. mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(
  6. null).get(0);
  7. } else {
  8. throw new Exception("cann't find catalina MBeanServer");
  9. }
  10.  
  11. Set names = null;
  12. try {
  13. names = mBeanServer.queryNames(new ObjectName(
  14. "*:j2eeType=WebModule,name=//localhost/"+appName+",*"), null);
  15. } catch (Exception e) {
  16. throw new Exception("cann't find "+appName+ " web moudule mbean! can't undeploy web app./n"+e.getMessage());
  17. }
  18. if(names==null || names.size()==0) {
  19. log.debug("can't find "+appName+ " web moudule mbean!");
  20. return false;
  21. }
  22.  
  23. ObjectName oname =null;
  24. Iterator it = names.iterator();
  25. if (it.hasNext()) {
  26. oname=(ObjectName) it.next();
  27. }
  28.  
  29. if(oname==null)
  30. return false;
  31. try {
  32. mBeanServer.invoke(oname,methodName,null,null);
  33. return true;
  34. } catch (Exception e) {
  35. throw new Exception("can't "+methodName+" "+appName+ " web application!/n"+e.getMessage());
  36. }
  37. }
  38.  
  39. public static void main(String[] args){
  40. callWebModuleMBeanMethod("app1","stop"); //停止web应用app1
  41. callWebModuleMBeanMethod("app1","start"); //启动web应用app1
  42. }


 
原创粉丝点击