springboot整合定时器-quartz

文章介绍了如何使用Quartz框架结合SpringBoot来创建定时任务,例如定时删除OSS中的冗余文件和处理未支付订单。通过添加quartz依赖,配置@Scheduled注解以及启用@EnableScheduling,可以实现按Cron表达式定义的周期执行业务代码。
摘要由CSDN通过智能技术生成

https://www.pppet.net/在线Cron表达式生成器

1.什么是定时器以及定时器的英语场景

定时器: 在指定的时间执行相应的业务代码。

应用场景: 比如: 定时删除OSS中冗余的文件

三十分钟未支付---->取消订单。

定时发送短信---->11.11====>发送短信

2.引入quartz依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

3. 配置定时器任务

@Component //交于spring容器该类对象
public class QuarzConfig {
    @Autowired
    private EmpMapper empMapper;
    @Scheduled(cron = "0/5 * * * * ? ")
    public void show(){
        //1.查询过期的订单
        List<Emp> emps = empMapper.selectList(null);
        System.out.println(students);
        //2.删除过期的订单
    }
}

4.开启定时器注解驱动

@SpringBootApplication
@MapperScan(basePackages = "com.lzx.mapper")
@EnableScheduling //开启定时器注解
public class Qy163SpringBoot02Application {

    public static void main(String[] args) {
        SpringApplication.run(Qy163SpringBoot02Application.class, args);
    }

}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值