简体   繁体   English

如何在Java中实现会话超时?

[英]How to implement Session Timeout in Java?

We have a simple Java based application. 我们有一个简单的基于Java的应用程序。 There, we create session on receiving createSession() request from the client. 在那里,我们在接收到来自客户端的createSession()请求时创建会话。 Then we execute "operations" on the session and finally on receiving release(), we destroy the session object. 然后,我们在会话上执行“操作”,最后在接收到release()之后,销毁会话对象。 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. 基于每个对象上的“ lastAccessedTimeStamp”,它将在适用时调用release()。 What else can be done? 还有什么可以做的? What are the other APIs that could be used here? 这里还可以使用哪些其他API?

What kind of application is this? 这是什么样的应用程序? For web applications, session handling (including timeout) is done automatically by the servlet container; 对于Web应用程序,会话处理(包括超时)由Servlet容器自动完成; the timeout value is set in the web.xml configuration file. 超时值在web.xml配置文件中设置。

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. 如果要重新发明所有这些轮子,那么可以,一个单独的线程定期(也许每分钟一次)检查所有会话并释放超时的会话,这听起来很明智。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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