Cron4j Scheduler



Cron4j is a scheduler for the Java platform which is very similar to the UNIX CRON daemon. It is very useful when required to launch scheduled task, from within your Java applications, according to some simple rules.


The Java 2 platform already has a built-in scheduler, implemented with the class java.util.Timer. The cron4j scheduler, however, acts in a different way. You can say to the java.util.Timer scheduler something like "launch this task after 5 minutes from now" or "launch it after 5 minutes from now, then repeat it every 10 minutes". That's all. The cron4j scheduler, instead, lets you do something a little more complex, like "launch this task every Monday, at 12:00", "launch it every 5 minutes, but don't launch it during the weekend", "launch it every hour between the 8:00AM and the 8:00PM and launch it every 5 minutes between the 8:00PM and the 8:00AM", "launch it once every day but Sunday, during every month but July and August" and so on, and all that with a single line of code.


How to Use Cron4j:Download Cron4j from https://sourceforge.net/projects/cron4j/files/cron4j/2.2.5/cron4j-2.2.5.zip/download. Add cron4jxxx.jar(xxx is version number) from zip file to the classpath of the application where you intend to use the scheduler feature. The cron4j job configuration do not require any properties file.

Now create the Cron4jDemo class where job instance and trigger will be defined.

import it.sauronsoftware.cron4j.Scheduler;

public class Cron4jDemo {

                public static void main(String[] args) {
                                // Creates a Scheduler instance
                                Scheduler s = new Scheduler();
                                // Schedule a once-a-minute task
                                s.schedule("* * * * *", new Runnable() {
                                                public void run() {
                                                                System.out.println("This text will be printed every minute...");
                                                }
                                });
                                // Starts the scheduler
                                s.start();
                                // Will run for ten minutes
                                try {
                                                Thread.sleep(1000L * 60L * 10L);
                                } catch (InterruptedException e) {
                                                e.printStackTrace();
                                }
                                // Stops the scheduler
                                s.stop();
                }
                                                                    }

Pattern: [* * * * *] : [Minute Hour Day Month Day-of-Week]A UNIX crontab-like pattern is a string split in five space separated parts. Each part is intended as:

1. Minutes sub-pattern: During which minutes of the hour should the task been launched? The values range is from 0 to 59.
2. Hours sub-pattern: During which hours of the day should the task been launched? The values range is from 0 to 23.
3. Days of month sub-pattern: During which days of the month should the task been launched? The values range is from 1 to 31. The special value "L" can be used to recognize the last day of month.
4. Months sub-pattern: During which months of the year should the task been launched? The values range is from 1 (January) to 12 (December), otherwise this sub-pattern allows the aliases "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov" and "Dec".
5. Days of week sub-pattern: During which days of the week should the task been launched? The values range is from 0 (Sunday) to 6 (Saturday), otherwise this sub-pattern allows the aliases "Sun", "Mon", "Tue", "Wed", "Thu", "Fri" and "Sat".

The star wildcard character is also admitted, indicating "every minute of the hour", "every hour of the day", "every day of the month", "every month of the year" and "every day of the week", according to the sub-pattern in which it is used.

Once the scheduler is started, a task will be launched when the five parts in its scheduling pattern will be true at the same time.


Some examples:

5 * * * *This pattern causes a task to be launched once every hour, at the begin of the fifth minute (00:05, 01:05, 02:05 etc.).

* * * * *This pattern causes a task to be launched every minute.

* 12 * * MonThis pattern causes a task to be launched every minute during the 12th hour of Monday.

* 12 16 * MonThis pattern causes a task to be launched every minute during the 12th hour of Monday, 16th, but only if the day is the 16th of the month.

Every sub-pattern can contain two or more comma separated values.

59 11 * * 1,2,3,4,5This pattern causes a task to be launched at 11:59AM on Monday, Tuesday, Wednesday, Thursday and Friday.

Values intervals are admitted and defined using the minus character.

