一 使用场景
订单超时取消,指的是当⽤户成功提交订单之后在规定时间内没有完成⽀付,则将订单关闭还原库存。
实现订单的超时取消业务通常有两种解决⽅案:
- 定时任务(quartz)
- 延时队列(MQ)
- 实现流程

二 框架使用
1. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
2. 创建定时任务
定时任务,每隔指定的时间就执⾏⼀次任务
案例:每隔3s就打印⼀次Helloworld
@Component
public class PrintHelloWorldJob {
//https://cron.qqe2.com 可以查询时间设置方式
@Scheduled(cron = "0/3 * * * * ?") //在需要定时执行执行的类上加此注解并设置时间
public void printHelloWorld(){
System.out.println("----hello world.");
}
}
3. 在启动类开启定时任务
@SpringBootApplication
@EnableScheduling
public class QuartzDemoApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzDemoApplication.class, args);
}
}
