初学jcrontab感觉非常好,用它来做一个定时程序非常容易,而且方便。有关jcrontab的介绍和它的定时文件的格式,在前面的那篇文章已经介绍过了,这里再来讲解一下它在程序中的具体应用。在这里,我们先约定数据源这个概念,“数据源”(我暂且这样称呼),它主要是用来由jcrontab按照定时规则要处理的类和程序,这可以是多个,也可以是一个,我按照jcrontab提供的方法通常是将它写到文件中,xml文件或数据库中。这样,按照jcrontab的规则,提供给jcrontab这些数据源就可以使用jcrotab的定时功能了。
根据jcrontab存储的不同的数据源,我们可以分成以下几个:
- 普通文件来存储
- 数据库存储
- xml文件存储
在程序中也可以添加要执行的数据源,比如执行本地的应用程序等。下面分别介绍一下。在这之前,先介绍一下有关jcrontab用到的配置文件:
- jcrontab.properties配置文件,这是用来启动jcrontab的必需文件。在jcrontab的发布包中已经有一个完整格式的jcrontab.properties文件了,里面有它的样例,我们只需要根据我们自己的应用需要,来使用具体的配置属性,来构造自己的jcrontab.properties。
- 还有一个配置文件就是我们自己的定时配置文件。(若我们采用的数据源是文件的话,就需要这个了)这个文件是用来写负责处理定时程序的,里面按照规定好的时间来处理规定好的类或类的方法。
- 若我们采用数据库存储数据源的话,那么,我们就需要配置一个数据库的信息,在jcrontab.properties文件中已经有一个例子了,我们可以改成我们需要的数据库配置信息,这个很容易。
- 若要采用的是xml形式的文件,那么我们要指定数据源是xml的,同时指定处理xml的解析器,这里用的是apache的xerces。
- 在程序中随时可以添加数据源,通过crontab中的newtask方法,就可以随时添加。
通过文件记录数据源
下面通过例子,我们来讲解一个具体的jcrontab程序,看它是如何定时处理程序的。
运行程序的jcrontabapp类: jcrontabapp类
import org.jcrontab.crontab;
import org.jcrontab.nativeexec;
import org.jcrontab.sendmail;
import org.jcrontab.data.crontabparser;
import org.jcrontab.data.crontabentrybean;
import org.jcrontab.data.crontabentrydao;
import java.io.file;
public class jcrontabapp {
private static crontab cron = null;
private string jcrontabfile = null;
public jcrontabapp() {
}
public string getjcrontabfile() {
return jcrontabfile;
}
public void setjcrontabfile(string jcrontabfile) {
jcrontabfile = jcrontabfile;
}
/**
* 初始化jcrontab,通过指定的jcrontab.properties来执行具体的操作
* 启动jcrontab
*/
protected void init() {
cron = crontab.getinstance();
try {
shutdownhook();
cron.setdaemon(false);
if (null == jcrontabfile)
cron.init();
cron.init(jcrontabfile);
system.out.println("start jcrontab...");
} catch (java.lang.exception e) {
e.printstacktrace();
}
}
/**
* 关闭jcrontab的执行
* @throws exception
*/
protected void shutdownhook() throws exception {
runtime.getruntime().addshutdownhook(new thread() {
public void run() {
system.out.println("shutting down...");
cron.uninit(200);
system.out.println("stoped jcrontab!");
}
});
}
/**
* 判断是否是holiday
* @return
* @throws exception
*/
public boolean isholiday()throws exception{
return cron.isholiday();
}
/**
* 根据不同的参数执行不同的定时程序,可选参数可从main方法中查看
* @param args
* @throws exception
*/
public void exectue(string[] args) throws exception {
if (args.length < 2) {
return;
}
this.setjcrontabfile(args[0]);
init();
if ("database".equals(args[1]))
executedatabase();
else if ("appliction".equals(args[1])) {
string[] parameters = new string[args.length-2];
system.arraycopy(args,2,parameters,0,args.length-2);
cron.newtask("org.jcrontab.nativeexec","main",parameters);
} else if ("javamail".equals(args[1]))
executejavamail(args[2]);
}
/**
* 执行数据库的操作
* @throws exception
*/
protected void executedatabase() throws exception {
crontabparser cp = new crontabparser();
crontabentrybean[] ceb = new crontabentrybean[2];
ceb[0] = cp.marshall("* * * * * com.aweb.test.numtest 123");
ceb[0].setyears("*");
ceb[0].setseconds("0");
ceb[0].setbusinessdays(true);
ceb[0].setid(0);
ceb[1] = cp.marshall("* * * * * com.aweb.test.lettertest 234");
ceb[1].setyears("*");
ceb[1].setseconds("0");
ceb[1].setbusinessdays(true);
ceb[1].setid(1);
crontabentrydao.getinstance().store(ceb);
}
/**
* 执行本地的应用程序的操作
* @param parameters
*/
protected void executeappliction(string[] parameters) {
nativeexec.main(parameters);
}
/**
* 将执行的文件发送为email
* @param sendfilename
* @throws exception
*/
protected void executejavamail(string sendfilename) throws exception {
file sendfile = new file(sendfilename);
sendmail sm = new sendmail();
sm.send(sendfile);
}
/**
* 主要通过main方法来实现jcrontab的实现
* @param args
* @throws exception
*/
public static void main(string args[]) throws exception {
if (args.length < 2) {
system.out.println("usage:the values of args:<-type>[]");
system.out.println("********************************************************");
system.out.println("optional parameters of type:");
system.out.println("-filedata:doing file operating.");
system.out.println("-database:doing database operating.");
system.out.println("-appliction :doing native application execute.");
system.out.println("-javamail :doing javamail operating.");
system.out.println("********************************************************");
system.exit(1);
}
jcrontabapp jp = new jcrontabapp();
jp.exectue(args);
system.out.println(jp.isholiday());
}
}
带测试运行的numtest类和lettertest类:
numtest类 public class numtest {lettertest类
public static void main(string args[]){
for(int i=0;i<10;i++){
system.out.println("time:"+system.currenttimemillis()+" i= "+i);
}
}
} public class lettertest {
public static void main(string args[]) {
system.out.println("time: " + system.currenttimemillis() + " letter: " + "hello world!");
}
}
配置文件jcrontab.properties:
jcrontab.properties文件 ##
#mon may 24 19:12:24 cest 2004
org.jcrontab.version=2.0.rc1
org.jcrontab.log.logger=org.jcrontab.log.log4jlogger
org.jcrontab.log.log4j.properties=log4j.properties
org.jcrontab.data.file = crontab
org.jcrontab.data.datasource = org.jcrontab.data.filesource
org.jcrontab.crontab.refreshfrequency = 3
处理定时程序的crontab文件:
crontab文件 * * * * * com.aweb.test.numtest
* * * * * com.aweb.test.lettertest
然后,我们在命令行输入jcrontab.properties文件的位置,和要处理的类型,这里,我们选filedata,接着运行该类,我们就可以看到它的执行结果:
[2004-10-19 16:10:20,296 lv:debug cl:jcrontab] org.jcrontab.log.log4j.properties : log4j.properties
[2004-10-19 16:10:20,296 lv:debug cl:jcrontab] org.jcrontab.version : 2.0.rc1
[2004-10-19 16:10:20,296 lv:debug cl:jcrontab] org.jcrontab.crontab.refreshfrequency : 3
[2004-10-19 16:10:20,296 lv:debug cl:jcrontab] org.jcrontab.data.file : crontab.properties
[2004-10-19 16:10:20,296 lv:debug cl:jcrontab] org.jcrontab.data.datasource : org.jcrontab.data.filesource
[2004-10-19 16:10:20,296 lv:debug cl:jcrontab] org.jcrontab.log.logger : org.jcrontab.log.log4jlogger
start jcrontab...
[2004-10-19 16:10:20,500 lv:debug cl:jcrontab] interval to sleep : 39984
[2004-10-19 16:11:00,484 lv: info cl:jcrontab] com.aweb.test.numtest#
[2004-10-19 16:11:00,484 lv: info cl:jcrontab] com.aweb.test.lettertest#
[2004-10-19 16:11:00,484 lv:debug cl:jcrontab] interval to sleep : 60000
time:1098173460484 i= 0
time:1098173460484 i= 1
time:1098173460484 i= 2
time:1098173460484 i= 3
time:1098173460484 i= 4
time:1098173460484 i= 5
time:1098173460484 i= 6
time:1098173460500 i= 7
time:1098173460500 i= 8
time:1098173460500 i= 9
time: 1098173460515 letter: hello world!
这样每隔一分钟,就会运行一次numtest类和lettertest类
- 通过数据库记录数据源
在进行数据库的操作前,你需要在数据库中建一张表,这个表在jcrontab中有它的sql语句,events表,它的表结构,可以参考jcrontab提供的sql语句。我们只需要将crontab文件中的属性做一下修改就可以了
修改后的crontab文件: crontab文件增加下面的属性:
org.jcrontab.data.genericsqlsource.driver = org.gjt.mm.mysql.driver这样,我们再在命令行下输入jcrontab.properties文件的位置,和要处理的类型,这里,我们选database,就可以执行我们写好的两个类了。运行时,它是先将这两个待处理的类的数据源,按照规定好的数据库中的字段,一一存入数据库中,然后再从数据库中读取events表中的每一行(这每一行就是待处理的数据源),它就根据这些行定时处理。执行的结果同上。
org.jcrontab.data.genericsqlsource.url = jdbc:mysql://127.0.0.1/netbook
org.jcrontab.data.genericsqlsource.username = root
org.jcrontab.data.genericsqlsource.password =
org.jcrontab.data.datasource = org.jcrontab.data.genericsqlsource - 通过xml来存储数据源
其实和文件存储差不多,主要这里要用到xml的解析器,这里用的是apache xerces来处理的,其它的都一样。
修改后的crontab文件: crontab文件去掉原来的crontab文件中的信息,加上下面的属性
org.jcrontab.data.file = crontab.xml同样在命令行下输入jcrontab.properties文件的位置,和要处理的类型,这里和文件的一样选filedata,来运行该类,执行结果同上。
org.xml.sax.driver=org.apache.xerces.parsers.saxparser
org.jcrontab.data.datasource=org.jcrontab.data.xmlsource - 定时处理程序,还可以用来处理本地的应用程序
根据系统的不同,可以处理不同的应用程序,比如:执行ping的命令,那么你就可以调用jcrontab中已经写好的nativeexec类来执行即可。它主要负责处理本地系统上的应用程序。我们在xml文件中添加一条记录,就可以实现了。
修改后的crontab文件: crontab.xml文件<?xml version="1.0" encoding="gb2312"?>这样执行的结果就是每隔一分钟就会执行一次net send命令(是不是装了个定时炸弹呀!呵呵,不过是在服务器端才运行的哟!)。
<crontab>
<seconds>0</seconds>
<minutes>*</minutes>
<hours>*</hours>
<daysofmonth>*</daysofmonth>
<months>*</months>
<daysofweek>*</daysofweek>
<years>*</years>
<bussinesdays>true</bussinesdays>
<startdate>null</startdate>
<enddate>null</enddate>
<class>com.aweb.test.numtest</class>
<method>main</method>
<description>null</description>
</crontabentry>
<crontabentry id=1>
<seconds>0</seconds>
<minutes>*</minutes>
<hours>*</hours>
<daysofmonth>*</daysofmonth>
<months>*</months>
<daysofweek>*</daysofweek>
<years>*</years>
<bussinesdays>true</bussinesdays>
<startdate>null</startdate>
<enddate>null</enddate>
<class>com.aweb.test.lettertest</class>
<method>main</method>
<description>null</description>
</crontabentry>
<crontabentry id=2>
<seconds>0</seconds>
<minutes>*</minutes>
<hours>*</hours>
<daysofmonth>*</daysofmonth>
<months>*</months>
<daysofweek>*</daysofweek>
<years>*</years>
<bussinesdays>true</bussinesdays>
<startdate>null</startdate>
<enddate>null</enddate>
<class>org.jcrontab.nativeexec</class>
<method>main</method>
<parameters>net send 192.168.0.109 "你好!每一分钟出现一次!呵呵"</parameters>
<description>null</description>
</crontabentry>
</crontab> - 通过email来发送执行的数据源
在jcrontab中提供了javamail的功能,你可以用javamail来实现上面同样的功能。你只需要修改一下配置文件
修改后的crontab文件: crontab文件增加下面的属性
<a href="mailto:org.jcrontab.sendmail.to=yourname@sohu.com">org.jcrontab.sendmail.to=yourname@sohu.com</a>这样它就会将执行的数据源发送到你的邮箱中。不过,目前这个我没有测试成功,我认为主要是由于邮件服务器的原因吧,它总是出现javamail的运行异常。程序方面应该不会有问题,这需要我在以后的学习中继续来完成,也希望大家帮我实现这个功能!
<a href="mailto:org.jcrontab.sendmail.from=myname@sohu.com">org.jcrontab.sendmail.from=myname@sohu.com</a>
org.jcrontab.sendmail.smtp.host=smtp.sohu.com
org.jcrontab.sendmail.smtp.username= yourname
org.jcrontab.sendmail.smtp.password=password
总结:jcrontab提供的定时处理程序的功能不单单是这些,还可以定时处理ejb等高级的组件,在应用服务器中也可以采用servlet的方式来定时处理(个人感觉:它提供的用servlet来定时处理程序的功能不太好!大家去慢慢体会吧!)。还有一些其它方面的功能,还需要继续的学习和探索!
闽公网安备 35060202000074号