- 浏览: 22325 次
- 性别:
- 来自: 杭州
最新评论
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
发表评论
-
java-HashMap
2014-04-09 01:17 0HashMap继承AbstractMap并实现Map接口。类图 ... -
Java中多线程与多线程同步
2014-04-07 21:25 0多线程的目的: 多线程实现了程序的并发执行。 Java 中线程 ... -
Java多线程通信
2014-04-07 21:21 459上一篇介绍Java提供synchr ... -
Java集合类(Set和List)
2014-04-07 21:15 593java提供两种存储对象的方式:数组和集合类。 两者的主要区别 ... -
Java中两个集合工具类:Collections和Arrays
2014-04-07 21:08 1136为了方便的对Array对象、Collection对象进行操作, ... -
Java-io小结
2014-04-07 20:37 480Java处理的数据流主要分为字节流和字符流。字符流处理的单元为 ... -
SQL小结
2014-04-07 16:29 568<转>[size=medium][/size] 结 ... -
hibernate缓存
2014-03-18 18:52 416hibernate缓存深入详解 -
SSH总结
2014-03-17 18:39 550struts hibernate spring -
sql order by 排序控制
2013-10-17 16:49 558SELECT * FROM table ta order ... -
在线代码格式化
2013-08-26 16:42 620http://www.ostools.net/ -
判断java中string型变量是否为空
2013-08-12 19:02 554<s:if test='actionItem.deleg ... -
多浏览器兼容问题 IE Firefox Chrome
2013-08-12 18:39 1423[size=x-large]在测试与各个浏览器兼容性的 ... -
Integrating Jasypt with Hibernate
2012-06-25 18:29 2022[size=medium]Integrating Jasypt ...
相关推荐
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进行任务调度的基本流程包括创建`Scheduler`实例、安排任务、启动和停止调度。例如,以下代码创建了一个每分钟执行一次的任务: ```java Scheduler s = new Scheduler(); s.schedule("* * * * *", new...
cron4j的核心是它的调度器(Scheduler),它负责启动、停止和管理任务。用户可以定义各种时间表达式来决定任务何时执行。这些时间表达式是由一系列字段组成的字符串,类似于经典的crontab格式,例如"0 0 * * * ?...
cron4j是一个强大的开源任务调度库,专门为Java平台设计,其设计灵感来源于UNIX系统的cron守护进程。cron4j的核心功能是让开发者能够灵活地安排在特定时间或周期性执行的任务,而无需深入理解操作系统级别的定时器。...
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 ...
`slf4j-log4j12-1.5.6.jar`是SLF4J到Log4j的具体绑定,它使得SLF4J API的调用能转换成Log4j的日志输出。 3. **配置Log4j** 为了自定义日志行为,如输出级别、文件路径、格式等,你需要一个`log4j.properties`或`...
= 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,
引入`slf4j-api.jar`是第一步,它包含了SLF4J的API接口,然后还需要一个具体的日志实现,如`slf4j-log4j12.jar`,它桥接了SLF4J与Log4j。 `log4j`是Apache组织的一个开源项目,提供了一种灵活且强大的日志记录机制...
Quartz是一款强大的、开源的Java定时任务框架,用于在Java应用程序中实现复杂的调度...同时,由于log4j-1.2.14版本较旧,现在一般推荐使用更新的log4j2或者其他现代的日志框架,如Logback,以获取更好的性能和特性。
因此,出现了如Quartz、Spring Scheduler、Cron4j等第三方框架,它们提供了更强大、更灵活的定时任务解决方案。 二、Citic Scheduler特性 1. **易于集成**:Citic Scheduler设计为轻量级框架,能够快速地整合到现有...
- **日志记录**:使用日志记录工具(如 Log4j)记录异常信息,以便后续追踪和分析。 #### Quartz 代码片段 此外,文档还提供了一些实用的代码片段,可以帮助开发者快速解决常见的编程问题。 - **使用多个(非集群...
Wisp仅重30Kb,零依赖性,除了用于日志记录的SLF4J。 它将尝试仅创建将要使用的线程:如果一个线程足以运行所有作业,则将仅创建一个线程。 通常仅在必须同时运行两个作业时才创建第二个线程。 调度程序的精度将取...
1. `slf4j-log4j12-1.6.6.jar`:这是SLF4J(Simple Logging Facade for Java)的一个绑定包,将SLF4J接口绑定到Log4j日志实现。SLF4J提供了一种在不同的日志框架之间切换的机制,而Log4j则是一个广泛应用的日志记录...
3. `log4j-1.2.9.jar`:Log4j是一个流行的Java日志记录框架,用于记录应用程序的运行日志。在使用Quartz时,日志记录可以帮助开发者调试和监控任务的执行情况。 4. `commons-logging-1.0.4.jar`:Apache Commons ...
最后,`slf4j-log4j12-1.7.7.jar`是SLF4J的一个绑定实现,将SLF4J接口与Log4j 1.2.x结合在一起。这意味着在你的Quartz项目中,你可以通过SLF4J API使用Log4j进行日志记录,而无需直接引用Log4j的API。 为了在项目中...
3. **slf4j-log4j12-1.6.1.jar**:这是SLF4J的一个绑定实现,将SLF4J接口与Log4j日志框架连接起来。这意味着,当你的应用程序使用SLF4J进行日志记录时,这些记录会被转发到Log4j进行处理。同样,这个版本也是1.6.1,...
Quartz允许自定义日志框架,如使用SLF4J或Log4j进行日志记录。 14. **Job状态** Job可以声明为`Durable`(持久化),即使Scheduler关闭,Job的信息也会被保存。 15. **定时表达式** Cron表达式是强大的定时工具...
- 日志记录是必不可少的,Quartz支持多种日志框架,如Log4j、SLF4J,用于记录作业执行情况和异常信息。 综上所述,"quartzMonitor."项目是一个集成了Quartz调度功能,并且允许用户通过页面配置Cron表达式来控制...
Cron4J的灵活性和可配置性使得它在某些场景下比Quartz更适合。 8. **Apache Commons Daemon和Procrun** 当需要在Windows服务中运行Java定时任务时,Apache Commons Daemon和Procrun可以将Java应用程序转换为...
2. `logback-classic.jar`/`log4j.jar`:具体的日志实现库,如Logback或Log4j,SLF4J的绑定实现。 3. `common-lang3.jar`:Apache Commons Lang库,提供了一些Java语言的实用工具类。 4. `common-collections.jar`:...