为Spring @Scheduled提供时区? [英] Provide time zone to Spring @Scheduled?
问题描述
如何为基于Spring的 @ Scheduled cron job?
How can I configure the time zone for a Spring based @Scheduled cron job?
背景:
我的工作每天执行一次,比如说下午2点,使用Spring的 @Scheduled
注释:
I have a job that executes once a day, say 2 PM, using Spring's @Scheduled
annotation:
@Scheduled(cron = "0 0 14 * * *")
public void execute() {
// do scheduled job
}
问题是不同服务器之间的下午2点不同,因为Spring使用 TimeZone.getDefault()
内部。此外, JavaDoc TimeZone.getDefault()
声明:
The problem is that 2 PM differs between different servers, because Spring uses on TimeZone.getDefault()
internally. Moreover, the JavaDoc of TimeZone.getDefault()
states that:
获取此主机的默认TimeZone 。默认TimeZone的来源可能因实施而异。
Gets the default TimeZone for this host. The source of the default TimeZone may vary with implementation.
换句话说,时区未确定。它可能取决于JVM实现,服务器时区配置,服务器位置和/或其他未知因素。因此,cron作业在不同服务器上的不同时间触发,除非有办法明确设置应该使用哪个时区?
In other words, the time zone is not determined. It may depend on JVM implementation, server time zone configuration, server location, and / or other unknown factors. Consequently, the cron job triggers on different times on different servers, unless there is a way to explicitly set which time zone that should be used?
我使用的是Spring 3.2。 2。
I am using Spring 3.2.2.
更新
截至4月4日,Spring Jira问题 SPR-10456 已经解决。因此, @Scheduled
注释有一个新的区域属性正是出于此目的。
As of Spring 4, Spring Jira issue SPR-10456 has been resolved. Consequently, the @Scheduled
annotation has a new zone attribute for exactly this purpose.
推荐答案
事实证明我无法使用 @Scheduled
注释,但我实施了解决方法。在 SchedulingConfigurer 的JavaDoc中声明:
It turned out that I could not use the @Scheduled
annotation, but I implemented a work-around. In the JavaDoc of the SchedulingConfigurer it is stated that:
[SchedulingConfigurer is]通常用于设置在执行计划任务或注册计划时使用的特定TaskScheduler bean以编程方式执行任务,而不是使用@Scheduled注释的声明方法。
[SchedulingConfigurer is] Typically used for setting a specific TaskScheduler bean to be used when executing scheduled tasks or for registering scheduled tasks in a programmatic fashion as opposed to the declarative approach of using the @Scheduled annotation.
接下来,我更改了cron作业以实现 Runnable
界面,然后更新我的配置文件以实现 SchedulingConfigurer
,见下文:
Next, I changed the cron job to implement the Runnable
interface and then updated my configuration file to implement the SchedulingConfigurer
, see below:
@Configuration
@EnableScheduling
@ComponentScan("package.that.contains.the.runnable.job.bean")
public class JobConfiguration implements SchedulingConfigurer {
private static final String cronExpression = "0 0 14 * * *";
private static final String timeZone = "CET";
@Autowired
private Runnable cronJob;
@Bean
CronTrigger cronTrigger() {
return new CronTrigger(cronExpression, TimeZone.getTimeZone(timeZone));
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addCronTask(new CronTask(job, cronTrigger()));
}
}
请阅读@EnableScheduling 了解更多信息。
更新
截至Spring 4,Spring Jira问题 SPR-10456 已得到解决。因此, @Scheduled
注释有一个新的zone 属性就是为了这个目的。
As of Spring 4, Spring Jira issue SPR-10456 has been resolved. Consequently, the @Scheduled
annotation has a new zone attribute for exactly this purpose.
这篇关于为Spring @Scheduled提供时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!