简体   繁体   中英

How to implement Session Timeout in Java?

We have a simple Java based application. There, we create session on receiving createSession() request from the client. Then we execute "operations" on the session and finally on receiving release(), we destroy the session object. Now I want to implement session timeout feature for the application. If the session is idle for a configurable amount of time, destroy it. I can create a dedicated thread which will wake up after certain interval of time and iterate through all the session objects. Based on the "lastAccessedTimeStamp" on each object, it would invoke release() whenever applicable. What else can be done? What are the other APIs that could be used here?

What kind of application is this? For web applications, session handling (including timeout) is done automatically by the servlet container; the timeout value is set in the web.xml configuration file.

If you want to reinvent all these wheels then yes, a separate thread that periodically (perhaps once per minute) checks all sessions and releases the timed-out ones sounds like a sensible implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM