quzrtz.xml 此文件为定时任务配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- KPI 考核每天 夜晚 12:00 扫描后,将第二天要执行的任务放在 定时任务池中 以便第二天准时执行 -->
<bean id="informationPushTask" class="com.icloudmoosoft.system.timer.kpiplan.InformationPushTask">
<property name="scheduler" ref="schedulerFactory"/>
<property name="baseDao" ref="BaseDao"></property>
</bean>
<!-- 配置定时任务,用于初始化定时器 -->
<bean id="executionJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="informationPushTask"/>
</property>
<property name="targetMethod">
<value>initJobTrigger</value>
</property>
<property name="concurrent" value ="false"/>
</bean>
<bean id="initTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="executionJobDetail"/>
</property>
<property name="cronExpression">
<value>0 42 16 ? * *</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init=‘false‘那么容器启动就会执行调度程序 -->
<bean id="schedulerFactory" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="CooperateManagerTaskDoTime"/> <!-- 扫描申请事项的任务 -->
<ref bean="InitAttendanceManagerTaskDoTime"/> <!-- 每年初始化 假期管理中的数据 加班,请假清空,年假初始化 -->
<ref bean="initTrigger"/>
</list>
</property>
</bean>
</beans>
public class InformationPushTask {
//获得工厂
private Scheduler scheduler;
private BaseDao baseDao;
public void setScheduler(Scheduler scheduler) {
this.scheduler = scheduler;
}
public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
//初始化数据库任务
public void initJobTrigger(){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("初始化数据 start :"+sf.format(new Date()));
public class JobInformationPushTask implements Job{
private BaseDao baseDao;
public void executionTask(Integer iPId){
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("执行任务开始。。。"+iPId+"__dateTime:"+sf.format(new Date()));
}
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try{
baseDao=(BaseDao)context.getScheduler().getContext().get("baseDao");
if(null!=context){
//ID 不能用context.getScheduler().getContext() 来传 除非你只有一个任务,因为工厂只初始化一次
String strId= context.getJobDetail().getName();
executionTask(Integer.parseInt(strId .substring(strId.lastIndexOf("_")+1,strId.length())));
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
//添加第二天需要执行的任务到 任务池 里面
List<KpiPlanBatch> listMap = baseDao.selectList("hr.pjxt.kpiplanbatch.kpiPlanBatch");
System.out.println("listMap.size():"+listMap.size());
if(!listMap.isEmpty()){
try {
for (KpiPlanBatch kplan : listMap) {
insertTrigger(kplan.getJobTime(),kplan.getbNum());
System.out.println("插入定时任务信息:"+kplan.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
}
//移除初始化触发器
try {
scheduler.unscheduleJob("initTrigger", Scheduler.DEFAULT_GROUP);
scheduler.start();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
//下达任务
public boolean insertTrigger(Date dtime,Integer id){
//验证是否有任务
try {
SimpleTrigger checkSimpleTrigger = (SimpleTrigger)scheduler.getTrigger("trigger_" + id, Scheduler.DEFAULT_GROUP);
if(null != checkSimpleTrigger){
return false;
}
} catch (SchedulerException e1) {
e1.printStackTrace();
}
if(null!=dtime){
try {
//动态及动态产生一个人任务 跟一个触发器 交由Scheduler工厂管理
//1.新建一个任务 2.分配入默认的组 3.执行工作的BEAN 需实现JOB 接口 并实现execute方法
JobDetail jobDetail = new JobDetail("reportJob_" + id, Scheduler.DEFAULT_GROUP,JobInformationPushTask.class);
//复杂的定时任务
SimpleTrigger trigger = new SimpleTrigger("trigger_" + id,Scheduler.DEFAULT_GROUP,dtime,null,0,0L);
//JOB的BEAN 传值 可以是DAO 也可以是connection
scheduler.getContext().put("baseDao",baseDao);
//启动新增定时器任务
scheduler.scheduleJob(jobDetail, trigger);
return true;
} catch (Exception e) {
//你懂的
revokeTask(id);
e.printStackTrace();
}
}
return false;
}
//删除任务
public void revokeTask(Integer ipId){
try {
//移除触发器
SimpleTrigger simpleTrigger = (SimpleTrigger)scheduler.getTrigger("trigger_" + ipId, Scheduler.DEFAULT_GROUP);
if(null != simpleTrigger){
scheduler.unscheduleJob(simpleTrigger.getName(), Scheduler.DEFAULT_GROUP);
}
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}