Quartz介绍: 1.什么是Quartz? Quartz是Job scheduling(作业调度)领域的一个开源项目,Quartz既可以单独使用也可以跟spring框架整合使用, 在实际开发中一般会使用后者。使用Quartz可以开发一个或者多个定时任务,每个定时任务可以单独指定执行的时间, 例如每隔1小时执行一次(频率)、每个月第一天上午10点执行一次(定时)、每个月最后一天下午5点执行一次(定时)等。 2.Quartz的使用: 1.maven坐标: Quartz基础包: <!--quartz的基础包--> <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.spring整合Quartz: <!--spring整合Quartz--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.0.2.RELEASE</version> </dependency> 2.Quartz入门: 1.创建工程quartzDemo,打包成war包: 2.导入maven坐标: <?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.it</groupId> <artifactId>quartzDemo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <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.springframework</groupId> <artifactId>spring-web</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> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 指定端口 --> <port>8080</port> <!-- 请求路径 --> <path>/</path> </configuration> </plugin> </plugins> </build> </project> 3.定义任务类: package com.it.job; import java.util.Date; /** * 任务类 * * @author Lyle * @date 2020/6/25 */ public class JobDemo { public void startJob(){ System.out.println("开始执行任务,执行时间是:"+new Date()); } } 4.配置文件:applicationContext-jobs.xml 配置自定义Job、任务描述、触发器、调度工厂等 <?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--自定义Job--> <bean id="jobDemo" class="com.it.job.JobDemo"></bean> <!--任务调度工厂,加载自定义的Job--> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--注入任务类和任务类中的定时方法--> <property name="targetObject" ref="jobDemo"></property> <property name="staticMethod" value="startJob"></property> </bean> <!--开启一个触发器,加载任务调度工厂--> <!--触发器有两种:频率触发器和定时触发器--> <!--1.定时触发器--> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!--注入调度工厂--> <property name="jobDetail" ref="jobDetail"></property> <!--cron表达式:每10秒执行一次--> <property name="cronExpression" value="0/10 * * * * ?"></property> </bean> <!--开启使用调度工厂--> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!--注入触发器--> <property name="triggers"> <array> <ref bean="cronTrigger"></ref> </array> </property> </bean> </beans> 5.配置web.xml,加载配置文件: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-jobs.xml</param-value> </context-param> </web-app>