You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Campbell, Lance" <la...@illinois.edu> on 2014/07/31 18:06:11 UTC

Programmatically retrieve number of tomcat connections

Tomcat 7
Is there a way via a Java servlet to get the number of tomcat connections?


Thanks,

Lance Campbell<http://illinois.edu/person/lance>
Software Architect
Web Services at Public Affairs
217-333-0382
[University of Illinois at Urbana-Champaign logo]<http://illinois.edu/>



Re: Programmatically retrieve number of tomcat connections

Posted by "Igal @ getRailo.org" <ig...@getrailo.org>.
André -- thank you for making this clear.


On 7/31/2014 2:27 PM, André Warnier wrote:
> Caldarale, Charles R wrote:
>>> From: Igal Sapir Subject: RE: Programmatically retrieve number of 
>>> tomcat connections
>>
>>> Isn't Filter.doFilter() called once for each incoming http request?
>>
>> Yes, which is why it has nothing to do with the number of _connections_.
>>
>
> To elaborate just a little :
> Presumably when the OP wrote "connections", he meant "connections", 
> and this was not just sloppy language.
>
> In that case, consider "keep-alive" connections : a browser 
> establishes *one* connection to Tomcat, and then uses that same 
> connection to send 100 HTTP requests (and get 100 HTTP responses) over 
> that same connection.
> A filter would count 100 requests (assuming that they are all to that 
> same application), but there would still be a single connection.
> (On the other hand of course, if all the request were directed to 
> another application which does not have the filter, then the filter 
> would count 0 requests, and there would still be 1 connection).
>
> Talking about fuzzy language though, review Mark's original response : 
> which "connections" are we talking about here ? and are we talking 
> about a "snapshot", where we want to know how many connections with 
> Tomcat are active at a specific moment in time, or how many 
> connections with Tomcat have been established/closed over a period of 
> time ?
> Or about something else altogether ?
>
> And if one really wanted to count connections at the Tomcat level, I 
> would imagine that the sensible place to do this would be at the 
> Connector level, no ? (all of them)
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

-- 
Igal Sapir
Railo Core Developer
http://getRailo.org/


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Programmatically retrieve number of tomcat connections

Posted by André Warnier <aw...@ice-sa.com>.
Caldarale, Charles R wrote:
>> From: Igal Sapir [mailto:igal@getrailo.org] 
>> Subject: RE: Programmatically retrieve number of tomcat connections
> 
>> Isn't Filter.doFilter() called once for each incoming http request?
> 
> Yes, which is why it has nothing to do with the number of _connections_.
> 

To elaborate just a little :
Presumably when the OP wrote "connections", he meant "connections", and this was not just 
sloppy language.

In that case, consider "keep-alive" connections : a browser establishes *one* connection 
to Tomcat, and then uses that same connection to send 100 HTTP requests (and get 100 HTTP 
responses) over that same connection.
A filter would count 100 requests (assuming that they are all to that same application), 
but there would still be a single connection.
(On the other hand of course, if all the request were directed to another application 
which does not have the filter, then the filter would count 0 requests, and there would 
still be 1 connection).

Talking about fuzzy language though, review Mark's original response : which "connections" 
are we talking about here ? and are we talking about a "snapshot", where we want to know 
how many connections with Tomcat are active at a specific moment in time, or how many 
connections with Tomcat have been established/closed over a period of time ?
Or about something else altogether ?

And if one really wanted to count connections at the Tomcat level, I would imagine that 
the sensible place to do this would be at the Connector level, no ? (all of them)

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Programmatically retrieve number of tomcat connections

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Igal Sapir [mailto:igal@getrailo.org] 
> Subject: RE: Programmatically retrieve number of tomcat connections

> Isn't Filter.doFilter() called once for each incoming http request?

Yes, which is why it has nothing to do with the number of _connections_.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Programmatically retrieve number of tomcat connections

Posted by Igal Sapir <ig...@getrailo.org>.
Isn't Filter.doFilter() called once for each incoming http request?
On Jul 31, 2014 10:24 AM, "Caldarale, Charles R" <Ch...@unisys.com>
wrote:

