当前位置:网站首页>Quartz(定时器)
Quartz(定时器)
2022-06-27 09:24:00 【Sparkle_wsl】
Quartz(定时器)
使用步骤
导包
<!--定时清理jar包--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> </dependency>
自定义一个Job(定时指定的任务)
public class ClearImg { @Autowired private JedisPool jedisPool; public void clearImg(){ System.out.println(jedisPool); //将全部的内容 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); } } } }
配饰Spring-jobs的配置文件
将自定的job配置到Spring中
<!-- 注册自定义Job --> <bean id="jobDemo" class="com.itheima.jobs.ClearImg"></bean>
注册JobDetail,作用负责通过反射调用指定的job
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 注入目标对象 --> <property name="targetObject" ref="jobDemo"/> <!-- 注入目标方法 --> <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 0/1 * * ? </value> </property> </bean>
注册一个统一的调度工厂,通过这个调度工厂调度任务
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <!-- 注入多个触发器 --> <property name="triggers"> <list> <!-- 触发器1 --> <ref bean="myTrigger"/> <!-- 触发器2 --> <res bean="xXX"> <!-- .... --> </list> </property> </bean>
Cron表达式
一共有7位,常用的又6位
从做到右:秒 分 时 日 周 年
在线生成Cron表达式
边栏推荐
- NoSQL database redis installation
- 手把手带你玩摄像头模组
- Vector:: data() pointer usage details
- [cloud native] 2.3 kubernetes core practice (Part 1)
- 如何获取GC(垃圾回收器)的STW(暂停)时间?
- 不容置疑,这是一个绝对精心制作的项目
- 为智能设备提供更强安全保护 科学家研发两种新方法
- Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
- 借助原子变量,使用CAS完成并发操作
- JS 客户端存储
猜你喜欢
Enumeration? Constructor? Interview demo
Process 0, process 1, process 2
快速入门CherryPy(1)
[cloud native] 2.3 kubernetes core practice (Part 1)
0号进程,1号进程,2号进程
One week's experience of using Obsidian (configuration, theme and plug-in)
最全H桥电机驱动模块L298N原理及应用
Quelques exercices sur les arbres binaires
That is, a one-stop live broadcast service with "smooth live broadcast" and full link upgrade
HiTek电源维修X光机高压发生器维修XR150-603-02
随机推荐
Some exercises about binary tree
MySQL proficient-01 addition, deletion and modification
i=i++;
Apache POI的读写
借助原子变量,使用CAS完成并发操作
MATLAB小技巧(19)矩阵分析--主成分分析
One week's experience of using Obsidian (configuration, theme and plug-in)
多個類的設計
Object contains copy method?
Enumeration? Constructor? Interview demo
Analysis of orthofinder lineal homologous proteins and result processing
The markdown plug-in of the browser cannot display the picture
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
Fake constructor???
Flow chart of Alipay wechat payment business
i=i++;
Improving efficiency or increasing costs, how should developers understand pair programming?
快速入门CherryPy(1)
Semi supervised learning—— Π- Introduction to model, temporary assembling and mean teacher
DV scroll board width of datav rotation table component