如何在Spring 4.0.2的@Scheduled方法中获取当前会话(HttpSession)对象? [英] How to get current session (HttpSession) object in @Scheduled method in Spring 4.0.2?

查看:117
本文介绍了如何在Spring 4.0.2的@Scheduled方法中获取当前会话(HttpSession)对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Web 应用程序使用的是 Spring 4.0.2.我的 Web 应用程序是关于文件处理的.有一些关于文件的状态,例如进行中"、暂停"、已完成".一个用户可以完成多个文件,但一次只能完成一个.因此,对于单个用户,一次只有一个文件必须处于进行中"状态.现在,我想每 15 分钟检查一次特定文件是否发生了任何事件.如果没有发生任何事件,我想将文件状态从进行中"更改为暂停".所以我尝试在 Spring 中编写调度程序,如下所示.

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?

推荐答案

这是不可能的.调度程序在应用程序启动时启动,此时没有会话,并在与 servlet 容器分离的线程中运行.
通常,您会以某种形式持久化您希望由调度程序管理的 bean 访问的状态(在数据库、普通文件、队列等中...)

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

这篇关于如何在Spring 4.0.2的@Scheduled方法中获取当前会话(HttpSession)对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