文章目录
- 1. 必不可少jar包依赖
- 2. 编写任务调度类01
- 3. 编写任务调度类02
- 4. quartz配置文件
- 5. 测试service
- 6. 效果图
开发环境 | 版本 |
---|---|
jdk | 1.8 |
Maven | 3.6.1 |
spring | Spring 4.2.6.RELEASE |
Idea | 2019 |
1. 必不可少jar包依赖
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.2.6.RELEASE</version></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.3.2</version></dependency>
2. 编写任务调度类01
package com.gblfy.order.controller;package com.gblfy.order.controller;import com.gblfy.order.service.impl.QuartzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;/*** 基于Spring整合Quartz进行完成定时任务*/
@Component
public class FirstCron {@Autowiredprivate QuartzService quartzService;private static final Logger logger = LoggerFactory.getLogger(FirstCron.class);protected void executeJob() {quartzService.test();logger.info("定时任务1进行中.......");}
}
3. 编写任务调度类02
package com.gblfy.order.controller;import com.gblfy.order.service.impl.QuartzService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;/*** 基于Spring整合Quartz进行完成定时任务*/
@Component
public class SecondCron {@Autowiredprivate QuartzService quartzService;private static final Logger logger = LoggerFactory.getLogger(SecondCron.class);protected void executeJob() {quartzService.test2();logger.info("定时任务2进行中.......");}
}
4. quartz配置文件
在spring配置文件中配置作业类MethodInvokingJobDetailFactoryBean、作业调度的触发方式(触发器)、调度工厂
applicationContext-quartz.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.xsd"><bean name="firstCron" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><bean class="com.gblfy.order.controller.FirstCron"/></property><property name="targetMethod" value="executeJob"/><!--作业不并发调度--><property name="concurrent" value="false"/></bean><bean name="secondCron" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><bean class="com.gblfy.order.controller.SecondCron"/></property><property name="targetMethod" value="executeJob"/><!--作业不并发调度--><property name="concurrent" value="false"/></bean><!--按照一定频率的触发器--><!--<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><property name="jobDetail" ref="thirdCron"/><property name="startDelay" value="0"/><property name="repeatInterval" value="2000"/>
</bean>--><!--Cron表达式触发器--><bean id="firstCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="firstCron"/><property name="cronExpression" value="0/2 * * * * ?"/></bean><!--Cron表达式触发器--><bean id="secondCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="secondCron"/><property name="cronExpression" value="0/5 * * * * ?"/></bean><!--配置调度工厂--><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><!--<ref bean="simpleTrigger"/>--><ref bean="firstCronTrigger"/><ref bean="secondCronTrigger"/></list></property></bean>
</beans>
注意:MethodInvokingJobDetailFactoryBean,有两个关键属性:
targetObject指定任务类.
targetMethod指定运行的方法.
5. 测试service
package com.gblfy.order.service.impl;import com.gblfy.order.controller.FirstCron;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;@Service
public class QuartzService {private static final Logger logger = LoggerFactory.getLogger(FirstCron.class);public void test() {logger.info("执行定时任务1 业务逻辑.......");}public void test2() {logger.info("执行定时任务2 业务逻辑.......");}
}