Listener for Session Expiration

Greetings!

How do I listen for a session expiring? I have an application that has users, and I would like to log when they are ‘logged off’ (by their session expiring).

Override Application.close() - it will get called when the application end (either explicitly or by session expiration).

Application.close() is called when the application session ends either by session expiration or for another reason (ending it from the code). You could override it and do the logging there.

Jonas and Artur,

Thanks for the quick response.

Bill.

Is the session already ended when close() is called? That is, can I retrieve a session object (the user) or is it already gone by the time I get this call?

Should be possible.

I’m doing a project in Vaadin 7. In that I need to hide my UI(or disable access to UI) when a user session expires.

I tried overriding the

@Override
public void close() {
    // Stuff to disable UI components
}

But, Nothing has happened. Is it possible to disable(freeze) the components in my UI automatically after session expires.

import com.vaadin.server.VaadinSession;
import java.util.Enumeration;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class SessionExpireListener implements HttpSessionListener {
    
    @Override
    public void sessionCreated(HttpSessionEvent hse) {
        System.out.println("Session created");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent hse) {
        System.out.println("Session destroyed");
        Enumeration e = hse.getSession().getAttributeNames();
        while (e.hasMoreElements()) {
            Object o = hse.getSession().getAttribute((String) e.nextElement());
            if (o instanceof VaadinSession) {
                VaadinSession vs = (VaadinSession) o;
            }
        }        
    }
}

where should we add this SessionExpireListener?

IMHO, a much cleaner way to do this would be as described in the following article.

https://vaadin.com/wiki/-/wiki/Main/Cleaning+up+resources+in+a+UI

Updated link: https://vaadin.com/docs/v8/framework/articles/CleaningUpResourcesInAUI.html