1.Quartz介绍
简介:
Quartz是一个完全由 Java 编写的开源Job Scheduler(作业调度)框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。Quartz既可以单独使用,也可以跟spring框架整合使用,在实际开发中一般会使用后者。作用:
使用Quartz可以开发一个或者多个定时任务,每个定时任务可以单独指定执行的时间。即在指定的时间执行指定类中的指定方法。项目中的应用:
具体场景:本地数据库中存在没有记录的垃圾文件,为了定时删除这些垃圾文件用到了Quartz技术。官网:
http://www.quartz-scheduler.org/
1.1.使用步骤:
-
导入Quartz框架的maven坐标
当然结合Spring框架使用,需要导入Spring框架坐标
<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>
-
自定义一个任务
其实就是一个普通类中的方法,待会在配置文件中指定就可以了,如下ClearImaJob类中的clearImg方法。
public class ClearImgJob { @Autowired private JedisPool jedisPool; public void clearImg() { //获取两个集合的余 Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES); if(sdiff !=null){ for (String picName : sdiff) { //删除七牛云服务器上的图片 QiniuUtils.deleteFileFromQiniu(picName); //从Redis集合中删除多余的图片名称 jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName); } } } }
-
提供Spring配置文件applicationContext-jobs.xml
需要配置的项:配置自定义Job(任务)、任务描述、触发器、调度工厂等,其实就是将几个类的对象交给Spring容器管理。
<?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"> <!--开启spring注解使用--> <context:annotation-config></context:annotation-config> <!--注册自定义Job--> <bean id="clearImgJob" class="com.whut.jobs.ClearImgJob"></bean> <!--任务描述:指定对应任务的类和方法--> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 注入目标对象 --> <property name="targetObject" ref="clearImgJob"/> <!-- 注入目标方法 --> <property name="targetMethod" value="clearImg"/> </bean> <!--触发器:注册一个触发器,指定任务触发的时间--> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 注入JobDetail --> <property name="jobDetail" ref="jobDetail"/> <!-- 指定触发的时间,基于Cron表达式 --> <property name="cronExpression"> <!-- <value>0 0 2 * * ?</value> cron表达式:后面有具体描述 --> <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>
-
触发使用
只需加载此配置文件即可触发使用,就是加载一个SpringContext.xml文件
方法1(基本不用):
手动触发:new ClassPathXmlApplicationContext(“spring‐jobs.xml”);
方法2:
相当于之前自动创建类,在web.xml中指定加载的文件<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext-jobs.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
1.2.cron表达式
简介:
上面的入门案例中我们指定了一个表达式:0/10 * * * * ?这种表达式称为cron表达式,通过cron表达式可以灵活的定义出符合要求的程序执行的时间。
cron表达式分为七个域,之间使用空格分隔。其中最后一个域(年)可以为空。每个域都有自己允许的值和一些特殊字符构成。使用这些特殊字符可以使我们定义的表达式更加灵活。
-
特殊字符讲解
-
cron表达式在线生成器
不用自己编写表达式,可以借助工具生成
http://cron.qqe2.com/
以上内容仅个人总结,难免有错误之处,欢迎指正