tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marco Schmalz <ma...@schess.ch>
Subject Re: Replacement for HTTPSessionContext!?
Date Fri, 28 Sep 2001 13:28:03 GMT
Just came into my mind...

You should use the WeakReference-Object (from the java.lang.ref package) 
to prevent memorywaste, otherwise the Garbagecollector won't erase the 
timed-out Session objects.

this means:

//when a user logs in:
WeakReference sessionRef = new WeakReference(request.getSession());
Set sessions = (Set)getServletContext().getAttribute("myproj.sessions");
sessions.add(sessionRef);

//when you want to invalidate all sessions:
Iterator sessionIt = 
((Set)getServletContext().getAttribute("myproj.sessions")).iterator();
while(sessionIt.hasNext()) {
	WeakReference ref = (WeakReference)sessionIt.next();
	if(ref.get() != null) {
		HttpSession session = (HttpSession)ref.get();
		session.invalidate();
	}
}


I never realy worked with the Reference-package, but I think this should 
work.

Marco

Alex Colic wrote:

> Thanks alot all.
> 
> Marco the below code was great. It was just what I needed. I have a web
> application that talks to a VB backend. When the VB backend is shut down I
> need all sessions invalidated. I tried implementing a session listener but
> just because the VB back end goes down doesn't mean the session is
> invalidated. I needed a way to iterate through the session and kill the
> objects. The below code should work really well.
> 
> Thanks
> 
> 
> Alex
> **********************
> To: tomcat-user@jakarta.apache.org
> From: Marco Schmalz <marco@schess.ch>
> Subject: Re: Replacement for HTTPSessionContext!?
> Message-ID: <3BB44EC2.80801@schess.ch>
> 
>  > I am looking for a way to get a list of sessions and then invalidate
> them.
> 
> use a Set with all sessions in it and store it in the ServletContext.
> 
> btw, im more likely to write code than to explain it with words. voilĂ :
> 
> //create a set to store the sessions and put it into the ServletContext
> Set sessions = Collections.synchronizedSet(new HashSet());
> getServletContext().setAttribute("myproj.sessions", sessions);
> 
> //when a user logs in:
> Set sessions = (Set)getServletContext().getAttribute("myproj.sessions");
> sessions.add(request.getSession());
> 
> //when you want to invalidate all sessions:
> Iterator sessionIt =
> ((Set)getServletContext().getAttribute("myproj.sessions")).iterator();
> while(sessionIt.hasNext()) {
> 	HttpSession session = (HttpSession)sessionIt.next();
> 	session.invalidate();
> }
> 
> i hope this helps
> marco
> 
> 
> 



Mime
View raw message