> > From: Igal @ getRailo.org [mailto:igal@getrailo.org]
> > Subject: Re: Programmatically retrieve number of tomcat connections
>
> > 1) you create a class that implements the Filter interface and maintains
> > the count in an AtomicLong object.
>
> > 2) you increment the AtomicLong before the call to chain.doFilter() and
> > decrement it after that call.
>
> This has nothing to do with the number of connections.
>
> > another way I can think of is to get all the threads in the JVM with
> > Thread.getAllStackTraces(), loop over them, and check their names for
> > example.
>
> Which also has little to do with the number of connections, especially
> when using NIO.
>
> You may be able to dig the number of connections out via an internal JMX
> call.  This is, of course, Tomcat specific, but avoids trying to invoke
> Tomcat's internal classes.
>
>  - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
> MATERIAL and is thus for use only by the intended recipient. If you
> received this in error, please contact the sender and delete the e-mail and
> its attachments from all computers.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Programmatically retrieve number of tomcat connections

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Chuck,

On 7/31/14, 1:22 PM, Caldarale, Charles R wrote:
>> From: Igal @ getRailo.org [mailto:igal@getrailo.org] Subject: Re:
>> Programmatically retrieve number of tomcat connections
> 
>> 1) you create a class that implements the Filter interface and
>> maintains the count in an AtomicLong object.
> 
>> 2) you increment the AtomicLong before the call to
>> chain.doFilter() and decrement it after that call.
> 
> This has nothing to do with the number of connections.
> 
>> another way I can think of is to get all the threads in the JVM
>> with Thread.getAllStackTraces(), loop over them, and check their
>> names for example.
> 
> Which also has little to do with the number of connections,
> especially when using NIO.
> 
> You may be able to dig the number of connections out via an
> internal JMX call.  This is, of course, Tomcat specific, but avoids
> trying to invoke Tomcat's internal classes.

+1

IMO, this is the right way to do it. You don't have to use a JMX
connection, even: you can just query the local JMX server for the bean
object and interrogate it.

Lance, look at the source code to the JMXProxyServlet and you can see
how to fetch JMX beans from the server. Then, just write some code to
do the same thing for the bean you're looking for and grab the data
you want.

Or, you could just use the JMX proxy servlet directly to fetch the
data you want.

Why do you want this information from /inside/ the application, rather
than outside of it?

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIcBAEBCAAGBQJT2nzeAAoJEBzwKT+lPKRYZqkP/0y/m6XFlb689l64rHzmH9ia
pVjtPkoTGTNFNEcYbYEM1yK2n56Ty9kbzP0svhNRJPfsBghvhXOi/qG7IQ6cxTq0
skFQ+ybCXCFuUviaTUdrzYbrB+AkKRT3K9gnjOHdRetlLGCJffmDPVHiUyQekAMF
s4jHBaEJ466rrScfQtW8hMEA+S8ra9gRDkYV2h9jjwuXohqAIc6qgnDsUBlzQzgp
JceP4PjuaDr1KAONGY+tJ7fLxHwf2GLs0D7urfIRPH9dlxk3pv2O0JzzXpk2pXsX
yxm02JSQGX1jk1EbNNqzB8+eUf7coMWseRfbRGuvB2GgB4zWGeq9qFwgsl83FT1k
RpSUf6XWrXduVw9FHev5Usus/riqP71lZjZAg1EgM/DJXdXmcfIOElQrOCI0nTra
WMuB+p1XSLPf57RffsjkMy4SyqQjcryK4Z6pjYHiezYe//7hCqpMQeFhx4tIRh6g
jWSsO0ayGmurmzX0LjteYSR9ru6tFlxqQ+2taGR0ZgdCmiVwfc4CTLj7orgRojJg
XsD5K0jnyIXbvXdgdD0qfWs4eJIDBgquRX+A97qUAtoLOqBy/s9hv/OHIYfqlbbW
KOxSm2YlJ/TYm+pZOKJlHYh7W1+tErq7C+2AqqD78bytgetCYEHihYmIGRP0HpiP
zr2jw9wnMaGJcpUTPwN9
=2Vqm
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Programmatically retrieve number of tomcat connections

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Igal @ getRailo.org [mailto:igal@getrailo.org] 
> Subject: Re: Programmatically retrieve number of tomcat connections

> 1) you create a class that implements the Filter interface and maintains 
> the count in an AtomicLong object.

> 2) you increment the AtomicLong before the call to chain.doFilter() and 
> decrement it after that call.

This has nothing to do with the number of connections.

> another way I can think of is to get all the threads in the JVM with 
> Thread.getAllStackTraces(), loop over them, and check their names for 
> example.

Which also has little to do with the number of connections, especially when using NIO.

