【问题标题】:On quartz scheduler, clarification needed在石英调度程序上,需要澄清
【发布时间】:2012-10-29 05:00:45
【问题描述】:

关于 Quartz 的几个独立问题

  1. 如果我想让事件执行一次,是否足够

    trigger.setRepeatCount(0);
    
  2. 考虑到这个sn-p。事件在预定时间为“现在之前”时运行,而在预定时间为将来时无法执行

    JobDetail job = new JobDetail();
    job.setName(eventType.toString() + " event");
    job.setJobClass(Action.class);
    
    SimpleTrigger trigger = new SimpleTrigger();
    trigger.setStartTime(new Date(momentInTime.inMillis()));
    trigger.setName("trigger");
    
    trigger.setRepeatInterval(repeatFrequency.inMillis());
    trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
    
    scheduleManager.getScheduler().scheduleJob(job, trigger);
    

使用上面的代码,我观察到以下内容

Time now         : 1352410780356
Will execute at  : 1352410840356  // 1 min interval
Execution starts : 1352410840368

现在和计划执行的时间相差正好 1 分钟..

好的..尝试更大的间隔

Time now         : 1352411061156
Will execute at  : 1352411301156 // 3 min interval
Execution starts : 1352411301165

再一次.. 差别正好是 3 分钟,因为

应该在某个时刻执行

   new MomentInTime(new DayOfMonth(8), new HourOfDay(15), new MinuteOfHour(48));

它实际上开始(我应该说似乎开始)作为当前时间和请求时间之间的偏移量(甚至是偏移量)。

看来,如果是10:43:25,并且我想在10:45:00 开始工作,它会发现有2 个偶数分钟的差异,并将安排在10:45:25 的工作。

是什么原因造成的?

同时,

public MomentInTime(DayOfMonth day, HourOfDay hour, MinuteOfHour min) {
    calendarInstance = Calendar.getInstance();

    // Get current year and month
    int year = calendarInstance.get(Calendar.YEAR);
    int month = calendarInstance.get(Calendar.MONTH);

    calendarInstance.set(year, month, day.getValue(), hour.getValue(), min.getValue());
    System.out.println("Time now       : " + System.currentTimeMillis());
    System.out.println("Will execute at: " + calendarInstance.getTimeInMillis());
}

public long inMillis() {
    return calendarInstance.getTimeInMillis();
}

【问题讨论】:

    标签: java scheduled-tasks scheduling quartz-scheduler


    【解决方案1】:

    1 行修复

        calendarInstance.set(Calendar.MILLISECOND, 0);
    

    完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多