您的位置:首页 > 其它

默认排除每月的一些天

2012-08-07 10:09 218 查看
package com.cavaness.quartzbook.chapter3;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerUtils;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;
import org.quartz.impl.calendar.MonthlyCalendar;
import org.quartz.impl.calendar.WeeklyCalendar;

/**
* 默认排除每月的一些天
* @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()));

scheduleJob(scheduler, PrintInfoJob.class);

}

/**
* 配置作业,创建触发器
* @param scheduler 调度器
* @param jobClass 作业所在的类
* @throws SchedulerException
*/
public void scheduleJob(Scheduler scheduler, Class jobClass) throws SchedulerException {
MonthlyCalendar monthlyCalendar = new MonthlyCalendar(); // 默认排除每月的一些天

boolean[] excludedDays = new boolean[31];//  The array must non-null and of size greater or equal to 31. The 0 index element represents the first day of the month.
excludedDays[2] = true; // 第三天排除
excludedDays[4] = true;//  第五天排除

monthlyCalendar.setDaysExcluded(excludedDays);

try {
scheduler.addCalendar("bankHolidays", monthlyCalendar, true, true); // 两个true分别表示替换和更新原来的monthlyCalendar
} catch (SchedulerException e) {
log.error("为调度器关联annualCalendar失败!", e);
throw e;
}

Trigger printInfoTrigger = TriggerUtils.makeImmediateTrigger("printInfoTrigger",
SimpleTrigger.REPEAT_INDEFINITELY, 10000);
printInfoTrigger.setCalendarName("bankHolidays"); // 关联触发器和calendar

JobDetail printInfoJobDetail = new JobDetail("printInfoJobDetail", Scheduler.DEFAULT_GROUP, jobClass);

try {
scheduler.scheduleJob(printInfoJobDetail, printInfoTrigger); // 关联触发器和调度器
} catch (SchedulerException e) {
log.error("关联触发器printInfoTrigger和调度器scheduler失败!", e);
throw e;
}

}

public static void main(String[] args) throws SchedulerException {
Listing_4_9 listing_4_9 = new Listing_4_9();
listing_4_9.startScheduler();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