`
cainiaoyu
  • 浏览: 22325 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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
分享到:
评论

相关推荐

    cron4j定时任务框架-cron4j.zip

    import it.sauronsoftware.cron4j.Scheduler; import it.sauronsoftware.cron4j.Task; import it.sauronsoftware.cron4j.TaskExecutor; public class Cron4jExample { public static void main(String[] args) { ...

    cron4j 2.2

    使用cron4j进行任务调度的基本流程包括创建`Scheduler`实例、安排任务、启动和停止调度。例如,以下代码创建了一个每分钟执行一次的任务: ```java Scheduler s = new Scheduler(); s.schedule("* * * * *", new...

    cron4j 2.2.3源代码,API下载集合包

    cron4j的核心是它的调度器(Scheduler),它负责启动、停止和管理任务。用户可以定义各种时间表达式来决定任务何时执行。这些时间表达式是由一系列字段组成的字符串,类似于经典的crontab格式,例如"0 0 * * * ?...

    cron4j-开源

    cron4j是一个强大的开源任务调度库,专门为Java平台设计,其设计灵感来源于UNIX系统的cron守护进程。cron4j的核心功能是让开发者能够灵活地安排在特定时间或周期性执行的任务,而无需深入理解操作系统级别的定时器。...

    getl_example_src-1.1.25.zip_The Rules_scheduler

    cron4j is a scheduler for the Java platform which is very similar to the UNIX cron daemon. With cron4j you can launch, within your Java applications, any task you need at the right time, according to ...

    spring定时任务所需jar包 slf4j-api-1.5.6.jar 和slf4j-log4j12-1.5.6.jar

    `slf4j-log4j12-1.5.6.jar`是SLF4J到Log4j的具体绑定,它使得SLF4J API的调用能转换成Log4j的日志输出。 3. **配置Log4j** 为了自定义日志行为,如输出级别、文件路径、格式等,你需要一个`log4j.properties`或`...

    简单流畅的 Go cron 调度 这是来自.zip

    = nil { // handle error } // add a job to the scheduler j, err := s.NewJob( gocron.DurationJob( 10*time.Second, ), gocron.NewTask( func(a string, b int) { // do things }, "hello", 1,

    spring mvc定时任务需要的所有jar包,包括slf4j、log4j

    引入`slf4j-api.jar`是第一步,它包含了SLF4J的API接口,然后还需要一个具体的日志实现,如`slf4j-log4j12.jar`,它桥接了SLF4J与Log4j。 `log4j`是Apache组织的一个开源项目,提供了一种灵活且强大的日志记录机制...

    quartz 定时任务开发需要jar包

    Quartz是一款强大的、开源的Java定时任务框架,用于在Java应用程序中实现复杂的调度...同时,由于log4j-1.2.14版本较旧,现在一般推荐使用更新的log4j2或者其他现代的日志框架,如Logback,以获取更好的性能和特性。

    java 定时任务管理框架

    因此,出现了如Quartz、Spring Scheduler、Cron4j等第三方框架,它们提供了更强大、更灵活的定时任务解决方案。 二、Citic Scheduler特性 1. **易于集成**:Citic Scheduler设计为轻量级框架,能够快速地整合到现有...

    Quartz_Scheduler_Example_Programs_and_Sample_Code.pdf Version 2.2.1

    - **日志记录**:使用日志记录工具(如 Log4j)记录异常信息,以便后续追踪和分析。 #### Quartz 代码片段 此外,文档还提供了一些实用的代码片段,可以帮助开发者快速解决常见的编程问题。 - **使用多个(非集群...

    Wisp:具有最小占用空间和简单API的简单Java Scheduler库

    Wisp仅重30Kb,零依赖性,除了用于日志记录的SLF4J。 它将尝试仅创建将要使用的线程:如果一个线程足以运行所有作业,则将仅创建一个线程。 通常仅在必须同时运行两个作业时才创建第二个线程。 调度程序的精度将取...

    quartz_2.2.1核心包+依赖包.zip

    1. `slf4j-log4j12-1.6.6.jar`:这是SLF4J(Simple Logging Facade for Java)的一个绑定包,将SLF4J接口绑定到Log4j日志实现。SLF4J提供了一种在不同的日志框架之间切换的机制,而Log4j则是一个广泛应用的日志记录...

    Java Quartz自动调度

    3. `log4j-1.2.9.jar`:Log4j是一个流行的Java日志记录框架,用于记录应用程序的运行日志。在使用Quartz时,日志记录可以帮助开发者调试和监控任务的执行情况。 4. `commons-logging-1.0.4.jar`:Apache Commons ...

    quartz-2.2.3 需要的5个jar包

    最后,`slf4j-log4j12-1.7.7.jar`是SLF4J的一个绑定实现,将SLF4J接口与Log4j 1.2.x结合在一起。这意味着在你的Quartz项目中,你可以通过SLF4J API使用Log4j进行日志记录,而无需直接引用Log4j的API。 为了在项目中...

    quartz2所需jar包

    3. **slf4j-log4j12-1.6.1.jar**:这是SLF4J的一个绑定实现,将SLF4J接口与Log4j日志框架连接起来。这意味着,当你的应用程序使用SLF4J进行日志记录时,这些记录会被转发到Log4j进行处理。同样,这个版本也是1.6.1,...

    Quartz2.2.1的15个例子的代码

    Quartz允许自定义日志框架,如使用SLF4J或Log4j进行日志记录。 14. **Job状态** Job可以声明为`Durable`(持久化),即使Scheduler关闭,Job的信息也会被保存。 15. **定时表达式** Cron表达式是强大的定时工具...

    quartzMonitor.

    - 日志记录是必不可少的,Quartz支持多种日志框架,如Log4j、SLF4J,用于记录作业执行情况和异常信息。 综上所述,"quartzMonitor."项目是一个集成了Quartz调度功能,并且允许用户通过页面配置Cron表达式来控制...

    Java语言定时调度任务之实现.zip

    Cron4J的灵活性和可配置性使得它在某些场景下比Quartz更适合。 8. **Apache Commons Daemon和Procrun** 当需要在Windows服务中运行Java定时任务时,Apache Commons Daemon和Procrun可以将Java应用程序转换为...

    quartz所需的基本jar包

    2. `logback-classic.jar`/`log4j.jar`:具体的日志实现库,如Logback或Log4j,SLF4J的绑定实现。 3. `common-lang3.jar`:Apache Commons Lang库,提供了一些Java语言的实用工具类。 4. `common-collections.jar`:...

Global site tag (gtag.js) - Google Analytics