建立一个时序调度程序的过程如下:
1) 配置Job及JobDetail Bean,定义执行某个类里的特定方法;
2) 配置Trigger Bean,定义触发JobDetail的时间规律;
3) 配置SchedulerFactoryBean负责调度实际的Trigger;时序调度的运行模式有两种:
1) 一种是在某个特定时间自动运行,例如每天凌晨2点备份数据、每月初1号统计上月的数据等,我们称之为定时调度;
Job类:
简单例子,spring配置文件我用的内部bean,根据个人爱好吧...
1) 配置Job及JobDetail Bean,定义执行某个类里的特定方法;
2) 配置Trigger Bean,定义触发JobDetail的时间规律;
3) 配置SchedulerFactoryBean负责调度实际的Trigger;时序调度的运行模式有两种:
1) 一种是在某个特定时间自动运行,例如每天凌晨2点备份数据、每月初1号统计上月的数据等,我们称之为定时调度;
2)另一种是在服务启动一段时间后开始运行,再每隔一段时间再次运行,如系统监控程序每个10分钟就要测试一下数据库是否连接正常,我们称之为重复调度;
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!--
建立一个时序调度程序的过程如下:
1) 配置Job及JobDetail Bean,定义执行某个类里的特定方法;
2) 配置Trigger Bean,定义触发JobDetail的时间规律;
3) 配置SchedulerFactoryBean负责调度实际的Trigger;时序调度的运行模式有两种:
1) 一种是在某个特定时间自动运行,例如每天凌晨2点备份数据、每月初1号统计上月的数据等,我们称之为定时调度;
2)另一种是在服务启动一段时间后开始运行,再每隔一段时间再次运行,如系统监控程序每个10分钟就要测试一下数据库是否连接正常,我们称之为重复调度;
-->
<!-- 第一种集成方式:MethodInvokingJobDetailFactoryBean,并且job类,直接是pojo类,与普通类没有区别 -->
<!-- 定义具体的任务类 -->
<bean id="job_common" class="javacommon.util.CommJob" />
<!-- 全部定义到内部bean中,多个任务 -->
<bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<bean class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="job_common" />
</property>
<property name="targetMethod">
<value>quartzMethod_001</value>
</property>
</bean>
</property>
<property name="cronExpression" value="0/1 * * * * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="job_common" />
</property>
<property name="targetMethod">
<value>quartzMethod_002</value>
</property>
</bean>
</property>
<property name="cronExpression" value="0/2 * * * * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="job_common" />
</property>
<property name="targetMethod">
<value>quartzMethod_003</value>
</property>
</bean>
</property>
<property name="cronExpression" value="0/3 * * * * ?"/>
</bean>
</list>
</property>
</bean>
</beans>
Job类:
/**
* 文件名:CommJob.java
* 版权:Copyright 2014-2015 BuyanTech.All Rights Reserved.
* 描述: 调度任务类
* 修改人:Bill
* 修改时间:2014/11/20
* 修改内容: 无
*/
package javacommon.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.springframework.beans.factory.annotation.Autowired;
import com.buyantech.campaign.service.CarUserManager;
/**
* 普通调度任务类,将系统中需要调度的方法放到此类中
* @author Bill
* @since V1.0 2014/01/09
*/
public class CommJob {
@Autowired
private CarUserManager carUserManager;
private static long quartzMethodCount001 = 0;
private static long quartzMethodCount002 = 0;
private static long quartzMethodCount003 = 0;
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
public void quartzMethod_001(){
System.out.println("CommJob.quartzMethod_001():"+getCurDateOfFormat() + " 调度 "+ ( quartzMethodCount001 ++) +" 次 !" );
}
public void quartzMethod_002(){
System.out.println("CommJob.quartzMethod_002():"+getCurDateOfFormat() + " 调度 "+ ( quartzMethodCount002 ++) +" 次 !" );
}
public void quartzMethod_003(){
System.out.println("CommJob.quartzMethod_003():"+getCurDateOfFormat() + " 调度 "+ ( quartzMethodCount003 ++) +" 次 !" );
}
// 返回当前时间
public String getCurDateOfFormat(){
return sdf.format(Calendar.getInstance().getTime());
}
}
简单例子,spring配置文件我用的内部bean,根据个人爱好吧...