Java Cron Expression
The technology is constantly changing day by day. Sometimes, we are required to execute a job periodically on the server. Running the job on the server manually is a difficult task so, it cannot be done multiple times by the user or administrator. In order to resolve this problem, Cron helps the programmer to execute a job periodically on the server. It is widely used for automated system maintenance and admiration. Beside this, we can also use Cron expression in web development. There are many situations when a web application may need to execute a certain task periodically.
In this section, we will discuss the Java Cron expressions, it's working and implementation through Java Quartz scheduler.
What is Cron?
Basically, Cron is a utility that schedules a task. It allows user to schedule a task periodically at specified time, date, month, weak, and year. It is widely used in automated process where human intervention is not required.
In Java, Cron expressions are used to configure instances of the CronTrigger class. It is a subclass of org.quartz.Trigger class. A Cron expression is a string or expression consisting of six or seven fields (time unit). It describes individual details of the schedule and the command.
A cron expression may simple as well as complex. For example,
Simple: * * * * ? * (asterisk denotes fields)
Complex: 0 0/4 12,15,3-35,52 ? May,June,july TUE-SAT 2002-2010.
Also, note that a Cron expression can be written in different styles, as follows. For example, The following three Cron expression have the same meaning.
All the three expressions, execute at 9:10 AM every day.
Fields Used in Cron Expression
In Cron expressions, time unit is known as field. These are separated by white space, can contain any of the allowed values with various combinations of the allowed characters for that field. It shows the fields in the expected order. The following table depicts the time unit and corresponding values, and symbols that can be used with the expressions.
Special Symbols and Letters Used in Cron Expression
Asterisk (*): The symbol is used when we want to execute event at every time unit (minute, second, hour). For example, if * is used with minute time unit, it means an event will execute at every minute, automatically.
Question Mark (?): The symbol is used in the fields like day of month and day of week. It denotes the arbitrary value and ignores the fields value. Suppose, we want to execute a script on 2nd of every month without caring about what day of the week fall in the 2nd day of the month. In this case, we should specify a question mark symbol (?) in the day of week field.
Hyphen (-): The symbol is used to specify the range. It determines the range of values. For example, if an hour field is defined as <4-5>, means an event will execute at 4th and 5th hour.
Comma (,): It is used to define values of the fields separated by comma. For example, Fri, Sat, Sun, etc.
Forward Slash (/): It is used to increment values of the specified time unit. Suppose, we want to start an event at 10 minutes, after that we are required to execute the same event at 20-minute, next execution at 30 minutes, and so on. In this case, we specify the start time and the incremental value, separated by forward slash.
For example, if 10/10 in specified in the minute field, it means first event execute at 10 minutes, and the next execution will be at 20-minutes of an hour, and so no.
L (Last): The use of L has different meaning with various fields.
W: The later represents the weekdays (Monday to Friday). It determines the weekday that is nearest to the specified day of the month.
For example, if we specify 5W in the day of month field, it means w determine the weekday near to the 5th of the month.
There may exist another case. Suppose, 1st of the month is Saturday, an event will occur on 3rd of the month. It means that event or script will not execute in the previous month.
Hash (#): It represents the n-th occurrence of a weekday of the month. For example, third Friday of the month can be indicated as 6#3.
Why we should use cron expressions?
There are various reasons to use cron expressions. Some of them are:
Example of Cron Expressions
Let's see some common Cron expressions that uses special symbol and fields.
Special Setting in Cron Expressions
Besides the above special symbol, Cron expression also support some predefined special values. These values can be used instead of the fields.
Using Cron Expression in Java
Java provides an open source job scheduling system that schedules a job for a specific time unit. We can download Quartz scheduler from the official site.
In terms of programming, job is a class that contains the task to be executed. But the question is how to schedule it. For scheduling a job, we define a trigger and specified when the job will execute.
If we want to implement Quartz scheduler, we can add the dependency, as follows:
Note: Before executing the following Java program, ensure that you have added quartz-2.2.1.jar file in the path, else you will get error.
Let's see the implementation of cron expression and job through a Java program.
Step 1: Create a Maven project. We have created with the name CronExample.
Step 2: Inside the project, create two packages namely com.javatpoint.app and com.javatpoint.job. In the first package, we will define the main class and in the second package we will define the jobs (in our case number of jobs is five).
Step 3: In the pom.xml file, paste the following dependency. It automatically downloads the required JAR files for the project. The pom file looks like the following:
Step 4: Create five jobs (in com.javatpoint.job package) that you want to execute. In our case, we have defined the following five jobs.
Step 5: Create the main class with the name MyApp.
When we done with all the above steps, the project directory looks like the following:
Step 6: Run the above Java program to see the output.
We observe that every job is executing at specific time interval.