代码改变世界

时区切换导致quartz定时任务没有触发问题

2017-11-07 20:33  Loull  阅读(4648)  评论(0编辑  收藏  举报

时区切换对Quartz的cron表达式有影响,切换的1小时内停止触发定时任务,导致sla没有定时清空内存计数,误发限流。

美国夏令时PST切换到冬令时PDT,会有时间跳变。不带时区跳变的,会出现时间重叠或不连续

问题复现

mac本机模拟,把时区换成美国的,然后把时间调到11月5号01:59

import java.text.ParseException;
import java.util.Date;

import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzTester {

    public static void main(String[] args) throws ParseException, SchedulerException,
                                           InterruptedException {
        JobDetail jobDetail = new JobDetail("jobDetail", "jobDetail-1", QuartzDemo.class);
        CronTrigger trigger = new CronTrigger();
        trigger.setName("triggerTest");
        trigger.setCronExpression("0/5 * * * * ?");

        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(jobDetail, trigger);

        while (true) {
            long current = System.currentTimeMillis();
            System.out.println("normal time now=" + current + ", Date=" + new Date());
            Thread.sleep(5000);
        }
    }

    public static class QuartzDemo implements Job {

        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            long current = System.currentTimeMillis();
            System.out.println("Job now=" + current + ", Date=" + new Date());
        }

    }

}

 

1、把本地时间调到11-05 00:59

normal time now=1509857964930, Date=Sun Nov 05 00:59:24 EDT 2017
Job now=1509857965025, Date=Sun Nov 05 00:59:25 EDT 2017
normal time now=1509857969932, Date=Sun Nov 05 00:59:29 EDT 2017
Job now=1509857970012, Date=Sun Nov 05 00:59:30 EDT 2017
normal time now=1509857974935, Date=Sun Nov 05 00:59:34 EDT 2017
Job now=1509857975017, Date=Sun Nov 05 00:59:35 EDT 2017
normal time now=1509857979940, Date=Sun Nov 05 00:59:39 EDT 2017
Job now=1509857980020, Date=Sun Nov 05 00:59:40 EDT 2017
normal time now=1509857984944, Date=Sun Nov 05 00:59:44 EDT 2017
Job now=1509857985021, Date=Sun Nov 05 00:59:45 EDT 2017
normal time now=1509857989949, Date=Sun Nov 05 00:59:49 EDT 2017
Job now=1509857990013, Date=Sun Nov 05 00:59:50 EDT 2017
normal time now=1509857994952, Date=Sun Nov 05 00:59:54 EDT 2017
Job now=1509857995011, Date=Sun Nov 05 00:59:55 EDT 2017
normal time now=1509857999954, Date=Sun Nov 05 00:59:59 EDT 2017
normal time now=1509858004959, Date=Sun Nov 05 01:00:04 EDT 2017
normal time now=1509858009959, Date=Sun Nov 05 01:00:09 EDT 2017
normal time now=1509858014962, Date=Sun Nov 05 01:00:14 EDT 2017
normal time now=1509858019966, Date=Sun Nov 05 01:00:19 EDT 2017
normal time now=1509858024970, Date=Sun Nov 05 01:00:24 EDT 2017

从Nov 05 01:00开始,quartz的定时任务就停了

2、把本地时间调到11-05 01:10

信息: Quartz scheduler version: 1.5.2
Nov 05, 2017 1:10:07 AM org.quartz.core.QuartzScheduler start
信息: Scheduler DefaultQuartzScheduler$NON_CLUSTERED started.
normal time now=1509858607008, Date=Sun Nov 05 01:10:07 EDT 2017
normal time now=1509858612016, Date=Sun Nov 05 01:10:12 EDT 2017
normal time now=1509858617018, Date=Sun Nov 05 01:10:17 EDT 2017
normal time now=1509858622022, Date=Sun Nov 05 01:10:22 EDT 2017
normal time now=1509858627022, Date=Sun Nov 05 01:10:27 EDT 2017
normal time now=1509858632026, Date=Sun Nov 05 01:10:32 EDT 2017
normal time now=1509858637030, Date=Sun Nov 05 01:10:37 EDT 2017
normal time now=1509858642030, Date=Sun Nov 05 01:10:42 EDT 2017

quartz的定时任务一直没有触发

3、把本地时间调到11-05 01:59

normal time now=1509861583050, Date=Sun Nov 05 01:59:43 EDT 2017
normal time now=1509861588058, Date=Sun Nov 05 01:59:48 EDT 2017
normal time now=1509861593061, Date=Sun Nov 05 01:59:53 EDT 2017
normal time now=1509861598066, Date=Sun Nov 05 01:59:58 EDT 2017
normal time now=1509861603070, Date=Sun Nov 05 01:00:03 EST 2017
normal time now=1509861608072, Date=Sun Nov 05 01:00:08 EST 2017
normal time now=1509861613076, Date=Sun Nov 05 01:00:13 EST 2017
normal time now=1509861618080, Date=Sun Nov 05 01:00:18 EST 2017
normal time now=1509861623081, Date=Sun Nov 05 01:00:23 EST 2017
normal time now=1509865173966, Date=Sun Nov 05 01:59:33 EST 2017 //这里调整了系统时间
normal time now=1509865178970, Date=Sun Nov 05 01:59:38 EST 2017
normal time now=1509865183975, Date=Sun Nov 05 01:59:43 EST 2017
Job now=1509865185057, Date=Sun Nov 05 01:59:45 EST 2017
normal time now=1509865188977, Date=Sun Nov 05 01:59:48 EST 2017
Job now=1509865190012, Date=Sun Nov 05 01:59:50 EST 2017
normal time now=1509865193980, Date=Sun Nov 05 01:59:53 EST 2017
Job now=1509865195015, Date=Sun Nov 05 01:59:55 EST 2017
normal time now=1509865198985, Date=Sun Nov 05 01:59:58 EST 2017
Job now=1509865200022, Date=Sun Nov 05 02:00:00 EST 2017
normal time now=1509865203990, Date=Sun Nov 05 02:00:03 EST 2017
Job now=1509865205011, Date=Sun Nov 05 02:00:05 EST 2017
normal time now=1509865208995, Date=Sun Nov 05 02:00:08 EST 2017
Job now=1509865210016, Date=Sun Nov 05 02:00:10 EST 2017
normal time now=1509865214000, Date=Sun Nov 05 02:00:14 EST 2017
Job now=1509865215019, Date=Sun Nov 05 02:00:15 EST 2017

从夏令时的2点跳到冬令时的1点,quartz的定时任务没有开始执行,直到冬令时的2点,quartz的定时任务才开始执行。

结论

从夏令时的1点开始,到冬令时的2点,quartz都没触发定时任务,所以期间重启服务器也没用。

受时区影响的影响的:
Date()的分钟级加减
Calendar
mysql
把Date时间转成字符串,再转回Date
quartz的cron表达式

本机时区设置