关于优惠卷的定时任务:

/**
 * @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);
        }
    }
}

Spring Boot定时任务_List

总结:

  1. 启动类上要加 @EnableScheduling 注解
  2. 定时任务类上加@Component
  3. 定时方法上加@Scheduled

Spring Boot定时任务_定时任务_02

@Scheduled注解与参数