You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by ga...@cummins.com on 2007/09/18 09:07:24 UTC

How do you access all sessions from a servlet in tomcat 6.0?


We have tomcat configured to allow users sessions to stay alive for 12
hours. This is because this is how they like to work, login once at the
start of the day and shutdown at the end of the day.

I have a need to track their actual activity, like a concurrent license
tool, and thought the best way of doing this would be to have a servlet
start a timer at server startup, this servlet could then poll all the
active sessions at a set interval and check the  getLastAccessedtime()
method, building up a view of the actual activity.

Am I going about this in the right way?

If I am how do I access all the currently active sessions from a servlet?
It looks like if I can gain access to the servers engine/context I could
access all sessions via the ManagerBase class but I do not know how to do
this.

Thanks Gary
_____________
This e-mail transmission and any attachments to it are intended solely for
the use of the individual or entity to whom it is addressed and may contain
confidential and privileged information.  If you are not the intended
recipient, your use, forwarding, printing, storing, disseminating,
distribution, or copying of this communication is prohibited.  If you
received this communication in error, please notify the sender immediately
by replying to this message and delete it from your computer.


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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by "Lilianne E. Blaze" <li...@tlen.pl>.
Just be aware it will get messy if the webapp is distributed.

One suggestion - don't configure the sessions for 12h
maxInactiveInterval. Keep the default 30m (or was it 15m? whatever), and
change it to 12h only after login. Otherwise guest-sessions will expire
in 12h too, and that's just wasteful.

Greetings, Lilianne E. Blaze

gary.l.johnstone@cummins.com wrote:
> We have tomcat configured to allow users sessions to stay alive for 12
> hours. This is because this is how they like to work, login once at the
> start of the day and shutdown at the end of the day.
>
> I have a need to track their actual activity, like a concurrent license
> tool, and thought the best way of doing this would be to have a servlet
> start a timer at server startup, this servlet could then poll all the
> active sessions at a set interval and check the  getLastAccessedtime()
> method, building up a view of the actual activity.
>
> Am I going about this in the right way?
>
> If I am how do I access all the currently active sessions from a servlet?
> It looks like if I can gain access to the servers engine/context I could
> access all sessions via the ManagerBase class but I do not know how to do
> this.
>
> Thanks Gary
> _____________
> This e-mail transmission and any attachments to it are intended solely for
> the use of the individual or entity to whom it is addressed and may contain
> confidential and privileged information.  If you are not the intended
> recipient, your use, forwarding, printing, storing, disseminating,
> distribution, or copying of this communication is prohibited.  If you
> received this communication in error, please notify the sender immediately
> by replying to this message and delete it from your computer.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>   


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


RE: How do you access all sessions from a servlet in tomcat 6.0?

Posted by Br...@thomson.com.
Gary,

I'm not sure how much time was spent discussing alternatives as apposed
to the question of if getting access to all sessions would work. 

You originally talked about tracking for concurrent license checking...

We currently do this type of thing - and more, like abstracting the
session from the http session so the http session can time out and
return resources without actually losing the user session, concurrent
connections versus concurrent users, etc. Furthermore we have multiple
servers running the same app so that simply holding the information
locally in the VM is not sufficient. 

We handle tracking all of this stuff via the database. Thus the
distributed nature of our app and the precise lifecycle of the
HttpSession are not so important.

The downside to all this is that you have to be careful how you track
activity via the db. How/when do you log activity? How do you write your
connection-checking queries so you don't have performance issues?
How/when do you cache stuff locally to help improve performance? How do
you efficiently write logs for usage tracking? Using the DB works but it
does take some tuning (as we found out ;-).

Brian



