关于优惠卷的定时任务:
/**
* @author Wen先森
* @version 1.0
* @date 2022/5/26 10:35
* 检测优惠卷过期
*/
@Slf4j
@Component
public class CouponJob {
@Autowired
private IWpCouponService couponService;
@Autowired
private IWpCouponUserService couponUserService;
/**
* 每隔一个小时检查
*/
@Scheduled(fixedDelay =60 * 60 * 1000)
public void checkCouponExpired() {
log.info("系统开启任务检查优惠券是否已经过期");
List<WpCoupon> couponList = couponService.queryExpired();
for (WpCoupon coupon : couponList) {
coupon.setStatus(CouponConstant.STATUS_EXPIRED.intValue());
couponService.updateWpCoupon(coupon);
}
List<WpCouponUser> couponUserList = couponUserService.queryExpired();
for (WpCouponUser couponUser : couponUserList) {
couponUser.setStatus(CouponUserConstant.STATUS_EXPIRED.intValue());
couponUserService.updateWpCouponUser(couponUser);
}
}
}
总结:
- 启动类上要加 @EnableScheduling 注解
- 定时任务类上加@Component
- 定时方法上加@Scheduled
@Scheduled(fixedDelay =60 * 60 * 1000)
public void checkCouponExpired() {
log.info("系统开启任务");
//代码块
}