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

发布于 2024-11-10 12:33:37 字数 2313 浏览 5 评论 0原文

有没有办法将 crontrigger 设置为在特定日期之后开始。即 crontrigger 将使用其 cron 表达式在特定日期之后触发。我尝试使用 crontrigger 启动时间和首次触发时间,但没有成功。我可以使用另一个触发器来做到这一点,但我认为应该有另一种方式。

这是 cron 表达式

0  0/5  *  * * ? 

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

这是日志程序写入

触发器应在 EEST 2011 年 5 月 27 日周五 21:03:31 开始 // 我希望它运行于这次

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

        }

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

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俏︾媚 2024-11-17 12:33:37

问题是您使用的 cron 会从 0 分钟开始每隔偶数分钟触发一次(0 0/5 * * * ?)。

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

因此,如果您的开始时间是... Fri May 27 20:55:31 ... 方法 DateBuilder .EvenMinuteDateBefore(StartTime) 将其转换为...
5 月 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

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
悲念泪 2024-11-17 12:33:37

将 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 年 MDT 3 月 17 日星期六 10:10:00

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文