11

I have a Java/Java EE web application deployed on Tomcat Server 5.5.17. I want to know the number of clients which are connected to the server. How can we find it out?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Thunderhashy
  • 5,291
  • 13
  • 43
  • 47

3 Answers3

21

Most reliable way would be to search for ip.addr.of.srv:port in netstat. Here's the Windows based example (sorry, no Linux guru here ;) )

netstat -np tcp | find "12.34.56.78:80"

Replace 12.34.56.78 by IP where Tomcat listens on and 80 by port where Tomcat listens on.

This is actually not a programming problem, hence I voted to migrate this question to serverfault.com.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    I use Apache Geronimo and it displays in the web console the number of current busy threads (not sessions) so it is programmable. For your solution, you should have netstat -np tcp | find "12.34.56.78:80" | find "ESTABLISHED" so you don't include non-established states like FIN_WAIT, SYN_SENT, etc. – ericp Feb 06 '10 at 03:47
  • count(threads) != count(connections). There may be means of a threadpool and/or threads used for other purposes (daemons, backgroundtasks, cleanup, etc). As to your netstat suggestions, this makes indeed more sense. – BalusC Feb 06 '10 at 03:52
  • 5
    I accomplished this in CentOS with ````netstat -an | grep :80 | wc -l```` as described in this [article](https://www.exchangecore.com/blog/find-number-active-connections-linux-using-netstat/) – AntonioOtero Feb 24 '15 at 18:32
4

And if you need to figure to what each connection is doing , use this on linux

netstat -an | grep :8080 | awk '{print $6}'

If there are three connections , you will see

LISTEN TIME_WAIT TIME_WAIT

And if you only want to count connections which are in TIME_WAIT state

netstat -an | grep :8080 | grep TIME_WAIT | wc -l
vsingh
  • 6,365
  • 3
  • 53
  • 57
3

See the section under Tomcat Manager for an example of counting the sessions in a webapp.

Counting the number of connections is probably a bit harder. Tomcat starts a new thread for each request coming in up to a maximum of maxProcessors. Beyond this number, the requests are queued up to a maximum of acceptCount. Requests beyond this number are refused/dropped (or crashes, I am not sure). The properties can be monitored using a JConsole: Steps here. The specific properties mentioned above are properties of the HTTP Connector.

EDIT 1:

After looking through source code of CoyoteConnector and AJP Connector, there is a private property called curProcessors which tracks the number of processors currently in use. However, adding the curProcessors variable to the mbeans file for connectors does not seem to display the current value in the JConsole display.

Note: The mbeans XML file that I modified was in tomcat\server\lib\catalina.jar and is in the org\apache\catalina\connector directory in the jar. Below is an example of the entry I added:

<attribute   name="curProcessors"
    description="the number of processors currently in use"
    type="int"/>
Gary
  • 13,303
  • 18
  • 49
  • 71
Thimmayya
  • 2,064
  • 2
  • 18
  • 20
  • Can you tell me the difference between number of sessions and number of connections? I think I need to know the number of connections to a tomcat server. – Thunderhashy Feb 05 '10 at 22:32
  • 1
    I am quoting from the API docs- Session - Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. The session persists for a specified time period, across more than one connection or page request from the user. Basically, a user can have 1 session and multiple connections for different requests. Or, if you consider a page which is loading different sections using AJAX requests, loading each section would create a different connection to the server while using the same session (if any). – Thimmayya Feb 05 '10 at 23:18