<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<!-- OCS cron触发器,每分钟触发一次 -->
<bean id="ocsTask" class="org.quartz.JobDetail">
<property name="name" value="dailyTaskOcs" />
<property name="group" value="dailyJobsOcs" />
<property name="jobClass" value="com.genertech.quartz.PdaQuartz" />
</bean>
<bean id="cronTriggerOcs" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="ocsTask" />
<property name="cronExpression" value="0 0/2 * * * ?" />
</bean>
<!-- 报警 cron触发器,每分钟触发一次 -->
<bean id="ocsAlerm" class="org.quartz.JobDetail">
<property name="name" value="dailyTaskOcsAlerm" />
<property name="group" value="dailyJobsOcsAlerm" />
<property name="jobClass" value="com.genertech.quartz.OcsAlermQuartz" />
</bean>
<bean id="cronTriggerOcsAlerm" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="ocsAlerm" />
<property name="cronExpression" value="0 0/1 * * * ?" />
</bean>
<!-- SEA cron触发器 ,根据规则,自动生成维护计划 ,每天凌晨12:30触发一次 -->
<bean id="seaTask" class="org.quartz.JobDetail">
<property name="name" value="dailyTaskSea" />
<property name="group" value="dailyJobsSea" />
<property name="jobClass" value="com.genertech.quartz.SeaQuartz" />
</bean>
<bean id="cronTriggerSea" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="seaTask" />
<property name="cronExpression" value="0 30 0 * * ?"/>
</bean>
<!-- 不稳定设备 cron触发器,每隔一小时扫一次数据,计算不稳定设备 -->
<bean id="faultTask" class="org.quartz.JobDetail">
<property name="name" value="faultTaskSea" />
<property name="group" value="faultJobsSea" />
<property name="jobClass" value="com.genertech.quartz.FaultQuartz" />
</bean>
<bean id="cronTriggerFault" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="faultTask" />
<property name="cronExpression" value="0 0 0/1 * * ?" />
</bean>
<!-- 调度器 -->
<bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="cronTriggerOcs" />
<ref local="cronTriggerSea" />
<ref local="cronTriggerFault"/>
<ref local="cronTriggerOcsAlerm"/>
</list>
</property>
<!-- 配置入口参数中service -->
<property name="schedulerContextAsMap">
<map>
<entry key="dao" value-ref="baseDao" />
<entry key="ocsService" value-ref="ocsService" />
<entry key="seaService" value-ref="seaService" />
<entry key="alermService" value-ref="alermService" />
</map>
</property>
</bean>
</beans>
package com.genertech.core.quartz;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import com.genertech.core.app.common.ApplicationContext;
/**
*
*/
public class BizSchedulerManager {
private static Log _log = LogFactory.getLog(BizSchedulerManager.class);
private static BizSchedulerManager manager = null;
// private QuartzManager quartz = null;
private Scheduler scheduler = null;
private BizSchedulerManager() throws SchedulerException {
// quartz = new QuartzManager();
scheduler = StdSchedulerFactory.getDefaultScheduler();
_log.info("开始定时任务初始化");
//系统启动时判断是否存在改定时任务,不过不存在,就注册一个
String jobname = "quartzManager";
String jobgroup = "manager";
String triggername = "manager";
//表达式从配置文件中取
String expression = ApplicationContext.singleton().getValueAsString("quartz.manager.expression");
if(null == expression || "".equals(expression)){
//默认是一分钟
expression = "0 0/1 * * * ?";
}
String beanName = "quartzManager";
CronTriggerImpl trigger;
try {
//scheduler.clear();
//scheduler.start();
JobKey jobKey = new JobKey(jobname,jobgroup);
if(scheduler.checkExists(jobKey)){
_log.info("============定时任务quartzManager 已经存在");
}else{
trigger = new CronTriggerImpl();
trigger.setCronExpression(expression);
trigger.setName(triggername);
trigger.setGroup(Scheduler.DEFAULT_GROUP);
trigger.setJobGroup(jobgroup);
trigger.setJobName(jobname);
//trigger = new CronTriggerImpl(triggername, Scheduler.DEFAULT_GROUP, jobname, jobgroup, expression);
//JobDetail jobdetail = new JobDetailImpl(jobname, jobgroup, QuartzJob.class);
JobDetailImpl jobdetail = new JobDetailImpl();
jobdetail.setName(jobname);
jobdetail.setGroup(jobgroup);
jobdetail.setJobClass(QuartzJob.class);
jobdetail.getJobDataMap().put("beanName", beanName);
_log.info("============定时任务quartzManager 不存在,重新创建");
scheduler.scheduleJob(jobdetail, trigger);
}
//scheduler.addJob(jobdetail, true);
//scheduler.scheduleJob(jobdetail, trigger);
} catch (Exception e) {
_log.error(e.getMessage(), e);
}
scheduler.start();
}
public static BizSchedulerManager getInstance() throws Exception {
if (manager == null) {
try {
manager = new BizSchedulerManager();
} catch (SchedulerException e) {
throw new Exception(e);
}
}
return manager;
}
public static void main(String[] args) {
}
}