Quartz:定时任务组件
Quartz是Job scheduling(作业调度)领域的一个开源项目,Quartz既可以单独使用也可以跟spring框架整合使用,在实际开发中一般会使用后者。使用Quartz可以开发一个或者多个定时任务,每个定时任务可以单独指定执行的时间,例如每隔1小时执行一次、每个月第一天上午10点执行一次、每个月最后一天下午5点执行一次等。
使用步骤:
<1>引入依赖
<dependencies>
<dependency>
<groupId>cn.itcast</groupId>
<artifactId>*****_interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<!-- 指定端口 -->
<port>83</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
<2>配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext_*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<3>配置applicationContext_jobs.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!--开启spring注解使用-->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="**.**.jobs"></context:component-scan>
<!-- 配置处理定时任务的线程池 -->
<task:executor id="executor" pool-size="10"/>
<!-- 配置处理 异步定时任务的 线程池 -->
<task:scheduler id="scheduler" pool-size="10"/>
<!-- 启用annotation方式 -->
<task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>
</beans>
纯xml配置:需要更改执行方法的间隔Cron
//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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!--开启spring注解使用-->
<context:annotation-config></context:annotation-config>
<!--注册自定义Job-->
<bean id="clearImgJob" class="***.***.***.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>
-->
<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>
<4>配合redis使用的话导入redis相关配置,详见上一篇
<5>定义任务方法
自动生成Cron表达式
//使用注解配置的注解
@Component
public class ClearImgJob {
@Autowired
private JedisPool jedisPool;
//任务触发时间:可自动生成
@Scheduled(cron = "0/10 * * * * ?")
//方法为自己定义任务方法以及执行任务
public void clearImg(){
//根据reids中保存的两个集合的差值,来得到垃圾图片名称的集合
Set<String> sdiff = jedisPool.getResource().sdiff(set1, set2);
if(sdiff!=null){
for (String picName : sdiff) {
//根据图片名称利用工具类清理七牛云上的垃圾图片
QiniuUtils.deleteFileFromQiniu(picName);
//根据图片名称删除redis缓存中垃圾图片的名称
jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
System.out.println (picName);
}
}else{
System.out.println ("无需清理");
}
}
}