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

限于喜欢 提交于 2019-12-11 04:32:07

问题


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.


回答1:


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.




回答2:


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




回答3:


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




回答4:


"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.




回答5:


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




回答6:


Use this cron expression.

0 0/14 * * * ?



来源:https://stackoverflow.com/questions/20417600/quartz-cron-expression-to-run-job-at-every-14-minutes-from-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!