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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM