Connecting Tomcat with JMX in Windows

Source: Internet
Author: User
Tags jconsole

OS: win7

JDK: 1.6

Tomcat: 7.0.26

1. the Java environment variables are correctly configured. There are a lot of online variables.

2. Modify bin \ Catalina. bat in the Tomcat directory

Find set java_opts = % java_opts % in this file. The following behavior is shown:

Rem ----- execute the requested command ---------------------------------------

Add

 
Set java_opts =-DCOM. sun. management. jmxremote-DCOM. sun. management. jmxremote. port = 9008-DCOM. sun. management. jmxremote. authenticate = false-DCOM. sun. management. jmxremote. SSL = false

Note: 9008 is the port number and will be used later. In many cases, Tomcat requires permission verification. However, if it is not used here, the connection can be successful. Therefore, if the password file is not tested, only the DCOM. sun. management. jmxremote. set authenticate to false.

3. After editing bin \ Catalina. bat, double-click it. You can simply double-click it. Do not execute it in cmd. I am running an error (I don't know why)

4. Double-click bin \ Startup. bat. You 'd better double-click to execute it. I will execute it in cmd and cannot start Tomcat.

5. Run netstat-An in cmd. You can see that port 9008 is enabled, indicating that JMX of Tomcat is successfully configured.


6. in cmd, enter jconsole (this EXE command is in C: \ Program Files \ Java \ jdk1.6.0 _ 23 \ bin, that is, under the bin of the installation directory ), you can start the jconsole management interface and enter the following content in the interface:

Service: JMX: RMI: // JNDI/RMI: // localhost: 9008/jmxrmi


Or

Localhost: 9008

You can enter the management interface. On the Management page, you can view all mbeans on the "mbeans" tab page.


Now, JMX monitoring tomcat configuration is successful!

OS: XP

JDK: 1.6

Tomcat: 6.0.29

1. the Java environment variables are correctly configured. There are a lot of online variables.

2. Modify bin \ Catalina. bat in the Tomcat directory

Find set java_opts = % java_opts % in this file. The following behavior is shown:

Rem ----- execute the requested command ---------------------------------------

Add

Set java_opts =-DCOM. sun. management. jmxremote-DCOM. sun. management. jmxremote. port = 9008-DCOM. sun. management. jmxremote. authenticate = false-DCOM. sun. management. jmxremote. SSL = false

Note: 9008 is the port number and will be used later. In many cases, Tomcat requires permission verification. However, if it is not used here, the connection can be successful. Therefore, if the password file is not tested, only the DCOM. sun. management. jmxremote. set authenticate to false.

3. After editing bin \ Catalina. bat, double-click it. You can simply double-click it. Do not execute it in cmd. I am running an error (I don't know why)

4. Double-click bin \ Startup. bat. You 'd better double-click to execute it. I will execute it in cmd and cannot start Tomcat.

5. Run netstat-An in cmd. You can see that port 9008 is enabled, indicating that JMX of Tomcat is successfully configured.


6. in cmd, enter jconsole (this EXE command is in C: \ Program Files \ Java \ jdk1.6.0 _ 23 \ bin, that is, under the bin of the installation directory ), you can start the jconsole management interface and enter the following content in the interface:

Service: JMX: RMI: // JNDI/RMI: // localhost: 9008/jmxrmi


Or

Localhost: 9008

You can enter the management interface. On the Management page, you can view all mbeans on the "mbeans" tab page.


Now, JMX monitoring tomcat configuration is successful!

InCodeUse JMX to get the mbean value in Tomcat

OS: xpjdk: 1.6 Tomcat: 6.0.291.java: The environment variables are correctly configured. There are many online variables, so I will not talk about them anymore. modify bin \ Catalina in the Tomcat directory. bat searches for set java_opts = % java_opts % in this file. The following behavior is: REM ----- execute the requested command ------------------------------------- add set java_opts =-DCOM under this line. sun. management. jmxremote-DCOM. sun. management. jmxremote. port = 9008-DCOM. sun. management. jmxremote. authenticate = false-DCOM. sun. management. jmxremote. SSL = False description: 9008 is the port number, It will be used later. In many cases, Tomcat requires permission verification. However, if it is not used here, the connection can be successful. Therefore, if the password file is not tested, only the DCOM. sun. management. jmxremote. set authenticate to false. 3. After editing bin \ Catalina. bat, double-click it. Double-click it directly. Do not execute it in cmd. I have an error in execution (I don't know why). 4. double-click bin \ Startup. bat, it is best to double-click to execute. I run it in cmd and cannot start tomcat5. execute netstat-An in cmd. You can see that port 9008 has been enabled, indicating that Tomcat's JMX configuration is successful 6. in cmd, enter jconsole (this EXE command is in C: \ Program Files \ Java \ jdk1.6.0 _ 23 \ bin, that is, under the bin of the installation directory ), you can start the jconsole management interface and enter the following content in the interface: Service: JMX: RMI: // JNDI/RMI: // localhost: 9008/jmxrmi or localhost: 9008. You can access the management interface. On the Management page, on the "mbeans" tab page, you can see all mbeans. At this time, JMX monitoring Tomcat is configured successfully! The following code uses JMX to obtain the mbean value in Tomcat import Java. util. *; import javax. management. *; import javax. management. remote. *; public class jmxtest {public static void main (string [] ARGs) {// todo auto-generated method stub try {-- the URL here is the URL jmxserviceurl = new jmxserviceurl ("service: JMX: RMI: // JNDI/RMI: // localhost: 9008/jmxrmi "); // Map<  String , String [] > Map = new hashmap (); // string [] credentials = new string [] {"monitorrole", "QED"}; // map. put ("JMX. remote. credentials ", credentials); jmxconnector conn = jmxconnectorfactory. connect (URL); system. out. println ("jmxconnector =" + Conn. tostring (); string id = Conn. getconnectionid (); system. out. println ("connection id =" + id); mbeanserverconnection mbsc = Conn. getmbeanserverconnection (); string domains [] = mbsc. getdomains (); system. out. println ("# of domains =" + domains. length); For (INT I = 0; I < Domains. length; I ++) {system. Out. println ("Domain [" + I + "] =" + domains [I]);} set < Objectinstance  > Mbeanset = mbsc. querymbeans (null, null); system. Out. println ("mbeanset. Size ():" + mbeanset. Size (); iterator <  Objectinstance  > Mbeansetiterator = mbeanset. iterator (); While (mbeansetiterator. hasnext () {objectinstance = (objectinstance) mbeansetiterator. next (); objectname = objectinstance. getobjectname (); string canonicalname = objectname. getcanonicalname (); system. out. println ("canonicalname:" + canonicalname); -- in, there is a port value of Catalina: TYPE = server 8005, run Program In the console, you can see if (objectname. tostring (). equals ("Catalina: TYPE = server") {// get details of cluster mbeans string S = mbsc. getattribute (objectname, "Port "). tostring (); system. out. println ("========================================== ==== "); system. out. println (s); system. out. println ("========================================== ==== ");} // string canonicalkeyproplist = objectname. getcanonicalkeypropertyliststring ();} Conn. close ();} catch (exception ex) {system. out. println ("illegal argument exception:" + ex );}}}

The following is the compiled code, which contains a part of the spam code that you can choose to read.

 Import Java. Math. bigdecimal; Import Java. util .*; Import Javax. Management .*; Import Javax. Management. Remote .*;Import Javax. Naming. context; Public   Class Jmxtest { Public   Static   Void Main (string [] ARGs) {jmxconnector conn = Null ; Try {Mbeanserverconnection mbsc = connjmx (conn); string keyname ="  "; String objectname ="  "; String attributename ="  "; String attributekey ="  "; String value ="  "; Objectname =" Java. LANG: TYPE = memory "; Attributename =" Nonheapmemoryusage "; Attributekey =" Init "; Value = getvaluebykey (objectname, attributename, attributekey); printcontext (attributename, value); keyname =" Process count "; Objectname =" Java. LANG: TYPE = threading "; Attributename =" Threadcount "; Attributekey ="  "; Value = getvaluebykey (mbsc, objectname, attributename, attributekey); printcontext (attributename, value); keyname =" Normal running time "; Objectname =" Java. LANG: TYPE = Runtime "; Attributename =" Uptime "; Attributekey ="  "; Value = getvaluebykey (mbsc, objectname, attributename, attributekey ); // System. Out. println (attributename + "=" + value );     // Value = convertmilltotext (long. parselong (value )); Printcontext (attributename, value );} Catch (Exception ex) {system. Out. println (" Exception: "+ Ex );} Finally { Try { If (Conn! = Null ) {Conn. Close ();}} Catch (Exception ex1 ){}}} Private   Static   Void Printcontext (string key, string value) {system. Out. println (Key +" = "+ Value );} // Switch from millisecond to hour minute   Private   Static String convertmilltotext ( Long Millsecond) Throws Exception { Try {String S ="  "; Double D = ( Double ) Millsecond /( Double (1000*60*60); bigdecimal B = New Bigdecimal (d ); Int Hour = ( Int ) Math. Floor (d ); Double Minute = B. setscale (2, bigdecimal. round_half_up). doublevalue (); If (Hour> 0) {S = hour +" Hours ";} Minute = (minute-hour) * 60; B = New Bigdecimal (minute); minute = B. setscale (0, bigdecimal. round_half_up). doublevalue (); S + = ( Int ) Minute +" Minutes "; Return S ;} Catch (Exception ex) {system. Out. println (" Exception: "+ Ex ); Throw Ex ;}}// Connect to JMX   Private   Static Mbeanserverconnection connjmx (jmxconnector conn) Throws Exception { Try {String _ value ="  "; Jmxserviceurl url = New Jmxserviceurl (" Service: JMX: IIOP: // JNDI/corbaname: 1.2@192.168.1.146: 6888 # JMX/RMI/rmiconnectorserver "); Hashmap map = New Hashmap (); string [] credentials = New String [] {" Admin "," Admin "}; Map. Put (" JMX. Remote. Credentials ", Credentials); Conn = jmxconnectorfactory. Connect (URL, MAP); string id = conn. getconnectionid (); mbeanserverconnection mbsc = conn. getmbeanserverconnection (); Return Mbsc ;} Catch (Exception ex) {system. Out. println (" Illegal argument exception: "+ Ex ); Throw Ex ;}} // Value from mbeanserverconnection   Private   Static String getvaluebykey (mbeanserverconnection mbsc, string objectname, string attributename, string attributekey) Throws Exception { Try {String _ value ="  "; Objectname _ objectname = New Objectname (objectname ); If (Attributekey. Equals ("  ") {_ Value = mbsc. getattribute (_ objectname, attributename). tostring ();} Else {Javax. management. openmbean. compositedata DATA = (javax. management. openmbean. compositedata) mbsc. getattribute (_ objectname, attributename); _ value = data. get (attributekey ). tostring ();}Return _ Value ;} Catch (Exception ex) {system. Out. println (" Illegal argument exception: "+ Ex ); Throw Ex ;}} Private   Static String getvaluebykey (string objectname, string attributename, string attributekey) Throws Exception {jmxconnector conn = Null ; Try {String _ value ="  "; Jmxserviceurl url = New Jmxserviceurl (" Service: JMX: IIOP: // JNDI/corbaname: 1.2@192.168.1.146: 6888 # JMX/RMI/rmiconnectorserver "); Hashmap map = New Hashmap (); string [] credentials = New String [] {" Admin "," Admin "}; Map. Put (" JMX. Remote. Credentials ", Credentials); Conn = jmxconnectorfactory. Connect (URL, MAP); string id = conn. getconnectionid (); mbeanserverconnection mbsc = conn. getmbeanserverconnection (); // Set <objectinstance> mbeanset = mbsc. querymbeans ("Java. LANG: TYPE = memory", null );    // Set <objectinstance> mbeanset = mbsc. querymbeans (null, null ); Objectname _ objectname = New Objectname (objectname ); If (Attributekey. Equals ("  ") {_ Value = mbsc. getattribute (_ objectname, attributename). tostring ();} Else {Javax. management. openmbean. compositedata DATA = (javax. management. openmbean. compositedata) mbsc. getattribute (_ objectname, attributename); _ value = data. get (attributekey ). tostring ();} Return _ Value ;} Catch (Exception ex) {system. Out. println ("Illegal argument exception: "+ Ex ); Throw Ex ;} Finally { If (Conn! = Null ) {Conn. Close ();}}} Private   Static   Void Testconn () Throws Exception {jmxconnector conn = Null ; Try {// Jmxserviceurl url = new jmxserviceurl ("service: JMX: RMI: // JNDI/RMI: // localhost: 9008/jmxrmi "); Jmxserviceurl url = New Jmxserviceurl (" Service: JMX: IIOP: // JNDI/corbaname: 1.2@192.168.1.146: 6888 # JMX/RMI/rmiconnectorserver "); Hashmap map = New Hashmap (); string [] credentials = New String [] {" Admin "," Admin "}; Map. Put (" JMX. Remote. Credentials ", Credentials); Conn = jmxconnectorfactory. Connect (URL, MAP); system. Out. println ("Jmxconnector = "+ Conn. tostring (); string id = conn. getconnectionid (); system. Out. println (" Connection id = "+ Id); mbeanserverconnection mbsc = conn. getmbeanserverconnection (); string domains [] = mbsc. getdomains (); system. Out. println (" # Of domains = "+ Domains. Length ); For ( Int I = 0; I <domains. length; I ++) {system. Out. println (" Domain [ "+ I +" ] = "+ Domains [I]);} set <objectinstance> mbeanset = mbsc. querymbeans ( Null ,Null ); System. Out. println (" Mbeanset. Size (): "+ Mbeanset. Size (); iterator <objectinstance> mbeansetiterator = mbeanset. iterator (); While (Mbeansetiterator. hasnext () {objectinstance = (objectinstance) mbeansetiterator. next (); objectname = objectinstance. getobjectname (); string canonicalname = objectname. getcanonicalname (); system. out. println (" Canonicalname: "+ Canonicalname ); If (Objectname. tostring (). Equals (" Catalina: TYPE = Server ")){ // Get details of cluster mbeans String S = mbsc. getattribute (objectname ," Port "). Tostring (); system. Out. println (" ========================================================== = "); System. Out. println (s); system. Out. println (" ========================================================== = ");} // String canonicalkeyproplist = objectname. getcanonicalkeypropertyliststring (); } Conn. Close ();} Catch (Exception ex) {system. Out. println (" Illegal argument exception: "+ Ex ); Throw Ex ;} Finally {If (Conn! = Null ) {Conn. Close ();}}}}

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.