简体   繁体   中英

How to configure cronSchedule() / cron expression in Quartz library to run same job every day at 11am, 12am, 15am, 17am etc?

I am using Quartz library to add cron timer to my application.

Sample usage is explained here , specifying run periodicity in this API is done thru so-called "cron string" (cron expression) - it is well-explained in examples here and in official docs here .

CronTrigger trigger = newTrigger()
    .withIdentity("trigger1", "group1")
    .withSchedule(cronSchedule("0/20 * * * * ?")) // cron string
    .build();

I understand how to run a task specifying starting point + periodicity (say, at 11am every day, every Friday, etc).

But I need to run it every day at 11am, 12am, 3pm, 7pm etc - I can just enumerate exact times. How can I specify that? I need same task (job) be executed every day exactly at these many times.

It is stupid to create so many Trigger(s), each with separate cron expression. Is there a nice solution?

Here , docs say:

, - used to specify additional values. For example, “MON,WED,FRI” in the day-of-week field means “the days Monday, Wednesday, and Friday”.

So maybe this works: "0 0 11am,12am,3pm,7pm * * ?" ?

PS Or all I can do is to create a separate Trigger for every hour and then add all such Triggers (with same job) to Scheduler like

myScheduler.scheduleJob(sameJob, trigger11am);
myScheduler.scheduleJob(sameJob, trigger12am);
myScheduler.scheduleJob(sameJob, trigger3pm);
myScheduler.scheduleJob(sameJob, trigger7pm);

Yes, this is the way it can be done:

// setup CronTrigger
// misfire policies (when app was down at scheduled time):
// https://www.nurkiewicz.com/2012/04/quartz-scheduler-misfire-instructions.html            
Trigger trigger = TriggerBuilder
        .newTrigger()
        .withIdentity("triggerName", "myGroupName")
        .withSchedule(
                // at 11:30am, 12:30am, 03:30pm, 06:30pm, etc every day, including Sunday, Monday, days-off                         
                CronScheduleBuilder.cronSchedule("0 30 11,12,15,18,21,23 * * ?")
                // don't run missed jobs (missed when app was down) 
        .withMisfireHandlingInstructionDoNothing())             
        .build();

Here official examples at the bottom confirm it - see "Fire at 2:10pm and at 2:44pm every Wednesday".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM