Tomcat Extensions--monitoring

Source: Internet
Author: User
Tags set set tomcat server


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


A recent whim, to see if you can continue to 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 that the monitoring of the business side can be more convenient analysis and adjust the Tomcat configuration to provide better services.

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

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

To collect Tomcat data on a regular basis, you need to start a timed task to collect service information in the Tomcat startup process, and then, depending on your own needs, whether you want to save it by writing a log, or reporting it, or otherwise.

To do not invade Tomcat's own source code, you can choose to listener the way to achieve a custom listener, listening to service startup events, start data acquisition tasks, collecting 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, and you need to be aware that if you want to monitor other container, such as Host,context, then you need to place the corresponding container when you configure listener, most of the time, We are still accustomed to configuring at the server level, which is the most convenient at the server level. The individual listener of Tomcat itself, to monitor events such as Host,context, is to add listener to the Host,context under the server in turn when you hear the server event.

With a timed task of collecting Tomcat data regularly, the following is the collection of data.

First you have to get the mbean you need, 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 ());
          The catch (Exception e) {log.error ("Init failed.", e); }

To obtain tomcat performance data through 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 (o
                    Bjectname, "Currentthreadcount")); ServerInfo. Setcurrentthreadsbusy ((Integer) mbeanserver. getattribute (objectname, "currentth
                    Readsbusy ")); try {Object value = mbeanserver. getattribute (objectname, "Ke
                         Epalivecount ");
ServerInfo. Setkeepalivecount ((Integer) value);                   catch (Exception e) {//Ignore} Ob
                   Jectname grpname = null;
                    Enumeration Reqenumer =globalrequestprocessors. elements ();
                         while (Reqenumer. hasMoreElements ()) {objectname reqobjname = Reqenumer. Nextelement (); if (name. Equals (reqobjname. getKeyProperty ("name")) {grpname = Reqobjna
                        Me;
                   } if (grpname = = null) {return; } serverinfo. Setmaxtime (Long) mbeanserver. getattribute (Grpname, "Ma
                    Xtime ")); 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, "b
                    Ytesreceived "));

                    ServerInfo. Setbytessent ((Long) mbeanserver getattribute (grpname, "bytessent"));

              Store.storeinfo (ServerInfo); }

When Server.xml is configured with a custom listener, starting Tomcat, you can see the collected data, such as (here for testing, the data collected directly to write logs):

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 a lot of other information can be collected to see the specific needs.

In addition, can also use the value chain, write their own value to escalate request processing time, anomalies and so on.

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

Related content:

Tomcat Source 1: Where to start

Tomcat 2: Startup process

Tomcat Source 5: Request processing

Tomcat source--memory leak detection






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.