avatar

定时任务组件Quartz

本文介绍定时任务组件Quartz

1. Quartz介绍

Quartz是Job scheduling(作业调度)领域的一个开源项目,Quartz既可以单独使用也 可以跟spring框架整合使用,在实际开发中一般会使用后者。使用Quartz可以开发一个 或者多个定时任务,每个定时任务可以单独指定执行的时间,例如每隔1小时执行一次、 每个月第一天上午10点执行一次、每个月最后一天下午5点执行一次等。
官网:http://www.quartz-scheduler.org/

maven坐标

1
2
3
4
5
6
7
8
9
10
<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>

2.Quartz入门案例

本案例基于Quartz和spring整合的方式使用。

第一步 创项目,导依赖

创建maven工程quartzdemo,导入Quartz和spring相关坐标,pom.xml文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF‐8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven‐4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itheima</groupId>
    <artifactId>quartdemo</artifactId>
    <version>1.0‐SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring‐context‐support</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring‐tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <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>
    </dependencies>
</project>

第二步 创建任务类

1
2
3
4
5
6
7
8
9
package com.itheima.jobs;
/**
 * 自定义Job
 */
public class JobDemo {
    public void run(){
        System.out.println("job execute...");
    }
}

第三步 配置自定义Job

提供Spring配置文件spring-jobs.xml,配置自定义Job、任务描述、触发器、调度
工厂等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  <?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:context="http://www.springframework.org/schema/context"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring‐beans.xsd
                       
http://www.springframework.org/schema/mvc                        
http://www.springframework.org/schema/mvc/spring‐
mvc.xsd
                       
http://code.alibabatech.com/schema/dubbo                        
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
                       
http://www.springframework.org/schema/context                        
http://www.springframework.org/schema/context/spring‐context.xsd">
                       
<!‐‐ 注册自定义Job ‐‐>    
    <bean id="jobDemo" class="com.itheima.jobs.JobDemo"></bean>
<!‐‐ 注册JobDetail,作用是负责通过反射调用指定的Job ‐‐>    
    <bean id="jobDetail"
         
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFacto
ryBean">
        <!‐‐ 注入目标对象 ‐‐>
        <property name="targetObject" ref="jobDemo"/>
        <!‐‐ 注入目标方法 ‐‐>
        <property name="targetMethod" value="run"/>
    </bean>
    <!‐‐ 注册一个触发器,指定任务触发的时间 ‐‐>
    <bean id="myTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <!‐‐ 注入JobDetail ‐‐>
        <property name="jobDetail" ref="jobDetail"/>
        <!‐‐ 指定触发的时间,基于Cron表达式 ‐‐>
        <property name="cronExpression">
            <value>0/10 * * * * ?</value>
        </property>
    </bean>
    <!‐‐ 注册一个统一的调度工厂,通过这个调度工厂调度任务 ‐‐>
    <bean id="scheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <!‐‐ 注入多个触发器 ‐‐>
        <property name="triggers">
            <list>
                <ref bean="myTrigger"/>
            </list>
        </property>
    </bean>
</beans>

第四步 编写测试类

1
2
3
4
5
6
7
8
package com.itheima.jobs.com.itheima.app;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
    public static void main(String[] args) {
        new ClassPathXmlApplicationContext("spring‐jobs.xml");
    }
}

执行上面main方法观察控制台,可以发现每隔10秒会输出一次,说明每隔10秒自定义
Job被调用一次

文章作者: 微信:hao_yongliang
文章链接: https://haoyongliang.gitee.io/2019/12/08/%E5%B7%A5%E5%85%B7/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1%E7%BB%84%E4%BB%B6Quartz/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 郝永亮的主页
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论