我使用 @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/