# quartz **Repository Path**: CapRobin/quartz ## Basic Information - **Project Name**: quartz - **Description**: springboot+quartz+mybatis+redis的使用 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2023-07-15 - **Last Updated**: 2023-07-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## quartz定时任务使用简介 #### 定时器基础 ##### **步骤** 1. 创建任务调度器(Scheduler) 2. 实现springboot容器启动监听器(ApplicationListener\) 3. 创建任务(JobDetail) 4. 创建触发器(Trigger) 5. 判断是否有该触发器,如果没有创建后加入到任务调度器中,再启动 ##### 部分截图 ![image-20220603222648126](/typora-img/image-20220603222648126.png) ##### 每 1 小时触发一次定时器 ![img](/typora-img/W$NZJN3KB%UGA9X9%[TNRL.png) ![image-20220603223437635](/typora-img/image-20220603223437635.png) ##### 每 2 分钟触发一次定时器 ![image-20220603223605963](/typora-img/image-20220603223605963.png) ![image-20220603223622692](/typora-img/image-20220603223622692.png) ##### 每 5 秒钟触发一次定时器 ![image-20220603223732668](/typora-img/image-20220603223732668.png) ##### 每天凌晨 1:30 触发一次定时器 跟上面的差不多,将cron表达式更换成"0 30 1 * * ?"就可以了 #### 案例 **定时修改某些记录的状态。如修改 30 分钟内还未支付完成的订单状态为失效状态** | 字段名称 | 数据类型 | 说明 | | :----------: | :-----------: | :------------------------------: | | id | int | 主键(自动增长) | | order_number | varchar | 订单号 | | product_id | int | 商品id | | price | decimal(10,2) | 单价 | | state | int | 状态(0未支付、1已支付、-1失效) | | order_time | datetime | 下订单时间 | 1. 先向数据库中添加时间 ![image-20220604113122927](/typora-img/image-20220604113122927.png) 2. 每设置定时器每1分钟执行一次 ![image-20220604113806047](/typora-img/image-20220604113806047.png) ![image-20220604113832777](/typora-img/image-20220604113832777.png)