You may be able to dig the number of connections out via an internal JMX call.  This is, of course, Tomcat specific, but avoids trying to invoke Tomcat's internal classes.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Programmatically retrieve number of tomcat connections

Posted by "Igal @ getRailo.org" <ig...@getrailo.org>.
well -- Mark surely knows much better than I do -- so take into account 
what he wrote, but I was already writing this so I decided to send 
anyway, and for most application it should work fine IMO:

1) you create a class that implements the Filter interface and maintains 
the count in an AtomicLong object.

2) you increment the AtomicLong before the call to chain.doFilter() and 
decrement it after that call.

3) you add a way to get the value, either with a public method or by 
setting the value to a Servlet Attribute.

see more about Filters at 
http://www.oracle.com/technetwork/java/filters-137243.html

another way I can think of is to get all the threads in the JVM with 
Thread.getAllStackTraces(), loop over them, and check their names for 
example.


On 7/31/2014 10:01 AM, Mark Thomas wrote:
> On 31/07/2014 17:56, Campbell, Lance wrote:
>> Could you elaborate a little?  How do you actually get the number of HTTP connections?
> You can't do this with a Filter. You can determine the number of active
> requests for an application but connection != active request and
> application != Tomcat server.
>
> There is no standard Servlet API for the information you want.
>
> Mark
>
>
>>
>> Thanks,
>>
>> Lance Campbell
>> Software Architect
>> Web Services at Public Affairs
>> 217-333-0382
>>
>>
>>
>>
>> -----Original Message-----
>> From: Igal Sapir [mailto:igal@getrailo.org]
>> Sent: Thursday, July 31, 2014 11:35 AM
>> To: Tomcat Users List
>> Subject: RE: Programmatically retrieve number of tomcat connections
>>
>> Write a simple ServletFilter
>>   On Jul 31, 2014 9:33 AM, "Campbell, Lance" <la...@illinois.edu> wrote:
>>
>>> Good question.  I would like to have a servlet that would return to me
>>> the number of tomcat HTTP connections.  I know you can do this via a
>>> Linux console command.  But I would prefer to do it via a servlet.
>>>
>>>
>>> Thanks,
>>>
>>> Lance Campbell
>>> Software Architect
>>> Web Services at Public Affairs
>>> 217-333-0382
>>>
>>>
>>>
>>> -----Original Message-----
>>> From: Mark Thomas [mailto:markt@apache.org]
>>> Sent: Thursday, July 31, 2014 11:10 AM
>>> To: Tomcat Users List
>>> Subject: Re: Programmatically retrieve number of tomcat connections
>>>
>>> On 31/07/2014 17:06, Campbell, Lance wrote:
>>>> Tomcat 7
>>>>
>>>> Is there a way via a Java servlet to get the number of tomcat
>>> connections?
>>>
>>> Connections from what to what in what state?
>>>
>>> With or without non-Servlet API calls?
>>>
>>> Mark
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>>> For additional commands, e-mail: users-help@tomcat.apache.org
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>

-- 
Igal Sapir
Railo Core Developer
http://getRailo.org/


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Programmatically retrieve number of tomcat connections

Posted by Mark Thomas <ma...@apache.org>.
On 31/07/2014 17:56, Campbell, Lance wrote:
> Could you elaborate a little?  How do you actually get the number of HTTP connections?

You can't do this with a Filter. You can determine the number of active
requests for an application but connection != active request and
application != Tomcat server.

There is no standard Servlet API for the information you want.

Mark


> 
> 
> Thanks,
> 
> Lance Campbell
> Software Architect
> Web Services at Public Affairs
> 217-333-0382 
> 
> 
> 
> 
> -----Original Message-----
> From: Igal Sapir [mailto:igal@getrailo.org] 
> Sent: Thursday, July 31, 2014 11:35 AM
> To: Tomcat Users List
> Subject: RE: Programmatically retrieve number of tomcat connections
> 
> Write a simple ServletFilter
>  On Jul 31, 2014 9:33 AM, "Campbell, Lance" <la...@illinois.edu> wrote:
> 
>> Good question.  I would like to have a servlet that would return to me 
>> the number of tomcat HTTP connections.  I know you can do this via a 
>> Linux console command.  But I would prefer to do it via a servlet.
>>
>>
>> Thanks,
>>
>> Lance Campbell
>> Software Architect
>> Web Services at Public Affairs
>> 217-333-0382
>>
>>
>>
>> -----Original Message-----
>> From: Mark Thomas [mailto:markt@apache.org]
>> Sent: Thursday, July 31, 2014 11:10 AM
>> To: Tomcat Users List
>> Subject: Re: Programmatically retrieve number of tomcat connections
>>
>> On 31/07/2014 17:06, Campbell, Lance wrote:
>>> Tomcat 7
>>>
>>> Is there a way via a Java servlet to get the number of tomcat
>> connections?
>>
>> Connections from what to what in what state?
>>
>> With or without non-Servlet API calls?
>>
>> Mark
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Programmatically retrieve number of tomcat connections

