WEB开发网
开发学院WEB开发Jsp Jcrontab - java定时程序进阶学习 阅读

Jcrontab - java定时程序进阶学习

 2008-01-05 18:54:42 来源:WEB开发网   
核心提示: 初学Jcrontab感觉非常好,用它来做一个定时程序非常轻易,Jcrontab - java定时程序进阶学习,而且方便,有关Jcrontab的介绍和它的定时文件的格式,看它是如何定时处理程序的, 运行程序的JCrontabApp类: JCrontabApp类 import org.jcrontab.Crontab;

   初学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());
}
}
photoshop入门教程 Photoshop实例教程 Photoshop cs教程 滤镜 鼠绘 Photoshop照片处理 Photoshop视频教程 Photoshop作品展示 特效 抠图
  带测试运行的NumTest类和LetterTest类:


Tags:Jcrontab java 定时

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接