package com.cavaness.quartzbook.chapter3;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.NthIncludedDayTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.StdSchedulerFactory;
/**
* 编程式调度器,每月的第31天执行作业
* @author Kevin
*
*/
public class Listing_4_9 {
private static Log log = LogFactory.getLog(Listing_3_5.class);
/**
* 创建,启动调度器
* @throws SchedulerException
*/
public void startScheduler() throws SchedulerException {
Scheduler scheduler = null;
try {
scheduler = StdSchedulerFactory.getDefaultScheduler();
} catch (SchedulerException e) {
log.error("调度器创建失败!", e);
throw e;
}
try {
scheduler.start();
} catch (SchedulerException e) {
log.error("调度器启动失败!", e);
throw e;
}
log.info("调度器启动时间:" + new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date()));
JobDetail printInfoJobDetail = new JobDetail("printInfoJob",
Scheduler.DEFAULT_GROUP, PrintInfoJob.class); // 为作业取名,分组
NthIncludedDayTrigger printInfoTrigger = new NthIncludedDayTrigger();
printInfoTrigger.setName("printInfoTrigger");
printInfoTrigger.setGroup(Scheduler.DEFAULT_GROUP);
printInfoTrigger.setIntervalType(NthIncludedDayTrigger.INTERVAL_TYPE_MONTHLY);
printInfoTrigger.setN(31); // 每月的第31天执行作业,如果某个月没有31号,此月不执行
printInfoTrigger.setFireAtTime("15:38:30");
try {
scheduler.scheduleJob(printInfoJobDetail, printInfoTrigger);
} catch (SchedulerException e) {
log.error("关联作业printInfoJobDetail和触发器printInfoTrigger失败!", e);
throw e;
}
}
public static void main(String[] args) throws SchedulerException {
Listing_4_9 listing_4_9 = new Listing_4_9();
try {
listing_4_9.startScheduler();
} catch (SchedulerException e) {
log.error("调度器初始化失败!", e);
throw e;
}
}
}
2.编程式调度器,每月的第31天执行作业
最新推荐文章于 2024-03-21 15:31:37 发布