Provide time zone to Spring @Scheduled?

48,704

Solution 1

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
}

Solution 2

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.

Share:
48,704
matsev
Author by

matsev

Updated on July 25, 2020

Comments

  • matsev
    matsev almost 4 years

    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, using Spring's @Scheduled annotation:

    @Scheduled(cron = "0 0 14 * * *")
    public void execute() {
        // do scheduled job
    }
    

    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:

    Gets the default TimeZone for this host. The source of the default TimeZone may vary with implementation.

    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?

    I am using Spring 3.2.2.


    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.

  • matsev
    matsev about 11 years
    Generally, I agree, and our servers are all configured to use the same time zone. Our servers need to interact with a third party server on a regular basis, hence the cron job. The problem is that the external server has a scheduled maintenance window during which we cannot interact with it, and that maintenance window is based on a different time zone than our servers. Of course, we could have configured all our servers to use the same time zone as the third party server, but we rather keep the coupling to external sources as limited as possible.
  • Ishan Thilina Somasiri
    Ishan Thilina Somasiri over 9 years
    A usage example of the timezone in the @scheduled annotation would be @Scheduled(cron = "0 0/60 * * * ?",zone = "Asia/Colombo")
  • Chandz
    Chandz almost 9 years
    @matsev I have followed your solution as I am also dealing with timezones along with cronTrigger, and my code is not working properly. Can you please check my code and guide me if I have went wrong anywhere? Here is the link: stackoverflow.com/questions/31370293/…
  • Chandz
    Chandz almost 9 years
    @IshanThilinaSomasiri Can you please help me by checking this link, stackoverflow.com/questions/31370293/…
  • Chandz
    Chandz almost 9 years
    @IshanThilinaSomasiri My doubt is, When cronTrigger() is built in WebConfig.java which contains both timezone & cron expression, how can we pass the same timezone & cron expression for @Scheduled(cron = "0 0/60 * * * ?", zone = "Asia/Colombo") in the different class where the method is present which has logic to run based of cron job.?
  • Bahadir Tasdemir
    Bahadir Tasdemir almost 8 years
    You can find yours from this list: joda-time.sourceforge.net/timezones.html
  • Alexander
    Alexander over 6 years
    etc @Scheduled(cron = "0 10 19 * * FRI", zone = "CET")