初學(xué)Jcrontab感覺非常好,用它來做一個(gè)定時(shí)程序非常輕易,而且方便。有關(guān)Jcrontab的介紹和它的定時(shí)文件的格式,在前面的那篇文章已經(jīng)介紹過了,這里再來講解一下它在程序中的具體應(yīng)用。在這里,我們先約定數(shù)據(jù)源這個(gè)概念,“數(shù)據(jù)源”(我暫且這樣稱呼),它主要是用來由Jcrontab按照定時(shí)規(guī)則要處理的類和程序,這可以是多個(gè),也可以是一個(gè),我按照J(rèn)crontab提供的方法通常是將它寫到文件中,xml文件或數(shù)據(jù)庫(kù)中。這樣,按照J(rèn)crontab的規(guī)則,提供給Jcrontab這些數(shù)據(jù)源就可以使用Jcrotab的定時(shí)功能了。
根據(jù)Jcrontab存儲(chǔ)的不同的數(shù)據(jù)源,我們可以分成以下幾個(gè):
在程序中也可以添加要執(zhí)行的數(shù)據(jù)源,比如執(zhí)行本地的應(yīng)用程序等。下面分別介紹一下。在這之前,先介紹一下有關(guān)Jcrontab用到的配置文件:
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來執(zhí)行具體的操作
* 啟動(dòng)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();
}
}
/**
* 關(guān)閉Jcrontab的執(zhí)行
* @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();
}
/**
* 根據(jù)不同的參數(shù)執(zhí)行不同的定時(shí)程序,可選參數(shù)可從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]);
}
/**
* 執(zhí)行數(shù)據(jù)庫(kù)的操作
* @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);
}
/**
* 執(zhí)行本地的應(yīng)用程序的操作
* @param parameters
*/
protected void executeAppliction(String[] parameters) {
NativeExec.main(parameters);
}
/**
* 將執(zhí)行的文件發(fā)送為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方法來實(shí)現(xiàn)Jcrontab的實(shí)現(xiàn)
* @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實(shí)例教程 Photoshop cs教程 濾鏡 鼠繪 Photoshop照片處理 Photoshop視頻教程 Photoshop作品展示 特效 摳圖
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注