如何设置用户活动的计时器? [英] How to set a timer for activity of users?
问题描述
如果用户在5个小时内停止工作,我需要执行一种特定的方法.
I need to execute a specific method if user stop working in 5 hours.
可以说用户已登录,但是他没有在5个小时内将任何记录添加到数据库的特定表中.
Lets say user is signed in, but he does not add any record to a specific table of database for 5 hours.
每次用户将记录添加到指定表时,都应重置该特定用户的计时器,否则它将继续运行.
Any time user adds a record to the specified table, the timer of that particular user should be reset otherwise it will keep going.
如果达到5个小时,应用程序应显示一条消息,表明您5个小时未添加任何记录.
If it reaches to 5 hours , application should show a message indicating that you have not added any record for 5 hours.
为澄清起见,请查看以下步骤
To clarify please have a look at these steps
1- (1:00 PM) user1 is signed in -> timer of user1 is set
3- (1:00 PM) user2 is signed in -> timer of user2 is set
5- (1:10 PM) user1 adds something to the tableA
-> timer of user1 will be reset
6- (6:00 PM) user1 adds something to the tableA
-> timer of user1 will be reset
7- (6:00 PM) 5 hours is elapsed for user2 without adding any record to tableA
-> show the message to user2
8- (11:10 PM) 5 hours is elapsed for user1 without adding a record to TableA
-> show the message to user1
....
- 如上所示,我不在乎登录时会做什么.我只是在乎将记录添加到该特定表中. (表A)例如,用户2处于活动状态,但未添加任何 记录到tableA中,因此一旦计时器达到5个小时,就会收到消息.
- As shown above I do not care what they do while being signed in. I just care about adding records to that specific table. (TableA) For example user 2 is active but does not add any record to tableA so receives the message once his timer reaches to 5 hours.
除以下内容外,我发现了以下内容,但假定它没有用,因为一旦在用户的类中运行它,就不可能回到相同的类并对其进行重置.
I've found the following except, but suppose it is not useful, as once I run it in user's class, it is not possible to get back to the same class and reset it.
Timer timer = new Timer();
int startingTime=10000; //millisecond 10 seconds=10000
int delayTime=1000; // millisecond 1 second
timer.schedule(new TimerTask()
{
public void run() {
System.out.println("Timer repeat statement");
}
},startingTime,delayTime);
推荐答案
由于您使用的是java-ee和struts,因此我认为您正在谈论的是Web应用程序.
Since you use java-ee and struts, I assume you are talking about web-app.
因此,请使用标准的会话管理.实现HttpSessionActivationListener,以在会话启动时初始化计时器,并将会话超时设置为5小时.当会话过期时,运行您的代码.
So use standard session management. Implement HttpSessionActivationListener to init the timer on session start, and set your session time out to 5 hours. When session expired, run you code.
您需要实现session-scope bean进行检查,并在执行到特定表的记录时使用从您的服务层调用的计时器来重置计时器.
You need to implement session-scope bean to check this, and reset the timer with a method called from your service layer to this bean, when a record to specific table performed.
此外,您还必须在JSP中检查与当前会话关联的计时器,以检查是否需要显示消息.
Also you have to check timer associated with current session in your JSP to check if you need to display message.
例如您创建了TimerBean-一些带有会话相关计时器的bean,可以从JSP中进行检查.
E.g. you created TimerBean - some bean with session-related timer to check from JSP.
1.
public class SessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent arg0) {
//init your timer in TimerBean and associate it with created session
}
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
//check your timer value and run you code if it's necessary
}
}
2.
@Startup //use eager initialization, or call init from login page
@Singleton // singleton session bean
public class YourDbTableSessionManager {
@Resource
private javax.ejb.SessionContext
void writeToDb {
//reset your timer in TimerBean for SessionContext
//perform write
}
}
3. web.xml
3. web.xml
<web-app ...>
<listener>
<listener-class>your.package.SessionListener</listener-class>
</listener>
</web-app>
这篇关于如何设置用户活动的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!