Other User Session isAlive?

Hi,

I inserted the session id of the user in the database… I want to know if that session is stillAlive… is there a way for that? or anything you can suggest?

All I want to know is, If the user still active or its heartbeat is alive…

Thanks.

Hi Lance,

https://vaadin.com/book/vaadin7/-/page/application.lifecycle.html

At 4.7.3 there is written how you implement a session destroy listener. So as long as the session is not destroyed the session might be active :wink:

The listener doesnt work when the computer was accidentally shutdown… or the user deletes the sessions and cookies in the browser.

Hey Lance,

thanks for the information, but are you sure? The servlet should work completly fine whatever the client is doing. At the moment when the session is destroyed the destroy listener MUST be informed.
If it isn’t informed this is a defect.

If the computer is accidently shutdown/crashes/the server process is killed sessionDestroy listener is not called(Becuase the process altogether has stopped/crashed). But things would be different if you are shutting down your app server gracefully, your session listeners would be called.

To answer your question you have to do some thing like this.

@WebListener
public class UserSessionListener implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
    System.out.println("Session Created");
    //TODO Insert a record in to database

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
    System.out.println("Session Destroyed");
    //TODO Delete a record from database
    }

}

Thanks,
Krishna.

Also, the sessionDestroyListener doesnt work when I close the browser which means… the program still assume that the user is active.

This is because there’s no way to do reliable close-detection on every browser, so the functionality is not there. See discussion at
https://vaadin.com/forum/#!/thread/1553240/
.

You can kind-of do this with the heartbeat mechanism - just set the heartbeat interval and missed heartbeat session closing values low enough - and remember to enable session closing on heartbeat failure.

Thanks for the link… :slight_smile: