1.quartz下载链接网址:http://www.quartz-scheduler.org/
2.点击download
3.这里下载2.2版本
4.下载成功后解压
5.导包
6.新建普通java类
7.新建配置文件
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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 1.托管要执行的任务 -->
<!-- 要调用的工作类 -->
<bean id="job1" class="com.song.work.job.job1"></bean>
<!-- 2.定义任务列表 -->
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobDtetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 目标执行任务job1-->
<!-- 调用的类 -->
<property name="targetObject"> <ref bean="job1"/> </property>
<!-- 配置任务执行的方法-->
<!-- 调用类中的方法 -->
<property name="targetMethod"> <value>work</value> </property>
</bean>
<!-- 3.定义触发器 -->
<bean id="CronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!-- 3.1.何时触发 使用cronExpression定义执行规则 -->
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 此处的时间自定义 -->
<value>10,15,20,25,30,35,40,45,50,55 * * * * ?</value>
</property>
<!-- 3.2.触发啥 -->
<property name="jobDetail"> <ref bean="jobDtetail1"/></property>
</bean>
<!-- 4.定义调度器-->
<bean id="schedule1" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="CronTrigger1"/>
</list>
</property>
</bean>
</beans>
代码解析:
调度器调动触发器
触发器触发任务列表
任务列表配置任务
报错:
class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
解决方法:
将CronTriggerBean修改为CronTriggerFactoryBean。
成功!