为 Spring @Scheduled 提供时区? [英] Provide time zone to Spring @Scheduled?
问题描述
如何为基于 Spring 的 @Scheduled cron 作业?
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 PM 不同,因为 Spring 在 TimeZone.getDefault()
上使用 内部.此外,JavaDocTimeZone.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 的来源可能因实现而异.
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.
更新
从 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.
推荐答案
原来我无法使用 @Scheduled
注释,但我实现了一个变通方法.在的JavaDoc中SchedulingConfigurer 声明:
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 了解更多信息.
Please read the JavaDoc of the @EnableScheduling for more information.
更新
从 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, e.g.
@Scheduled(cron = "0 0 14 * * *", zone = "CET")
public void execute() {
// do scheduled job
}
这篇关于为 Spring @Scheduled 提供时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!