石英不是在规定的确切时间运行工作

时间:2014-05-29 05:56:07

标签: java quartz-scheduler

目前正在开发我的项目,它将使用quartz scheduler 2.2.1,我希望每天下午3:21:00运行石英作业,下面的代码可以找到。唯一的问题是石英没有完全像我指示的那样执行我的时间,因为它第一次完全在特定的时间运行,但是在第二天等等它将在它想要执行的时间之后仅运行10秒,30秒或更多。我需要石英在我开处方时准确运行。

你们中的任何一个人都有同样的问题,解决这个问题的方法是什么。

public class QuartzDaily {
public static void main(String[] args) throws ParseException, SchedulerException{

//quart schedule to run job everyday

Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

Date startDate = new SimpleDateFormat("dd.M.yyyy hh:mm:ss a").parse("05.6.2014 3:21:00 PM");  
System.out.println(startDate);

JobDetail job = newJob(TestJob.class)
.withIdentity("Job1", "groupJob1")
.build();

Trigger trigger = newTrigger()
  .withIdentity("trigger1", "grouprigger")
  .startAt(startDate)  // first fire time 15:21:00
  .withSchedule(simpleSchedule()
        .withIntervalInHours(1*24) // interval is actually set at 24 hours' worth of milliseconds
  .repeatForever())
  .build();

scheduler.scheduleJob(job, trigger);
scheduler.start();
System.out.println("Cron has started");

 }


//class containb job to run
public static class TestJob implements Job {
       public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Job is Running");
    }    
}
}

1 个答案:

答案 0 :(得分:1)

我的问题的答案是:

如果手动更改计算机系统时钟以在设置石英运行的日期跳转,则会影响石英作业性能。开始执行作业需要更长的时间。可能时钟系统暂时出现问题,需要将新进程内的所有进程同步。我自己试试这个,我相信测试石英计划的最佳方法是等待实际的日期或时间。

从周五到周一早上,我尝试了三天的自我试验。我将石英设置为每5分钟,20分钟,2小时,4小时,每天和每2天运行一次。工作在指定的时间准确运行。

希望这对其他人有用。