引入依赖
<!-- Quartz Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!-- Quartz End -->
添加注解
在入口类中添加 @EnableScheduling 注解来开启计划任务功能
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableScheduling //任务调度的注解
@ComponentScan(value="com.mc")
public class JewelryBespokeApplication {
public static void main(String[] args) {
SpringApplication.run(JewelryBespokeApplication.class, args);
}
}
创建任务
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import com.mc.common.exception.BusinessException;
import com.mc.jewelrybespoke.app.service.AppSkuService;
import com.mc.jewelrybespoke.entity.SalesOrderDetail;
import com.mc.jewelrybespoke.entity.Sku;
import com.mc.jewelrybespoke.query.SalesOrderDetailQuery;
import com.mc.jewelrybespoke.service.ISalesOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.mc.common.constant.CommonConstant;
import com.mc.jewelrybespoke.app.utils.LocalDateTimeUtil;
import com.mc.jewelrybespoke.entity.SalesOrderHeader;
import com.mc.jewelrybespoke.query.SalesOrderHeaderQuery;
import com.mc.jewelrybespoke.service.ISalesOrderHeaderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
@Component
@Async
@Slf4j
public class OrderOverTimeScheduling {
@Autowired
private ISalesOrderHeaderService salesOrderHeaderService;
@Autowired
private ISalesOrderDetailService salesOrderDetailService;
@Autowired
private AppSkuService appSkuService;
private static Lock lock = new ReentrantLock();
@Scheduled(cron = "0 0/5 * * * ? ") // 每5分钟执行一次
public void clearOrder() {
log.info("这里填写需要执行的业务逻辑!--------");
}
}