Quartz的maven依赖
<!-- quartz 的jar --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.1</version> </dependency>
方法一
基于特定基类 编写任务类,该类需要继承自QuartzJobBean
package com.sys.task; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.quartz.QuartzJobBean; /** * @Author:jimisun * @Description: * @Date:Created in 9:36 2018/8/1 * @Modified By: */ public class QuartzTestJob extends QuartzJobBean { public QuartzTestJob() { super(); } private static final Logger logger = LoggerFactory.getLogger(QuartzTestJob.class); private Integer timeout; //调度工厂实例化后,经过timeout时间开始执行调度 public void setTimeout(Integer timeout) { this.timeout = timeout; } @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { logger.info("定时任务1进行中......."); } }
Spring中配置Quartz
<!--开源作业调度框架 org.springframework.scheduling.quartz.JobDetailBean有两个属性. jobClass属性即我们在java代码中定义的任务类. jobDataAsMap属性即该任务类中需要注入的属性值.--> <bean name="quartzTestJob1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.sys.task.QuartzTestJob"/> <property name="jobDataAsMap"> <map> <entry key="timeout" value="0"/> </map> </property> </bean> <!--Cron表达式触发器 Quartz的作业触发器有两种(两种任选其一就行) org.springframework.scheduling.quartz.SimpleTriggerFactoryBean:只支持按照一定频度调用任务,如每隔30分钟运行一次. org.springframework.scheduling.quartz.CronTriggerFactoryBean:支持到指定时间运行一次,如每天12:00运行一次等--> <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="quartzTestJob1"/> <property name="cronExpression" value="0/5 * * * * ?"/> </bean> <!--按照一定频率的触发器--> <!-- <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> <property name="jobDetail" ref="quartzTestJob1"/> <property name="startDelay" value="0"/> <property name="repeatInterval" value="2000"/> </bean>--> <!--配置调度工厂--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!--<ref bean="simpleTrigger"/>--> <ref bean="cronTrigger1"/> </list> </property> </bean>
执行结果
DEBUG [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3] - Calling execute on job DEFAULT.quartzTestJob1 DEBUG [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] - batch acquisition of 1 triggers INFO [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3] - 定时任务1进行中.......
方法二
不基于特定的基类 编写普通的方法类
package com.sys.task; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @Author:jimisun * @Description: * @Date:Created in 9:36 2018/8/1 * @Modified By: */ public class QuartzTestJob3 { private static final Logger logger = LoggerFactory.getLogger(QuartzTestJob3.class); public void executeJob() { logger.info("定时任务3进行中......."); } }
在spring配置文件中配置作业类MethodInvokingJobDetailFactoryBean、作业调度的触发方式(触发器)、调度工厂
<!--MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject指定任务类.targetMethod指定运行的方法.-->
<bean name="quartzTestJob3" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <bean class="com.sys.task.QuartzTestJob3"/> </property> <property name="targetMethod" value="executeJob"/> <!--作业不并发调度--> <property name="concurrent" value="false"/> </bean>
<!--Cron表达式触发器--> <bean id="cronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="quartzTestJob3"/> <property name="cronExpression" value="0/5 * * * * ?"/> </bean>
<!--配置调度工厂--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!--<ref bean="simpleTrigger"/>--> <ref bean="cronTrigger1"/> </list> </property> </bean>
执行结果
DEBUG [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-6] - Calling execute on job DEFAULT.quartzTestJob3 INFO [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-6] - 定时任务3进行中....... DEBUG [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] - batch acquisition of 1 triggers
文章中测试学习来源自博文https://www.cnblogs.com/hafiz/p/6159280.html