What can I use to implement a background process for every session in Java servlets running on tomcat?

I want to run a function for each user with a session at regular intervals to check if the user is active. If it is inactive, the function will remove the user from the list of users in the context of the servlet and clear the user session.

What can I use to run the function at regular time intervals for each user?

From what I understand, servletcontextlistener is only started once for the life of the servlet, and not for each user, so it cannot be used. In addition, streams are recommended for servlets.

Edit: Users (using ajax) invoke an action that contains a function that updates the variable that I saved for each user, which indicates the last time they contacted the server.

+3
source share
2 answers

You are probably looking javax.servlet.http.HttpSessionListener. Create it, register it in web.xmland do sessionDestroyedyour job.

+2
source

Two ways I can come up with my head:

  • Use request.getSession (). setMaxInactiveInterval (someValue)

  • ajax, " " "kill session". HTTP-, , , " " . , :

    Javascript- , , ( , , ). , , 5 , ajax , .

+1

All Articles