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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--支持spring注解-->
<context:annotation-config></context:annotation-config>
<bean id="clearImgJob" class="com.xx.jobs.ClearImgJob"></bean>
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--注入目标对象-->
<property name="targetObject" ref="clearImgJob"></property>
<!--注入目标方法-->
<property name="targetMethod" value="clearImg"></property>
</bean>
<!--注册一个触发器,指定任务触发的时间-->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!--注入jobDetail-->
<property name="jobDetail" ref="jobDetail"></property>
<!--指定触发的时间,基于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>
</beans>
导入依赖
<dependencies>
<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>
代码演示
public class ClearImgJob {
@Autowired
private JedisPool jedisPool;
public void clearImg(){
Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
if(set!=null){
for (String s : set) {
//删除七牛云无用的图片
QiniuUtils.deleteFileFromQiniu(s);
//删除redis中的无用的数据
jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
}
}
}
}