Quartz的简单使用.Spring Boot 集成Quartz用作定时任务

引入依赖

<!-- 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("这里填写需要执行的业务逻辑!--------");
	}
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值