How to use the JMX service for Tomcat

Source: Internet
Author: User
Tags getmessage tostring tomcat jconsole

1 Basic Information

Summary: This article describes how to view, use the JMX services of Tomcat, and invoke Tomcat's JMX to stop and start the Web App.

Author: Chen Guangyao

2 Body

Tomcat 5.5.20 can use the JMX service for administrative operations. Here's how to see what JMX services Tomcat provides and how to use them.

1. View Tomcat's JMX services using JDK1.5 's own Jconsole program

To enable Jconsole to see Tomcat's JMX service, Tomcat is required to start a management port. Since tomcat5.5.20 does not provide a bat boot file by default (providing EXE startup files), you need to write a startup script yourself. Create a Run.bat file in the Tomcat5.5.20/bin directory and add the following: Set current_dir=%cd% set CATALINA_BASE=%CD. % 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" "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 Run.bat to start Tomcat5.5.20.

Run the jdk1.5 jconsole program again

D:/jdk1.5/bin/jconsole 2512 (2512 is Tomcat's process number)

You can access the management interface of Tomcat's JMX service and see all of the JMX's Mbean services provided by Tomcat.

2.   Use JSP to view the status information for Tomcat Web apps on tomcat Web apps

  Create a jmxdemo.jsp file, as below, to deploy to the WebApps application directory. Enter http://localhost:8080/jmxdemo.jsp on your browser to view status information for all Web Apps <% @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.getclassnam E (); 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 Type ())) 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 a Ttribute "+ 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 (); }%>

3. Call Tomcat's JMX service, such as Stop, start web App

  Write a JavaBean, which is used to invoke the JMX service of Tomcat, with the following key methods: public static Boolean Callwebmodulembeanmethod (String appname,string methodName ) throws exception{Mbeanserver mbeanserver = null;   if (Mbeanserverfactory.findmbeanserver (null). Size () > 0) {m Beanserver = (mbeanserver) mbeanserverfactory.findmbeanserver (null). Get (0); } else {throw new Exception ("Cann ' t find Catalina Mbeanserver");}   Set names = null; try {names = Mbeanserver.querynames (new ObjectName ("*:j2eetype=webmodule,name=//localhost/" +appname+ ", *"), null);} catch (Exception e) {throw new Exception ("Cann ' t find" +appname+ "Web Moudule mbean! Can ' t undeploy Web app./n "+e.getmessage ()); } if (Names==null | | names.size () ==0) {log.debug ("can ' t find" +appname+ "Web Moudule mbean!"); return false; }   ObjectName oname =null; Iterator it = Names.iterator (); if (It.hasnext ()) {oname= (ObjectName) It.next ();}   if (oname==null) return false; try {mbeanserver.invoke (oname,methodname,null,null); return true;} catch (EXception e) {throw new Exception ("Can ' t" +methodname+ "" +appname+ "Web application!/n" +e.getmessage ())}}   public static void main (string[] args) {Callwebmodulembeanmethod ("App1", "Stop");//Stop web App App1 Callwebmodulembeanmethod ("App1", "start"); Launch Web App App1}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.