简体   繁体   中英

Quartz cron expression to run job at every 14 minutes from now

I want to run a job at every 14 minutes form now.

For example if I schedule a job at 11:04 am, using 0 0/14 * * * ? cron expression. then expected fire time suppose to be 11:18,11:32,11:46 and so on . but it will fire at 11:00,11:14:11,28:11:42,11:56,12:00 which is not expected. and why it fired at 12:00 o'clock after 11:56, there is diff of only 4 min.

  • How can I achieve what I want using cron expression?
  • Can any one explain me this behaviour of quartz cron?

Thanks in advance.

your cron expression should look like

0 0/14 * 1/1 * ? *

A great website to create your cron expression when you are in doubt : http://www.cronmaker.com/

it will help you build your cron expression and show you the next firing date times of your cron.

For More Reference : http://www.nncron.ru/help/EN/working/cron-format.htm

Well, 0/14 give you fire time at 00, 14, 28, 42, 56 and again at 00 minutes of every hour. So last interval will be not 14 but 4 minutes. Thats how cron works. You can get equals interval in minutes only for cases when remainder of the division 60 by your interval is zero.

You get it wrong. 0/14 means it will fire each hour starting from 0 after 14min . That's why it is firing at 12.00

"0 0/14 * * * ?" means the next fire time from the beginning of the clock for every 14 minutes interval, like what you said.

The 1st '0' means SECOND at 0 (or 12) at the clock; and same for the 2nd '0' which means the MINUTE at 0 (or 12) at the clock; '/14' means 14 minutes as the interval.

So get the SECOND and MINUTE from current time and concatenate them with the interval into a cron expression then fire it. Below is the example for Java:

public static String getCronExpressionFromNowWithSchedule(int minuteInterval) throws Exception {
    String cronExpression = "";
    Calendar now = Calendar.getInstance();
    int year = now.get(Calendar.YEAR);
    int month = now.get(Calendar.MONTH); // Note: zero based!
    int day = now.get(Calendar.DAY_OF_MONTH);
    int hour = now.get(Calendar.HOUR_OF_DAY);
    int minute = now.get(Calendar.MINUTE);
    int second = now.get(Calendar.SECOND);
    int millis = now.get(Calendar.MILLISECOND);

    if (minuteInterval<=0) cronExpression = (second+1)+" * * * * ?";
    else cronExpression = (second+1)+" "+minute+"/"+minuteInterval+" * * * ?";

    System.out.println(cronExpression);
    return cronExpression;
}

The next fire time is at next second from current time for the Minute interval you passed into this method.

you should change your cron expression to 0 0/14 * 1/1 * ? * 0 0/14 * 1/1 * ? *

Use this cron expression.

0 0/14 * * * ?

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