Search code examples
quartz-schedulercrontrigger

quartz cron trigger first fire time


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){

        }

Solution

  • Set the startTime property to the future Date at which you want the schedule (expression) to start applying.

    I see you say that you tried that and it didn't work, but it certainly should, so please try again.

        CronTrigger ct = new CronTrigger("foo", "goo", "0 0/10 * * * ?"); // fire every ten minutes, all day every day
    
        // construct a date of March 17, 2012 10:03:00
        Calendar futureDate = Calendar.getInstance(); 
        futureDate.set(Calendar.YEAR, 2012);
        futureDate.set(Calendar.MONTH, GregorianCalendar.MARCH);
        futureDate.set(Calendar.DAY_OF_MONTH, 17);
        futureDate.set(Calendar.HOUR_OF_DAY, 10);
        futureDate.set(Calendar.MINUTE, 3);
        futureDate.set(Calendar.SECOND, 0);
        futureDate.set(Calendar.MILLISECOND, 0);
    
        // use the date as the startTime
        ct.setStartTime(futureDate.getTime());
    
        // check what time the trigger will first fire
        List fireTimes = TriggerUtils.computeFireTimes(ct, null, 1);
        Date firstFireTime = (Date) fireTimes.iterator().next();
    
        System.out.println("First fire time: " + firstFireTime);
    

    This results in:

    First fire time: Sat Mar 17 10:10:00 MDT 2012