国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Jcrontab - java定時(shí)程序進(jìn)階學(xué)習(xí)

2019-11-18 11:15:29
字體:
供稿:網(wǎng)友

    初學(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è):

  • 普通文件來存儲(chǔ)
  • 數(shù)據(jù)庫(kù)存儲(chǔ)
  • XML文件存儲(chǔ)

在程序中也可以添加要執(zhí)行的數(shù)據(jù)源,比如執(zhí)行本地的應(yīng)用程序等。下面分別介紹一下。在這之前,先介紹一下有關(guān)Jcrontab用到的配置文件:

  • jcrontab.PRoperties配置文件,這是用來啟動(dòng)Jcrontab的必需文件。在Jcrontab的發(fā)布包中已經(jīng)有一個(gè)完整格式的jcrontab.properties文件了,里面有它的樣例,我們只需要根據(jù)我們自己的應(yīng)用需要,來使用具體的配置屬性,來構(gòu)造自己的jcrontab.properties。
  • 還有一個(gè)配置文件就是我們自己的定時(shí)配置文件。(若我們采用的數(shù)據(jù)源是文件的話,就需要這個(gè)了)這個(gè)文件是用來寫負(fù)責(zé)處理定時(shí)程序的,里面按照規(guī)定好的時(shí)間來處理規(guī)定好的類或類的方法。
  • 若我們采用數(shù)據(jù)庫(kù)存儲(chǔ)數(shù)據(jù)源的話,那么,我們就需要配置一個(gè)數(shù)據(jù)庫(kù)的信息,在jcrontab.properties文件中已經(jīng)有一個(gè)例子了,我們可以改成我們需要的數(shù)據(jù)庫(kù)配置信息,這個(gè)很輕易。
  • 若要采用的是XML形式的文件,那么我們要指定數(shù)據(jù)源是XML的,同時(shí)指定處理XML的解析器,這里用的是Apache的Xerces。
  • 在程序中隨時(shí)可以添加數(shù)據(jù)源,通過Crontab中的newTask方法,就可以隨時(shí)添加。

  通過文件記錄數(shù)據(jù)源
下面通過例子,我們來講解一個(gè)具體的Jcrontab程序,看它是如何定時(shí)處理程序的。
運(yùn)行程序的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來執(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作品展示 特效 摳圖
  帶測(cè)試運(yùn)行的NumTest類和LetterTest類:



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 延川县| 赣州市| 江永县| 石林| 辰溪县| 贵德县| 大丰市| 航空| 资阳市| 蒙城县| 宜川县| 时尚| 株洲市| 花莲市| 八宿县| 承德县| 平遥县| 婺源县| 克东县| 梅河口市| 平江县| 安龙县| 林口县| 府谷县| 巴青县| 双桥区| 双城市| 洪雅县| 菏泽市| 中西区| 广饶县| 南丹县| 锡林浩特市| 静安区| 贵溪市| 万载县| 庆元县| 鸡泽县| 靖边县| 合江县| 清河县|