-->-----Original Message-----
-->From: gary.l.johnstone@cummins.com 
-->[mailto:gary.l.johnstone@cummins.com] 
-->Sent: Wednesday, September 19, 2007 5:05 AM
-->To: Tomcat Users List
-->Subject: Re: How do you access all sessions from a servlet 
-->in tomcat 6.0?
-->
-->
-->Thanks for all the feedback on this.
-->
-->___________________________
-->
-->Gary Johnstone
-->Engineering IT
-->Cummins Turbo Technologies Ltd
-->+44 1484 440532
-->
-->
-->
-->
-->
-->
-->                                                             
-->              
-->             Lyallex                                         
-->              
-->             <lyallex@gmail.co                               
-->              
-->             m>                                              
-->           To 
-->                                       "Tomcat Users List"   
-->              
-->             19/09/2007 10:38          
--><us...@tomcat.apache.org>           
-->                                                             
-->           cc 
-->                                                             
-->              
-->             Please respond to                               
-->      Subject 
-->               "Tomcat Users           Re: How do you access 
-->all sessions  
-->                   List"               from a servlet in 
-->tomcat 6.0?       
-->             <users@tomcat.apa                               
-->              
-->                 che.org>                                    
-->              
-->                                                             
-->              
-->                                                             
-->              
-->                                                             
-->              
-->                                                             
-->              
-->
-->
-->
-->
-->OK, for some reason I've been obsessing about this for a 
-->whole day now.
-->
-->If you hold an external reference to a Session then 
-->according to my tests the session will still time out as 
-->expected but the external reference will be non null. At the 
-->very least this means that you may end up with a large 
-->number of useless references taking up space in memory. Of 
-->course you can always remove an invalid or timed out 
-->reference in the sessionDestroyed method of your listener.
-->
-->There are a whole bunch of other issues surrounding this but 
-->I'm sure you've sussed them out for yourself already.
-->
-->Anyway, I'll shut up now.
-->
-->Rgds
-->Duncan
-->
-->
-->On 9/19/07, Lyallex <ly...@gmail.com> wrote:
-->> On 9/18/07, Lyallex <ly...@gmail.com> wrote:
-->> > How about creating a SessionListener
-->> >
-->> > class SomeSessionListener implements HttpSessionListener ...
-->> >
-->> > Register it in web.xml
-->> >
-->> > in the sessionCreated method of your listener get a 
-->reference to the 
-->> > new session from the HttpSessionEvent you can now access the 
-->> > getLastAccessedTime(), maybe store the refs in some singleton ...
-->>
-->> ...er, actually I think this could be a REALLY STUPID idea 
-->as I hadn't 
-->> thought about what happens if you maintain an external 
-->reference to a 
-->> session and the session expires...
-->>
-->> Investigating now
-->>
-->> Duncan
-->>
-->
-->---------------------------------------------------------------------
-->To start a new topic, e-mail: users@tomcat.apache.org To 
-->unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
-->For additional commands, e-mail: users-help@tomcat.apache.org
-->
-->
-->
-->_____________
-->This e-mail transmission and any attachments to it are 
-->intended solely for the use of the individual or entity to 
-->whom it is addressed and may contain confidential and 
-->privileged information.  If you are not the intended 
-->recipient, your use, forwarding, printing, storing, 
-->disseminating, distribution, or copying of this 
-->communication is prohibited.  If you received this 
-->communication in error, please notify the sender immediately 
-->by replying to this message and delete it from your computer.
-->
-->
-->---------------------------------------------------------------------
-->To start a new topic, e-mail: users@tomcat.apache.org To 
-->unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
-->For additional commands, e-mail: users-help@tomcat.apache.org
-->
-->

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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by ga...@cummins.com.
Thanks for all the feedback on this.

___________________________

Gary Johnstone
Engineering IT
Cummins Turbo Technologies Ltd
+44 1484 440532






                                                                           
             Lyallex                                                       
             <lyallex@gmail.co                                             
             m>                                                         To 
                                       "Tomcat Users List"                 
             19/09/2007 10:38          <us...@tomcat.apache.org>           
                                                                        cc 
                                                                           
             Please respond to                                     Subject 
               "Tomcat Users           Re: How do you access all sessions  
                   List"               from a servlet in tomcat 6.0?       
             <users@tomcat.apa                                             
                 che.org>                                                  
                                                                           
                                                                           
                                                                           
                                                                           




