Invalidate session on specific page
sykwitit Oct 20, 2010 5:02 PMHi Seam users,
I am new to Seam and I have a question about invalidating a session on a specific page (in this case: a 'too busy-page'). Below you'll find an example of the sessionlistener i use.
/**
* Counts active sessions in the application and throws
* <tt>TooBusyException</tt> when there are more users than a certain threshold.
*
*/
public class SessionListener implements HttpSessionListener, ServletContextListener {
private int maximumNumberOfUsers;
private static int numberOfUserSessions;
/**
* {@inheritDoc}
*/
@Override
public void contextDestroyed(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
/**
* {@inheritDoc}
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
// TODO Auto-generated method stub
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void sessionCreated(HttpSessionEvent event) {
Lifecycle.beginCall();
numberOfUserSessions++;
ApplicatieInstellingenService applicatieInstellingenService = (ApplicatieInstellingenService) Component.getInstance("applicatieInstellingenService");
maximumNumberOfUsers = applicatieInstellingenService.findAllApplicatieInstellingen().get(0).getMaximumAantalBezoekers();
if (numberOfUserSessions > maximumNumberOfUsers) {
System.out.println("=====================> Too many users, counter: " + numberOfUserSessions);
event.getSession().setMaxInactiveInterval(-1);
sessionDestroyed(event);
event.getSession().setAttribute("tooBusy", true);
}
System.out.println("=====================> Session created, counter: " + numberOfUserSessions);
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void sessionDestroyed(HttpSessionEvent event) {
if (numberOfUserSessions > 0) {
numberOfUserSessions--;
}
System.out.println("=====================> Session destroyed, counter: " + numberOfUserSessions);
}
/**
* {@inheritDoc}
*/
@Observer(
{ "MaxSessionsUpdated" })
public void log(int maximumAantalBezoekers) {
this.maximumNumberOfUsers = maximumAantalBezoekers;
}
/**
* {@inheritDoc}
*/
public static int getNumberOfUserSessions() {
return numberOfUserSessions;
}
}
In this example I check if there are more sessions than a certain threshold(maximumNumberOfUsers). If this condition is true I redirect to a toobusy-page. Right now I use sessionDestroyed(event)
to invalidate the session on the toobusy-page. I don't think this is the right solution. I have tried session.invalidate() and httpsession.invalidate(), but both didn't invalidate the session on the toobusy-page. What is the best way to invalidate the session on the toobusy-page? Thanks!