I'd like to update user session attributes (for example, user's money amount) every 20 minutes. How can I achieve this? Any ideas?
Asked
Active
Viewed 850 times
1
-
A servlet will only execute when called by the browser/front-end/jsp thus you'll have to get your some javascript to call the servelt every 20 minutes... Or you can manipulate the session object using javascript – TungstenX Dec 07 '14 at 16:26
-
@TungstenX there are many ways to manipulate session attributes other than by calling a servlet. A session is just an object, which can be manipulated at any point in Java code. And it is not possible to manipulate (or even access) the session object directly from Javascript code. – abl Dec 07 '14 at 16:51
-
@abl Of course one can get hold of the session object in Javascript: <% HttpSession s = request.getSession(false); %> and you are correct, on the Java side one can get hold of the session object under certain conditions. – TungstenX Dec 09 '14 at 15:35
-
@TungstenX I see no Javascript in your comment. That's a scriptlet. – abl Dec 09 '14 at 18:40
2 Answers
2
Your problem has two distinct parts:
- How do I update user session attributes whenever I want to?
- How do I do something every 20 minutes?
For the first part, you just need to keep a collection of all current sessions that you can access from anywhere (i.e. without the need of having an HttpServletRequest
). This is answered in this post:
How do I get a list of all HttpSession objects in a web application?
For the second part, your best fit is probably a ScheduledExecutorService. See also: Running a Java Thread in intervals
0
You can use Timer Service for this task.
If you use Spring
you can try TaskExecutor

Ruslan Ostafiichuk
- 4,422
- 6
- 30
- 35
-
-
You can take put user's session to a timer task on the servlet side – Ruslan Ostafiichuk Dec 09 '14 at 21:36