Vipul
Vipul

Reputation: 836

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.

Thanks in advance.

Upvotes: 1

Views: 13574

Answers (6)

Frederic Close
Frederic Close

Reputation: 9639

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

Upvotes: 8

Zaka
Zaka

Reputation: 33

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

Upvotes: 1

alexey28
alexey28

Reputation: 5220

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.

Upvotes: 3

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

Use this cron expression.

0 0/14 * * * ?

Upvotes: 0

shreyansh jogi
shreyansh jogi

Reputation: 2102

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

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

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

Upvotes: 1

Related Questions