59 11 * * 1-5This pattern is equivalent to the previous one.

The slash character can be used to identify step values within a range. It can be used both in the form */c and a-b/c. The subpattern is matched every c values of the range 0,maxvalue or a-b.

*/5 * * * *This pattern causes a task to be launched every 5 minutes (0:00, 0:05, 0:10, 0:15 and so on).

3-18/5 * * * *This pattern causes a task to be launched every 5 minutes starting from the third minute of the hour, up to the 18th (0:03, 0:08, 0:13, 0:18, 1:03, 1:08 and so on).

*/15 9-17 * * *This pattern causes a task to be launched every 15 minutes between the 9th and 17th hour of the day (9:00, 9:15, 9:30, 9:45 and so on... note that the last execution will be at 17:45).

All the fresh described syntax rules can be used together.

* 12 10-16/2 * *This pattern causes a task to be launched every minute during the 12th hour of the day, but only if the day is the 10th, the 12th, the 14th or the 16th of the month.

* 12 1-15,17,20-25 * *This pattern causes a task to be launched every minute during the 12th hour of the day, but the day of the month must be between the 1st and the 15th, the 20th and the 25, or at least it must be the 17th.

Finally cron4j lets you combine more scheduling patterns into one, with the pipe character:

0 5 * * *|8 10 * * *|22 17 * * *This pattern causes a task to be launched every day at 05:00, 10:08 and 17:22.


Invoking System Process:

ProcessTask task = new ProcessTask("C:\\Windows\\System32\\notepad.exe");
Scheduler scheduler = new Scheduler();
scheduler.schedule("* * * * *", task);
scheduler.start();


Scheduling tasks providing user arguments can be done as follows:

String[] command = { "C:\\tomcat\\bin\\catalina.bat", "start" };
String[] envs = { "CATALINA_HOME=C:\\tomcat", "JAVA_HOME=C:\\jdks\\jdk5" };
File directory = "C:\\MyDirectory";
ProcessTask task = new ProcessTask(command, envs, directory);

Main Features of Cron4j Scheduler:• You can schedule as many as tasks you want.
• You can schedule a task when you want, also after the scheduler has been started.
• You can change the scheduling pattern of an already scheduled task, also while the scheduler is running (reschedule operation).
• You can remove a previously scheduled task, also while the scheduler is running (de-schedule operation).
• You can start and stop a scheduler how many times you want.
• You can schedule from a file.
• You can schedule from any source you want.
• You can supply listeners to the scheduler in order to receive events about the executed task.
• You can control any ongoing task.
• You can manually launch a task, without using a scheduling pattern.
• You can change the scheduler working Time Zone.
• You can validate your scheduling patterns before using them with the scheduler.
• You can predict when a scheduling pattern will cause a task execution.

Similar Scheduler Frameworks:                Below are some tools similar to Quartz Framework
     Quartz Scheduler: It is a scheduler for the Java 2 platform having job-failover, transaction, clustering & listeners plug-ins support.
     Gos4j: Goal Oriented Scheduling for Java is a way of organizing processing priorities based on goals. Each goal is processed based on its time to complete, and its progress towards that goal.
     jcrontab: It is designed to be extended and integrated with any project. Reads and stores the tasks to execute in a file, a database or an EJB and provides a web UI and a basic swing GUI.
     Fulcrum-scheduler: It is based on the Turbine Scheduler provided with Turbine, but all older stuff has been removed. Currently ONLY the non persistent Scheduler is done. It loads scheduled jobs from the component config xml file.
     Essiembre J2EE Scheduler: It is a simple task scheduling mechanism for J2EE applications used to periodically refresh in-memory data to ensure it stays up-to-date.
     Oddjob: It is a free open source Java job scheduler and job tool kit which provides some order and visibility to all the batch files and cron jobs that tie an enterprise's critical business processes together.

References•         http://www.sauronsoftware.it/projects/cron4j/index.php

你可能感兴趣的:(quartz)