Tomcat extension--monitoring

Source: Internet
Author: User
Tags set set


(Turn around, source address: http://www.jmatrix.org/notes/1067.html)


Recently, I wondered if it was possible to continuously collect the performance information of the Tomcat server itself by adding a tomcat extension, such as the data of the thread pool, the number of requests, and so on, so as to be able to coordinate with the monitoring of the business, and make it easier to analyze and adjust the Tomcat configuration to provide better service.

This also does not require each time to connect JMX to observe the data, and IDC environment to open JMX, but also involves a variety of security issues ....

The Statusmanagerservlet in Tomcat Manager provides the current state information for the Tomcat service through JMX. I will also "copy" the code here to collect server data.

To collect Tomcat data on a regular basis, you need to start a timed task during the Tomcat startup process to collect service information and then, depending on your needs, whether it is saved by writing a log, or escalated, or otherwise.

To do not invade Tomcat itself source code, you can choose to implement a custom listener through listener, monitoring service startup events, start the Data Acquisition task, collect data regularly, such as:

public class Serverinfomonitorlifecyclelistener Implementslifecyclelistener {      private serverinfocollection Collect = new serverinfocollection ();      @Override public      void Lifecycleevent (Lifecycleevent event) {          Lifecycle Lifecycle = event. Getlifecycle ();           if (Lifecycle.after_start_event equals (EVENT. GetType ())                   && Lifecycle instanceof Server) {               Collect.startmonitor ();          }           if (Lifecycle.before_stop_event equals (EVENT. GetType ())                   && Lifecycle instanceof Server) {               Collect.stopmonitor ();}}}     

This is the server's startup event, you need to be aware that if you want to monitor other container, such as Host,context, then the configuration listener need to be placed in the corresponding container, in most cases, We are also accustomed to configuring at the server level, and indeed at the server level is the most convenient. Tomcat itself, the individual listener, want to listen to Host,context and other events is to hear the server event by the time of the server Host,context, and so add listener.

With timed tasks to collect Tomcat data on a regular basis, the following is the collection of data.

First you have to get the required mbean, such as:

Retrieve the MBean server mbeanserver = registry.getregistry (null, NULL). Getmbeanserver ();              try {//Query Thread pools threadpools. Clear ();              String onstr = "*:type=threadpool,*";              ObjectName ObjectName = new ObjectName (ONSTR);              Set set = Mbeanserver. Querymbeans (objectName, NULL);               Iterator Iterator = set. Iterator ();                    while (iterator. Hasnext ()) {ObjectInstance Oi = iterator.next ();              Threadpools. AddElement (Oi. getobjectname ());               }//Query Global Request processors globalrequestprocessors. Clear ();               Onstr = "*:type=globalrequestprocessor,*";               ObjectName = new ObjectName (ONSTR);               set = Mbeanserver. Querymbeans (objectName, NULL);               iterator = Set.iterator (); while (iterator. Hasnext ()) {ObjectInstance Oi = Iterator.next ();              Globalrequestprocessors. AddElement (Oi. getobjectname ());          }} catch (Exception e) {log.error ("Init failed.", e); }

get Tomcat performance data from an Mbean:

Enumeration enumeration = Threadpools. elements ();                   while (enumeration. hasMoreElements ()) {ObjectName ObjectName = enumeration. Nextelement ();                   String name = ObjectName. getKeyProperty ("name");                    ServerInfo serverinfo = new ServerInfo ();                    ServerInfo. SetMaxThreads ((Integer) mbeanserver. getattribute (ObjectName, "maxthreads"); ServerInfo. Setcurrentthreadcount ((Integer) mbeanserver. getattribute (Obje                    Ctname, "Currentthreadcount")); ServerInfo. Setcurrentthreadsbusy ((Integer) mbeanserver. getattribute (ObjectName, "currentthr                    Eadsbusy ")); try {Object value = mbeanserver. getattribute (ObjectName, "keep                         Alivecount ");                ServerInfo. Setkeepalivecount ((Integer) value);   } catch (Exception e) {//Ignore} ObjectName grpName = n                   ull;                    Enumeration Reqenumer =globalrequestprocessors. elements ();                         while (Reqenumer. hasMoreElements ()) {ObjectName reqobjname = Reqenumer. Nextelement (); if (name. Equals (reqobjname. getKeyProperty ("name"))) {GrpName = Reqobjname                        ;                   }} if (grpName = = null) {return; } serverinfo. Setmaxtime ((Long) mbeanserver. getattribute (GrpName, "Maxti                    Me "));                     ServerInfo. Setprocessingtime ((Long) mbeanserver. getattribute (GrpName, "processingtime"));         ServerInfo. Setrequestcount ((Integer) mbeanserver. getattribute (                     GrpName, "RequestCount"));                    ServerInfo. Seterrorcount ((Integer) mbeanserver. getattribute (GrpName, "Errorcount"); ServerInfo. Setbytesreceived ((Long) mbeanserver. getattribute (GrpName, "byte                    Sreceived "));                    ServerInfo. Setbytessent ((Long) mbeanserver. getattribute (GrpName, "bytessent"));              Store.storeinfo (ServerInfo); }

when Server.xml is configured with custom listener, launch Tomcat, you can see the collected data, such as (here for testing, the data collected directly to write the log):

Serverinfo:maxthreads:200,currentthreadcount:16,busythreadcount:6,keepalivecount:0,maxtime:6166,requestcount : 57,errorcount:0,processtime:10380,bytesrec:0,bytessend:238874

Of course, there are many other information that can be collected to see the specific needs.

In addition, you can use the value chain to write your own value to escalate request processing time, exceptions, and so on.

The underlying code can be seen on GitHub: https://github.com/jjmatrix/tomcat-extension


Related content:

Tomcat Source 1: Where to start

Tomcat Source 2: Startup process

Tomcat Source 5: Request processing

Tomcat source--memory leak detection






Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tomcat extension--monitoring

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.