quartz cron trigger first fire time

ayengin picture ayengin · May 26, 2011 · Viewed 11k times · Source

Is there any way setting crontrigger to begin after a specific date .That is crontrigger going to fire after a specific date using its cron expression.I try using crontrigger starttime and firstfire time but not worked.I can do this with using another trigger but i think there should be another way.

this is cron expression

0  0/5  *  * * ? 

i.e. at minutes (5,10,15,...00) not at now+5

this is log program writes

Trigger should start at Fri May 27 21:03:31 EEST 2011 // i expect it run on this time

Job start at  Fri May 27 20:55:00 EEST 2011          //it ignore start time 
Job start at  Fri May 27 21:00:00 EEST 2011
Job start at  Fri May 27 21:05:00 EEST 2011

 public CronTrigger scheduleJob(RemoteJob job, String cronExpression,Date firstFireTime) throws SchedulerException, ParseException {
    JobDetail jobDetail = new JobDetail(job.getDescription(), job.getName(), job.getClass());
    CronTrigger crTrigger = new CronTrigger(
            "cronTrigger", job.getName(), cronExpression);       
    scheduler.scheduleJob(jobDetail, crTrigger);    

    try{
        Calendar  c=Calendar.getInstance();
        c.add(Calendar.MINUTE, 10);
        firstFireTime=c.getTime();
        FileWriter writer=new FileWriter("/opt/scheduler.cron",true);
        writer.write("Trigger should start at " +c.getTime().toString()+"\n\n");
        writer.close();
    }catch(Exception e){

    }
     crTrigger.setStartTime(firstFireTime);
    crTrigger.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);

    return crTrigger;

}

this is job executed by trigger .

public class ExternalJob extends RemoteJob {

    private static final Logger _logger = Logger.getLogger(ExternalJob.class.getName());
    private static ExternalStorageProcessor processor = new ExternalStorageProcessor();
    private ExternalTask task;
    private static final String tempPath = "/opt/itaptemp/";
    private String name;
    private String description;
    private static final long MARK=1L;

    public ExternalJob(String name, String description) {



    public void execute(JobExecutionContext context) throws JobExecutionException {


         try{
            Calendar  c=Calendar.getInstance();          

            FileWriter writer=new FileWriter("/opt/scheduler.cron",true);
            writer.write("Job start at  " +c.getTime().toString()+"\n");
            writer.close();
        }catch(Exception e){

        }

Answer

marvin picture marvin · Oct 17, 2012

The problem is that you are using a cron which fires a every even minute beginng a 0 minute (0 0/5 * * * ? ).

You should set the start date alse to even minutes. Therefore you can use DateBuilder.EvenMinuteDateBefore(StartTime).

So if your startime was... Fri May 27 20:55:31 ... the method DateBuilder.EvenMinuteDateBefore(StartTime) will convert it to ... Fri May 27 20:55:00.

Then your schedule will look like:

Job start at  Fri May 27 20:55:00 EEST 2011
Job start at  Fri May 27 21:00:00 EEST 2011
Job start at  Fri May 27 21:05:00 EEST 2011