OK, for some reason I've been obsessing about this for a whole day now.

If you hold an external reference to a Session then according to my
tests the session will still time out as expected but the external
reference will be non null. At the very least this means that you may
end up with a large number of useless references taking up space in
memory. Of course you can always remove an invalid or timed out
reference in the sessionDestroyed method of your listener.

There are a whole bunch of other issues surrounding this but I'm sure
you've sussed them out for yourself already.

Anyway, I'll shut up now.

Rgds
Duncan


On 9/19/07, Lyallex <ly...@gmail.com> wrote:
> On 9/18/07, Lyallex <ly...@gmail.com> wrote:
> > How about creating a SessionListener
> >
> > class SomeSessionListener implements HttpSessionListener ...
> >
> > Register it in web.xml
> >
> > in the sessionCreated method of your listener get a reference to the
> > new session from the HttpSessionEvent you can now access the
> > getLastAccessedTime(), maybe store the refs in some singleton ...
>
> ...er, actually I think this could be a REALLY STUPID idea as I hadn't
> thought about what happens if you maintain an external reference to a
> session and the session expires...
>
> Investigating now
>
> Duncan
>

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



_____________
This e-mail transmission and any attachments to it are intended solely for
the use of the individual or entity to whom it is addressed and may contain
confidential and privileged information.  If you are not the intended
recipient, your use, forwarding, printing, storing, disseminating,
distribution, or copying of this communication is prohibited.  If you
received this communication in error, please notify the sender immediately
by replying to this message and delete it from your computer.


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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by Lyallex <ly...@gmail.com>.
On 9/19/07, David Delbecq <de...@oma.be> wrote:
> Just use WeakReference :)

Er, well that's OK, the  WeakReference referant object (the session)
is null after gc
but now I have a WeakReference object lurking in my Map as opposed to
the HttpSession object previously so I'm not really gaining anything.
Probably best to remove the K, V  pair when the session is destroyed.

> > Anyway, I'll shut up now.

I wish.

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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by David Delbecq <de...@oma.be>.
Just use WeakReference :)

Lyallex a écrit :
> OK, for some reason I've been obsessing about this for a whole day now.
>
> If you hold an external reference to a Session then according to my
> tests the session will still time out as expected but the external
> reference will be non null. At the very least this means that you may
> end up with a large number of useless references taking up space in
> memory. Of course you can always remove an invalid or timed out
> reference in the sessionDestroyed method of your listener.
>
> There are a whole bunch of other issues surrounding this but I'm sure
> you've sussed them out for yourself already.
>
> Anyway, I'll shut up now.
>
> Rgds
> Duncan
>
>
> On 9/19/07, Lyallex <ly...@gmail.com> wrote:
>   
>> On 9/18/07, Lyallex <ly...@gmail.com> wrote:
>>     
>>> How about creating a SessionListener
>>>
>>> class SomeSessionListener implements HttpSessionListener ...
>>>
>>> Register it in web.xml
>>>
>>> in the sessionCreated method of your listener get a reference to the
>>> new session from the HttpSessionEvent you can now access the
>>> getLastAccessedTime(), maybe store the refs in some singleton ...
>>>       
>> ...er, actually I think this could be a REALLY STUPID idea as I hadn't
>> thought about what happens if you maintain an external reference to a
>> session and the session expires...
>>
>> Investigating now
>>
>> Duncan
>>
>>     
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>   

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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by Lyallex <ly...@gmail.com>.
OK, for some reason I've been obsessing about this for a whole day now.

If you hold an external reference to a Session then according to my
tests the session will still time out as expected but the external
reference will be non null. At the very least this means that you may
end up with a large number of useless references taking up space in
memory. Of course you can always remove an invalid or timed out
reference in the sessionDestroyed method of your listener.

