Friday, January 14, 2011

Confirming Quartz Trigger firing times (especially cron expressions)

The Quartz Scheduler is very useful when it comes to scheduling jobs. It offers a wide range of trigger options, including cron-like expressions. Cron-like expressions are a bit obscure to read, unless you use them often. These expressions are explained in the CronExpression and CronTrigger classes API, and in the online tutorial. I sometimes need to make sure that the triggers will get fired at the proper time. The TriggerUtils class allows to output all firing dates of a trigger between two dates : TriggerUtils#computeFireTimesBetween(Trigger, Calendar, Date, Date). The Calendar parameter here is not a java.util.Calendar, but a org.quartz.Calendar, which can be annoying when using both java.util.Calendar and org.quartz.Calendar in the same class.

Making a sample program to output all firing times between two dates is rather simple :

import java.text.ParseException;
import java.util.List;

import org.quartz.CronTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.calendar.AnnualCalendar;


public class ConfirmTriggerFireTimes {

 public static void main(String[] args) throws ParseException {
  new ConfirmTriggerFireTimes().showTriggerFireTimes();
 }
 
 public void showTriggerFireTimes() throws ParseException {
  CronTrigger trigger = new CronTrigger();
  // Fire at 10:15am every Monday, Tuesday, Wednesday, and Thursday 
  trigger.setCronExpression("0 15 10 ? * MON-THU");
  java.util.Calendar startDate = java.util.Calendar.getInstance();
  startDate.set(2011,java.util.Calendar.JANUARY, 1);
  java.util.Calendar endDate = java.util.Calendar.getInstance();
  endDate.set(2011,java.util.Calendar.JANUARY, 31);
  outputFireTimeList(trigger, startDate, endDate);
 }
 
 @SuppressWarnings("rawtypes")
 private void outputFireTimeList(Trigger trigger, java.util.Calendar from, java.util.Calendar to) {
  List fireTimeList = TriggerUtils.computeFireTimesBetween(trigger, null, from.getTime(), to.getTime());
  for ( int i = 0; i < fireTimeList.size(); i++ ) {
   System.out.println(fireTimeList.get(i));
  }
 }
}
In this example, I want to confirm the firing dates of a trigger set to fire at 10:15am every Monday, Tuesday, Wednesday, and Thursday in January. The resulting output is :
Mon Jan 03 10:15:00 JST 2011
Tue Jan 04 10:15:00 JST 2011
Wed Jan 05 10:15:00 JST 2011
Thu Jan 06 10:15:00 JST 2011
Mon Jan 10 10:15:00 JST 2011
Tue Jan 11 10:15:00 JST 2011
Wed Jan 12 10:15:00 JST 2011
Thu Jan 13 10:15:00 JST 2011
Mon Jan 17 10:15:00 JST 2011
Tue Jan 18 10:15:00 JST 2011
Wed Jan 19 10:15:00 JST 2011
Thu Jan 20 10:15:00 JST 2011
Mon Jan 24 10:15:00 JST 2011
Tue Jan 25 10:15:00 JST 2011
Wed Jan 26 10:15:00 JST 2011
Thu Jan 27 10:15:00 JST 2011
Mon Jan 31 10:15:00 JST 2011
This looks good. We can see that any jobs associated to this trigger will be executed at 10:15am every Monday, Tuesday, Wednesday, and Thursday.

5 comments:

  1. Thông tin rất cần thiết, thank bạn đã chia sẻ.
    Tìm hiểu website : Giá đá thạch anh

    ReplyDelete
    Replies
    1. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head. I am highly impressed with your blog. It is very nicely explained. Your article adds best knowledge to our Java EE Training in Chennai. or learn thru Java EE Training in Chennai Students.

      Delete
  2. Bài post của tác giả rất hay, thank bạn đã share.
    Xem tại website : Đá thạch anh vụn

    ReplyDelete
  3. Bài post của Admin quá hay, thank bạn đã share.
    Page hữu ích : Gia công thạch anh

    ReplyDelete
  4. Bài post của Bạn cũng hữu ích, cám ơn anh đã chia sẻ.
    Thông tin thêm : Tỳ hưu thạch anh

    ReplyDelete