How to get current session (HttpSession) object in @Scheduled method in Spring 4.0.2?

≯℡__Kan透↙ 提交于 2019-12-12 02:02:34

问题


I am using Spring 4.0.2 for my Web Application. My Web Application is about file processing. There are some statues about files like "In Progress", "On Hold", "Completed". One user can complete multiple files, but only one at a time. So at a time only one file must be "In Progress" for a single user. Now, I want to check after every 15 mins whether is there any event occurred with particular file or not. If there is no event occurred, I want to change file status from "In Progress" to "On Hold". So that I tried to write Scheduler in Spring as given below.

@Scheduler(fixedDelay = 15*60*1000)
public void checkFrequently()
{
   // here I am doing some operation to check for any event occurred in last 15 min or not.
   // here, I need HttpSession for two purposes. 
   // 1. to get current logged in user
   // 2. to get current file for current user
}

Is there any possibility to get session in this method? If it is impossible, what are the alternatives?


回答1:


It is not possible. The scheduler is started at application launch, when there is no session, and runs in a thread separated from the servlet container.
Usually, you will persist in some form the states that you would like to make accessible by bean managed by the scheduler (being in a database, a plain file, a queue, etc...)



来源:https://stackoverflow.com/questions/23453556/how-to-get-current-session-httpsession-object-in-scheduled-method-in-spring-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!