定时任务 - 构建定时任务task

    科技2025-04-10  17

    @Component public class OrderJob { @Autowired private OrderService orderService; /** * 使用定时任务关闭超期未支付订单,会存在的弊端: * 1. 会有时间差,程序不严谨 * 10:39下单,11:00检查不足1小时,12:00检查,超过1小时多余39分钟 * 2. 不支持集群 * 单机没毛病,使用集群后,就会有多个定时任务 * 解决方案:只使用一台计算机节点,单独用来运行所有的定时任务 * 3. 会对数据库全表搜索,及其影响数据库性能:select * from order where orderStatus = 10; * 定时任务,仅仅只适用于小型轻量级项目,传统项目 * * 后续课程会涉及到消息队列:MQ-> RabbitMQ, RocketMQ, Kafka, ZeroMQ... * 延时任务(队列) * 10:12分下单的,未付款(10)状态,11:12分检查,如果当前状态还是10,则直接关闭订单即可 */ // @Scheduled(cron = "0/3 * * * * ?") // @Scheduled(cron = "0 0 0/1 * * ?") public void autoCloseOrder() { orderService.closeOrder(); System.out.println("执行定时任务,当前时间为:" + DateUtil.getCurrentDateString(DateUtil.DATETIME_PATTERN)); } } /** * A cron-like expression, extending the usual UN*X definition to include triggers * on the second as well as minute, hour, day of month, month and day of week. * <p>E.g. {@code "0 * * * * MON-FRI"} means once per minute on weekdays * (at the top of the minute - the 0th second). * <p>The special value {@link #CRON_DISABLED "-"} indicates a disabled cron trigger, * primarily meant for externally specified values resolved by a ${...} placeholder. * @return an expression that can be parsed to a cron schedule * @see org.springframework.scheduling.support.CronSequenceGenerator */ String cron() default ""; @EnableScheduling // 开启定时任务

     

    Processed: 0.012, SQL: 8