- 浏览: 20877 次
- 性别:
- 来自: 杭州
最新评论
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 426上一篇介绍Java提供synchr ... -
Java集合类(Set和List)
2014-04-07 21:15 571java提供两种存储对象的方式:数组和集合类。 两者的主要区别 ... -
Java中两个集合工具类:Collections和Arrays
2014-04-07 21:08 1100为了方便的对Array对象、Collection对象进行操作, ... -
Java-io小结
2014-04-07 20:37 433Java处理的数据流主要分为字节流和字符流。字符流处理的单元为 ... -
SQL小结
2014-04-07 16:29 487<转>[size=medium][/size] 结 ... -
hibernate缓存
2014-03-18 18:52 369hibernate缓存深入详解 -
SSH总结
2014-03-17 18:39 470struts hibernate spring -
sql order by 排序控制
2013-10-17 16:49 490SELECT * FROM table ta order ... -
在线代码格式化
2013-08-26 16:42 578http://www.ostools.net/ -
判断java中string型变量是否为空
2013-08-12 19:02 515<s:if test='actionItem.deleg ... -
多浏览器兼容问题 IE Firefox Chrome
2013-08-12 18:39 1355[size=x-large]在测试与各个浏览器兼容性的 ... -
Integrating Jasypt with Hibernate
2012-06-25 18:29 1935[size=medium]Integrating Jasypt ...
相关推荐
cron4j 2。2 介绍文档。
android客户端需要定时调用服务器端的URI Rest获取数据,以往用while+sleep控制起来很麻烦,指不定的各种异常,所以使用到了Cron4j。
cron4j定时任务框架-cron4j
cron4j 是一个Java的任务调度框架,类似于UNIX系统下的crontab: 1.源代码 2.API
PHP Cron排程器 这是一个与框架无关的cron... " peppeocchi/php-cron-scheduler " : " 4.* " } } Scheduler V4要求php> = 7.3,对于≤7.3和7.1的php版本,请使用对于≤7.1的php版本,请使用。 这个怎么运作 在项目的
<groupId>it.sauronsoftware.cron4j <artifactId>cron4j <version>2.2.5 <packaging>jar</packaging>
cron4j是Java平台的调度程序,它与UNIX cron守护程序非常相似。 使用cron4j,您可以根据一些简单的规则,在适当的时间从Java应用程序中启动所需的任何任务。
cron-utils-scheduler 一个基于cron-utils库的Java作业调度程序。 支持多种cron格式-甚至自定义格式! 优先级等待队列:按日期排序的作业:最新的优先。 等待直到拉到执行队列。 优先级EXECUTING队列:按日期排序...
jfinal开发实现的,监测公司业务网站服务列表异常时,发微信模板消息通知,cron4j定时任务监测
用于CakePHP 3的Cron Scheduler插件 目录 介绍 CakeScheduler允许您直接从PHP文件编写cron作业。 它适用于CakePHP shell以及任何其他有效PHP代码。 基本上,它替代了常规的crontab文件。 为什么使用它 编写cron作业...
按需下载
一个Java库,可使用cron表达式轻松调度命令。 使用来自Quartz的cron表达式解析器和来自netty的HashWheelTimer。 处理DST冲突,命令异常,执行重叠。
Aoe_Scheduler, Magento的Cron调度模块 Magento的 AOE调度程序 AOE调度器位于on功能的默认位置,可以以管理工作时间,并深入了解背景中的内容。 除了AOE调度器提供了一些功能,比如cli和web服务接口,优化服务器上的...
redis_cron redis_cron是Redis一个基于cron表达式的简单作业调度程序,它在redis中作为模块运行。 它使用与常规cron相似的语法,并允许您直接在redis上调度redis命令。 这个项目的灵感来自PostgreSQL扩展 。 由于...
Cron Expression:一个cron表达式解析器
Spring给程序猿们带来了许多便利。下面这篇文章主要给大家介绍了关于Spring @Scheduler使用cron表达式时的执行问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
安装将此添加到应用程序的shard.yml : dependencies : cron_scheduler : github : kostya/cron_scheduler用法require " cron_scheduler "require " logger "L = Logger .new STDOUTCronScheduler .define do # ...
rx-cron-调度程序这里的主要想法是将 Quartz ( ) 日历和 cron 添加到 rx-java ( ) 调度... 使用渐变: compile ' com.ullink.rx:rx-cron-scheduler:1.5 '用法 static class SideEffectTask implements Action0{ @Overr
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 ...
Ruby的作业调度程序(at,cron,in和每个作业)。 它使用线程。 注意:也许您正在寻找的? (特别是如果你使用这是上鲁弗斯调度2.0.24) 快速开始: # quickstart.rb require 'rufus-scheduler' scheduler = ...