简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM