Provide time zone to Spring @Scheduled?

后端 未结 5 1603
广开言路
广开言路 2020-12-16 09:09

How can I configure the time zone for a Spring based @Scheduled cron job?

Background:

I have a job that executes once a day, say 2 PM, usin

相关标签:
5条回答
  • 2020-12-16 09:24

    Your code should be like this:

    @Scheduled(cron = "0 0 14 * * *", zone = "GMT-5")
        public void execute() {
         // do scheduled job
    }
    

    "Zone" is gonna be the desired country's timezone.

    Here is a nice tutorial about scheduled tasks with Spring:

    https://www.baeldung.com/cron-expressions

    0 讨论(0)
  • 2020-12-16 09:32

    I doubt you want different jobs or parts of application to use different time zones. Assuming you want to have it all consistent and DRY, either configure OS on all servers to have consistent time zone, or set user.timezone Java system property for all of the application servers. Centrally manage configuration (OS, application server), and for that puppet and chef can be very useful.

    0 讨论(0)
  • 2020-12-16 09:33

    There is element zone in annotation @Scheduled, starting from version 4.0.

    You can insert a timezone as a string that can be accepted by java.util.TimeZone.

    0 讨论(0)
  • 2020-12-16 09:36

    You can also use time zone with @Scheduled tag in spring-boot like this :

    @Scheduled(cron = "0 0 14 * * *" , zone = "GMT+5:00")
    public void execute() {
        // do the scheduled job
    }
    
    0 讨论(0)
  • 2020-12-16 09:38

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

    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()));
        }
    }
    

    Please read the JavaDoc of the @EnableScheduling for more information.


    Update

    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
    }
    
    0 讨论(0)
提交回复
热议问题