How to set a timer for activity of users?

2020-06-06 05:31发布

I need to execute a specific method if user stop working in 5 hours.

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.

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
  ....
  • 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);

1条回答
欢心
2楼-- · 2020-06-06 05:52

Since you use java-ee and struts, I assume you are talking about web-app.

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.

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.

Also you have to check timer associated with current session in your JSP to check if you need to display message.

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

<web-app ...>
    <listener>
        <listener-class>your.package.SessionListener</listener-class>
    </listener>
</web-app>
查看更多
登录 后发表回答