你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

quartz定时任务框架使用

2021/12/30 4:32:25

一 使用场景

订单超时取消,指的是当⽤户成功提交订单之后在规定时间内没有完成⽀付,则将订单关闭还原库存。
实现订单的超时取消业务通常有两种解决⽅案:

  • 定时任务(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);
	 }
}