JAVA类
public class TaskServiceImpl{
public void synchronizeDb(){
System.out.println("Quartz的任务调度!");
}
}
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 定时任务的工作Bean -->
<bean id="quartzJob" class="com.whty.task.service.impl.TaskServiceImpl" />
<!-- 定义生成工作对象的工厂,并为工厂设定目标对象targetObject属性、目标对象的工作方法targetMethod属性 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="quartzJob" />
<property name="targetMethod">
<value>synchronizeDb</value>
</property>
<property name="concurrent" value="false" />
</bean>
<!-- 任务调度计时器,进行定时设置。CronTriggerBean能进行非常精确的定时设置 -->
<bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetail" />
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 0 0 */2 * * ? 每两小时、整点触发 -->
<!-- 0 0/2 * * * ? 每两分钟 -->
<!-- 0/5 * * * * ? 每五秒钟 -->
<!-- 0 15 10 * * ? 每天Y分X点触发 -->
<value>0/15 * * * * ?</value>
</property>
</bean>
<!-- 调度任务触发器,启动定时任务-->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronQuartzClock" />
</list>
</property>
</bean>
</beans>