zoukankan      html  css  js  c++  java
  • springboot使用SpringTask实现定时任务

    SpringTask是Spring自主研发的轻量级定时任务工具,相比于Quartz更加简单方便,且不需要引入其他依赖即可使用。

    只需要在配置类中添加一个@EnableScheduling注解即可开启SpringTask的定时任务能力。

    package com.xc.mall2.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    
    /**
     * 定时任务配置
     * Created by xc on 190830
     */
    @Configuration
    @EnableScheduling
    public class SpringTaskConfig {
    }

    添加OrderTimeOutCancelTask来执行定时任务

    package com.xc.mall2.component;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    /**
     * Created by xc on 20190830
     * 定时任务
     */
    @Component
    public class OrderTimeOutCancelTask {
        private Logger LOGGER = LoggerFactory.getLogger(OrderTimeOutCancelTask.class);
        // @Autowired
        // private OmsPortalOrderService portalOrderService;
    
        /**
         * cron表达式:Seconds Minutes Hours DayofMonth Month DayofWeek [Year]
         */
        @Scheduled(cron = "0/15 * * * * ?")
        private void cancelTimeOutOrder() {
            // CommonResult result = portalOrderService.cancelTimeOutOrder();
            // LOGGER.info("取消订单,并根据sku编号释放锁定库存:{}", result);
            LOGGER.info("定时任务OrderTimeOutCancelTask");
        }
    }

    参考文章:https://macrozheng.github.io/mall-learning/#/architect/mall_arch_06?id=%e9%a1%b9%e7%9b%ae%e4%bd%bf%e7%94%a8%e6%a1%86%e6%9e%b6%e4%bb%8b%e7%bb%8d

  • 相关阅读:
    算法(5)
    字典
    算法(4)
    AD域设置
    css两句话搞定漂亮表格样式
    Dev控件用法 aspxTreeList 无刷新 aspxGridView 数据
    ASP.Net 验证视图状态 MAC 失败
    C# 客服端上传文件与服务器器端接收 (简单代码)
    Linq to SQL 类型的对象图包含循环,如果禁用引用跟踪,择无法对其进行序列化。
    C# 导出 Excel 数字列出现‘0’的解决办法
  • 原文地址:https://www.cnblogs.com/ooo0/p/11435647.html
Copyright © 2011-2022 走看看