Posted by "Campbell, Lance" <la...@illinois.edu>.
Could you elaborate a little?  How do you actually get the number of HTTP connections?


Thanks,

Lance Campbell
Software Architect
Web Services at Public Affairs
217-333-0382 




-----Original Message-----
From: Igal Sapir [mailto:igal@getrailo.org] 
Sent: Thursday, July 31, 2014 11:35 AM
To: Tomcat Users List
Subject: RE: Programmatically retrieve number of tomcat connections

Write a simple ServletFilter
 On Jul 31, 2014 9:33 AM, "Campbell, Lance" <la...@illinois.edu> wrote:

> Good question.  I would like to have a servlet that would return to me 
> the number of tomcat HTTP connections.  I know you can do this via a 
> Linux console command.  But I would prefer to do it via a servlet.
>
>
> Thanks,
>
> Lance Campbell
> Software Architect
> Web Services at Public Affairs
> 217-333-0382
>
>
>
> -----Original Message-----
> From: Mark Thomas [mailto:markt@apache.org]
> Sent: Thursday, July 31, 2014 11:10 AM
> To: Tomcat Users List
> Subject: Re: Programmatically retrieve number of tomcat connections
>
> On 31/07/2014 17:06, Campbell, Lance wrote:
> > Tomcat 7
> >
> > Is there a way via a Java servlet to get the number of tomcat
> connections?
>
> Connections from what to what in what state?
>
> With or without non-Servlet API calls?
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

RE: Programmatically retrieve number of tomcat connections

Posted by Igal Sapir <ig...@getrailo.org>.
Write a simple ServletFilter
 On Jul 31, 2014 9:33 AM, "Campbell, Lance" <la...@illinois.edu> wrote:

> Good question.  I would like to have a servlet that would return to me the
> number of tomcat HTTP connections.  I know you can do this via a Linux
> console command.  But I would prefer to do it via a servlet.
>
>
> Thanks,
>
> Lance Campbell
> Software Architect
> Web Services at Public Affairs
> 217-333-0382
>
>
>
> -----Original Message-----
> From: Mark Thomas [mailto:markt@apache.org]
> Sent: Thursday, July 31, 2014 11:10 AM
> To: Tomcat Users List
> Subject: Re: Programmatically retrieve number of tomcat connections
>
> On 31/07/2014 17:06, Campbell, Lance wrote:
> > Tomcat 7
> >
> > Is there a way via a Java servlet to get the number of tomcat
> connections?
>
> Connections from what to what in what state?
>
> With or without non-Servlet API calls?
>
> Mark
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

RE: Programmatically retrieve number of tomcat connections

Posted by "Campbell, Lance" <la...@illinois.edu>.
Good question.  I would like to have a servlet that would return to me the number of tomcat HTTP connections.  I know you can do this via a Linux console command.  But I would prefer to do it via a servlet.


Thanks,

Lance Campbell
Software Architect
Web Services at Public Affairs
217-333-0382 



-----Original Message-----
From: Mark Thomas [mailto:markt@apache.org] 
Sent: Thursday, July 31, 2014 11:10 AM
To: Tomcat Users List
Subject: Re: Programmatically retrieve number of tomcat connections

On 31/07/2014 17:06, Campbell, Lance wrote:
> Tomcat 7
> 
> Is there a way via a Java servlet to get the number of tomcat connections?

Connections from what to what in what state?

With or without non-Servlet API calls?

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Programmatically retrieve number of tomcat connections

Posted by Mark Thomas <ma...@apache.org>.
On 31/07/2014 17:06, Campbell, Lance wrote:
> Tomcat 7
> 
> Is there a way via a Java servlet to get the number of tomcat connections?

Connections from what to what in what state?

With or without non-Servlet API calls?

Mark


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org