Time of time expression in the cron4j framework
The CRON4J framework is a lightweight Java scheduling library that allows developers to define and manage the regular execution of tasks.It uses a time expression similar to the CRON tool in Linux to configure the task execution time.This article will introduce the use of time expression in the CRON4J framework and provide some Java code examples.
Time expression is the key to the execution time configuration of the CRON4J framework.It consists of 6 fields, which represent the second, minute, hour, date, month and week.Below is an example of a time expression:
0 0 12 * * ?
This time expression indicates the task of performing the task at 12 noon daily.Next, we will introduce each field and techniques of time expression in detail.
1. Second (Second) field: It means the task in the first few seconds in one minute.The value range is 0-59, which can be executed every second using the passing symbol "*" representative.
2. Minute field: It means the task in the first few minutes in one hour.The range of the value is 0-59, and the "*" representative can also be executed per minute.
3. Hour field: indicate the task of the first hour of the day.The value range is 0-23, which can also be executed per hour using the passing symbol "*".
4. Deta (DAY) field: It means the task of performing the task for the first few days of the month.The range is 1-31, which is usually used with a few segments on the week.You can use the "?" To ignore the field.
5. Month field (Month) field: It means the task of performing the task in the first few months of the year.The value range is 1-12, and you can also use the English exclusive month or the passing symbol "*" representative to execute every month.
6. Weekday field: What are the tasks of the week in the week.The range is 1-7, of which 1 represents Sunday, 2 represents Monday, and so on.At the same time, you can use the English abbreviation week or the pass "?" It means that the field is ignored.
In addition to the "*" and ignoring the symbol "?", We can also use the comma ",", the slope "/", and "-" to define the time expression.
The comma ",", used to specify multiple values, for example:
0 0 12,18 * * ?
This time expression indicates that the task is performed at 12 noon and 6 pm.
Slip "/" is used for specified time interval, for example:
0 0/15 * * * ?
This time expression indicates four tasks per hour, 0 points, 15 points, 30 points and 45 points, respectively.
Lian character "-" is used to specify a range, such as:
0 0 9-17 * * ?
This time expression indicates that tasks are performed between 9 am and 5 pm every day.
Below is an example of Java code using the CRON4J framework scheduling task:
import it.sauronsoftware.cron4j.Scheduler;
import it.sauronsoftware.cron4j.Task;
import it.sauronsoftware.cron4j.TaskExecutionContext;
public class CronScheduler {
public static void main(String[] args) {
Scheduler scheduler = new Scheduler();
Task task = new Task() {
@Override
public void execute(TaskExecutionContext context) throws RuntimeException {
// The operation to be performed by time task to be performed
System.out.println("Task executed at: " + context.getFireTime());
}
};
scheduler.schedule (" * * * * * *", task); // Set time expression and task
scheduler.start (); // Start the scheduler
}
}
This sample code creates a simple timing task, and the time expression " * * * * * * *" indicates that the task is performed every minute.The current execution time was printed during the task execution.
In summary, the technique of time expression in the CRON4J framework includes understanding the meaning and value range of each field, and flexibly use the passage and symbols to customize the execution time of the task.The use of the CRON4J framework can easily manage and schedule timing tasks to improve the reliability and efficiency of the application.