1.定时任务

配置文件加@Configration 加了@EnableScheduling 后就不需要在启动器上加这个注解了 配置mybatis
mapper扫描也是一样的

● 用户对某商品进行下单操作;
● 系统需要根据用户购买的商品信息生成订单并锁定商品的库存;
●系统设置了60分钟用户不付款就会取消订单;
●开启一个定时任务,每隔10分钟检查下,如果有超时还未付款的订单,就取消订单并取消锁定的商品库存。
●由于SpringTask已经存在于Spring框架中,所以无需添加依赖。
●只需要在配置类中添加一个@EnableScheduling注解即可开启SpringTask的定时任务能力。

package com.macro.mall.tiny.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
 * 定时任务配置
 * Created by macro on 2019/4/8.
 */
@Configuration
@EnableScheduling
public class SpringTaskConfig {
    
    
}
添加OrderTimeOutCancelTask来执行定时任务
package com.macro.mall.tiny.component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created by macro on 2018/8/24.
 * 订单超时取消并解锁库存的定时器
 */
@Component
public class OrderTimeOutCancelTask {
    
    
    private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);

    /**
     * cron表达式:Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
     * 每10分钟扫描一次,扫描设定超时时间之前下的订单,如果没支付则取消该订单
     */
@Scheduled(cron = "0/5 * * * * ? ") //5秒执行一次
    @Scheduled(cron = "0 0/10 * ? * ?")
    private void cancelTimeOutOrder() {
    
    
        // TODO: 2019/5/3 此处应调用取消订单的方法,具体查看mall项目源码
        LOGGER.info("取消订单,并根据sku编号释放锁定库存");
    }
}

corn表达式生成器
“0 0 12 * * ?” 每天中午十二点触发
“0 15 10 ? * *” 每天早上10:15触发
“0 15 10 * * ?” 每天早上10:15触发
“0 15 10 * * ? *” 每天早上10:15触发
“0 15 10 * * ? 2005” 2005年的每天早上10:15触发
“0 * 14 * * ?” 每天从下午2点开始到2点59分每分钟一次触发
“0 0/5 14 * * ?” 每天从下午2点开始到2:55分结束每5分钟一次触发
“0 0/5 14,18 * * ?” 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
“0 0-5 14 * * ?” 每天14:00至14:05每分钟一次触发
“0 10,44 14 ? 3 WED” 三月的每周三的14:10和14:44触发
“0 15 10 ? * MON-FRI” 每个周一、周二、周三、周四、周五的10:15触发

猜你喜欢

转载自blog.csdn.net/zyf_fly66/article/details/113697505