石英cron触发第一次火灾时间

时间:2011-05-26 18:23:43

标签: quartz-scheduler crontrigger

有没有办法设置crontrigger在特定日期之后开始。这是crontrigger在特定日期之后使用其cron表达式触发。我尝试使用crontrigger starttime和firstfire时间但没有工作。我可以使用另一个触发,但我认为应该有另一种方式。

这是cron表达式

0  0/5  *  * * ? 

即。在分钟(5,10,15,... 00)不在现在+ 5

这是日志程序写入

触发器应该从5月27日星期五21:03:31 EEST 2011开始//我希望它能在这个时间运行

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;

}

这是由触发器执行的作业。

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

        }

2 个答案:

答案 0 :(得分:2)

问题是你使用的是一个每隔0分钟开始一次0分钟( 0 0/5 * * *?)的cron。

您应该将开始日期设置为偶数分钟。因此,您可以使用DateBuilder.EvenMinuteDateBefore(StartTime).

因此,如果您的startime是...... Fri May 27 20:55:31 ...方法DateBuilder.EvenMinuteDateBefore(StartTime)会将其转换为... Fri May 27 20:55:00

然后你的日程安排如下:

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

答案 1 :(得分:1)

将startTime属性设置为您希望计划(表达式)开始应用的未来日期。

我看到你说你试过了它并没有用,但肯定应该这样,所以请再试一次。

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

这导致:

  

首次开火时间:2012年3月17日星期六10:10:00 MDT 2012