Spring 3.0 @Scheduled 注释未按预期工作

标签 spring spring-mvc scheduled-tasks scheduling spring-annotations

我使用 @Scheduled 注释在 Spring MVC 应用程序(在 Apache Tomcat/7.0.26 上运行的版本 3.0.6.RELEASE)中创建函数 myProcess() )每小时运行一次(3,600,000 毫秒 = 1 小时):

@Scheduled(fixedRate = 3600000)
public void myProcess() { ... }

该函数按预期执行,但不是在早上(请参阅下面最近 2 天的示例日志时间)。这种事每天都会发生。我在日志文件中没有看到任何异常。您对这种奇怪行为的原因有什么想法吗?


Feb 13 02:11:15
Feb 13 03:11:16 
Feb 13 06:17:34
Feb 13 06:45:55 
Feb 13 07:03:22
Feb 13 07:31:57
Feb 13 08:11:16 
Feb 13 09:11:18
Feb 13 10:11:18 
Feb 13 11:11:28 

Feb 14 01:11:37
Feb 14 02:11:29
Feb 14 03:11:29 
Feb 14 06:19:51
Feb 14 06:49:17
Feb 14 07:35:57
Feb 14 08:11:29
Feb 14 09:11:35

最佳答案

我遇到了同样的问题,但是使用 cron属性:

// every day at midnight
@Scheduled(cron = "0 0 0 * * ?")
public void myProcess() {
}

我不太记得它的行为,但这不是我所期望的。我最后发现这可能是依赖于Spring 3.0.x中的一个bug。我尝试过(并且有效)的解决方案是在 applicationContext.xml 文件中声明任务,除了注释:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    ...

    <!-- scheduling and async -->
    <task:annotation-driven />
    <task:scheduled-tasks>
      <task:scheduled ref="myProcessHandler" method="myProcess" fixed-delay="0" />
    </task:scheduled-tasks>
    <bean id="myProcessHandler" class="path.to.MyProcessHandler" />

    ...

</beans>

即使fixed-delay属性对于修复错误是必要的(据我所知),没有考虑到,而 cron @Scheduled 的属性注释是。

我希望这会有所帮助。

关于Spring 3.0 @Scheduled 注释未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14890527/

相关文章:

web-services - Spring Web 服务模板 : Add username token

Spring Integration - 并发服务激活器

java - 如何在 freemarker 模板中转义来自 web 服务的数据中的 '&' ?

java - 如何在 spring MVC 中 Autowiring 多级继承类

windows-10 - 任务计划程序无法启动。附加数据 : Error Value: 2147943726

java - 如何使用spring boot admin 2版本和spring boot 1.5版本?

javascript - 使用 ANGULAR JS 和 Spring MVC 在 HTML 中动态显示添加/删除操作的结果

java - 使用 Spring MVC、Tomcat 应用程序为静态资源启用浏览器缓存

power-shell 脚本的 Windows 调度不可靠

javascript - 从调度程序填充 Azure 移动服务