springboot整合定时器-quartz

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);
    }

}

猜你喜欢

转载自blog.csdn.net/wnfkbh/article/details/130175733
今日推荐