2011-05-26 88 views
0

有沒有什麼辦法可以在特定的日期之後設置crontrigger的開始,也就是crontrigger會在使用cron表達式的特定日期之後觸發。我嘗試使用crontrigger starttime和firstfire time但是沒有工作。可以用另一個觸發器做到這一點,但我認爲應該有另一種方式。石英cron觸發器第一次着火時間

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

     } 

回答

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

這導致:

第一把火時間:週六3月17日10:10:00 MDT 2012

+0

你確定這是cron的觸發 – ayengin 2011-05-27 18:08:40

+0

絕對的情況下(除非有報道以前從未錯誤)。 – jhouse 2011-05-27 20:27:35

+0

我添加了我的調試信息和代碼.Cron觸發器忽略了啓動時間。 – ayengin 2011-05-29 16:42:57

2

的問題是,您使用的一個觸發每一個cron甚至分鐘開始0分鐘( 0/5 * * *?)。

您應該將開始日期設置爲偶數分鐘。因此,你可以使用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