There are a whole bunch of other issues surrounding this but I'm sure
you've sussed them out for yourself already.

Anyway, I'll shut up now.

Rgds
Duncan


On 9/19/07, Lyallex <ly...@gmail.com> wrote:
> On 9/18/07, Lyallex <ly...@gmail.com> wrote:
> > How about creating a SessionListener
> >
> > class SomeSessionListener implements HttpSessionListener ...
> >
> > Register it in web.xml
> >
> > in the sessionCreated method of your listener get a reference to the
> > new session from the HttpSessionEvent you can now access the
> > getLastAccessedTime(), maybe store the refs in some singleton ...
>
> ...er, actually I think this could be a REALLY STUPID idea as I hadn't
> thought about what happens if you maintain an external reference to a
> session and the session expires...
>
> Investigating now
>
> Duncan
>

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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by Lyallex <ly...@gmail.com>.
On 9/18/07, Lyallex <ly...@gmail.com> wrote:
> How about creating a SessionListener
>
> class SomeSessionListener implements HttpSessionListener ...
>
> Register it in web.xml
>
> in the sessionCreated method of your listener get a reference to the
> new session from the HttpSessionEvent you can now access the
> getLastAccessedTime(), maybe store the refs in some singleton ...

...er, actually I think this could be a REALLY STUPID idea as I hadn't
thought about what happens if you maintain an external reference to a
session and the session expires...

Investigating now

Duncan

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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by ga...@cummins.com.
Thanks Duncan after a quick read on session listeners that looks like it
will do the trick.

Gary

___________________________

Gary Johnstone
Engineering IT
Cummins Turbo Technologies Ltd
+44 1484 440532




_____________
This e-mail transmission and any attachments to it are intended solely for
the use of the individual or entity to whom it is addressed and may contain
confidential and privileged information.  If you are not the intended
recipient, your use, forwarding, printing, storing, disseminating,
distribution, or copying of this communication is prohibited.  If you
received this communication in error, please notify the sender immediately
by replying to this message and delete it from your computer.


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


Re: How do you access all sessions from a servlet in tomcat 6.0?

Posted by Lyallex <ly...@gmail.com>.
How about creating a SessionListener

class SomeSessionListener implements HttpSessionListener ...

Register it in web.xml

in the sessionCreated method of your listener get a reference to the
new session from the HttpSessionEvent you can now access the
getLastAccessedTime(), maybe store the refs in some singleton
accessible from the context and so some stuff in your TimerTask ...

Just an idea if you like messing about with stuff, nothing to do with
ManagerBase though so I bet there is a better way of doing it.

Rgds
Duncan

On 9/18/07, gary.l.johnstone@cummins.com <ga...@cummins.com> wrote:
>
>
> We have tomcat configured to allow users sessions to stay alive for 12
> hours. This is because this is how they like to work, login once at the
> start of the day and shutdown at the end of the day.
>
> I have a need to track their actual activity, like a concurrent license
> tool, and thought the best way of doing this would be to have a servlet
> start a timer at server startup, this servlet could then poll all the
> active sessions at a set interval and check the  getLastAccessedtime()
> method, building up a view of the actual activity.
>
> Am I going about this in the right way?
>
> If I am how do I access all the currently active sessions from a servlet?
> It looks like if I can gain access to the servers engine/context I could
> access all sessions via the ManagerBase class but I do not know how to do
> this.
>
> Thanks Gary
> _____________
> This e-mail transmission and any attachments to it are intended solely for
> the use of the individual or entity to whom it is addressed and may contain
> confidential and privileged information.  If you are not the intended
> recipient, your use, forwarding, printing, storing, disseminating,
> distribution, or copying of this communication is prohibited.  If you
> received this communication in error, please notify the sender immediately
> by replying to this message and delete it from your computer.
>
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

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