简体   繁体   English

如何在Spring 4.0.2中的@Scheduled方法中获取当前会话(HttpSession)对象?

[英]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. 我使用Spring 4.0.2作为我的Web应用程序。 My Web Application is about file processing. 我的Web应用程序是关于文件处理。 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. 现在,我想每隔15分钟检查是否有特定文件发生的事件。 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. 因此我尝试在Spring中编写Scheduler,如下所示。

@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. 调度程序在应用程序启动时启动,当没有会话时,并在与servlet容器分离的线程中运行。
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...) 通常,您将以某种形式保留您希望由调度程序管理的bean访问的状态(在数据库,普通文件,队列等中...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Spring的Session / HttpSession对象中存储对象列表 - Store List of objects in Session/HttpSession object in Spring Spring Boot中带有HttpSession对象的JUnit Test方法 - JUnit Test method with HttpSession object in spring boot HttpSession - 如何获取session.setAttribute? - HttpSession - how to get the session.setAttribute? 如何在春季获取当前正在运行的计划任务/作业列表? - How to get list of current running scheduled task / job in spring? HttpSession对象如何绑定到浏览器? - How does the HttpSession object get bound to the browser? 如何将HttpSession从WildFly外部化为Redis? (不提供春季课程) - How to externalise HttpSession from WildFly to Redis? (W/O Spring Session) 如何模拟 HttpSession 并将其作为参数传递给与 session 一起使用的方法? - How to Mock HttpSession and pass it as arguemnt to a method which works with session? 如何获取序列化的tomcat HttpSession以重新填充Spring SessionRegistry - How to get serialized tomcat HttpSession to repopulate spring SessionRegistry spring-session 是否使用 HttpSession 保存 bean? - Does spring-session saves bean with HttpSession? spring httpsession和tomcat session之间的区别 - Difference between spring httpsession and tomcat session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM