Cron Expression to Execute Every 10 Minutes
Cron expression is a time-based scheduling feature used in Unix-based systems and many other software applications. It allows you to automate tasks, schedule jobs, and run programs at specific intervals. One of the most popular requirements is to schedule the task to run every 10 minutes. In this article, we will show you how to create a Cron expression to execute a task every 10 minutes along with some sample code.
Understanding Cron Expression
Cron expression has six fields, separated by space or tab. These fields represent the time at which the task should be executed. The following is the format of the Cron expression:
* * * * * *
| | | | | |
| | | | | day of the week (0 - 7) (Sunday is 0 or 7)
| | | | month (1 - 12)
| | | day of the month (1 - 31)
| | hour (0 - 23)
| minute (0 - 59)
The asterisk (*) symbol indicates that the field should match all possible values. For example, 0 0 * * * * represents a task that should run at midnight (00:00) every day.
To execute a task every 10 minutes, we need to set the minute field to every 10 minutes. We should set the other fields to match any possible values. The following Cron expression can be used to run a task every 10 minutes:
*/10 * * * * *
The symbol */10
specifies that the task should be executed every 10 minutes, starting from minute 0. The other fields are set to match any possible value. This Cron expression will run the task every 10 minutes, regardless of the hour, day of the month, month, or day of the week.
Sample Code
Now let’s see how we can use this Cron expression in our code. The following sample code shows how to use the Cron expression with CronTrigger in Java:
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class CronJobExample {
public static void main(String[] args) {
try {
JobDetail job = JobBuilder.newJob(CronJob.class).withIdentity("CronJob").build();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("CronTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * *")).build();
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
In this code example, we create a job using JobBuilder and set the CronTrigger to execute the job every 10 minutes using the Cron expression we discussed above. The SchedulerFactory is used to create the Scheduler instance, and the job and trigger are scheduled using the Scheduler instance.
Conclusion
Cron expressions are an excellent way to schedule tasks and automate processes. By using the Cron expression */10 * * * * *, we can execute a task every 10 minutes with ease. The sample code we have looked at demonstrates how to use this Cron expression with CronTrigger in Java. Next time you need to schedule a task to execute every 10 minutes, use the Cron expression, and you will have it up and running in no time.