首页 > 编程语言 > 详细

Spring boot创建定时任务

时间:2018-04-16 15:40:54      阅读:182      评论:0      收藏:0      [点我收藏+]

基于spring boot的应用创建定时任务不要太简单,给一个类加上@Configuration @EnableScheduling注解,然后给该类需要定时执行的方法加上@Scheduled(cron = "${case.phase.cron}")注解。就OK了。

${case.phase.cron}表示case.phase.cron从应用配置文件application.properties里取,或者从config-server里取。

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 定时任务
 * 去调用办件状态查询标准接口
 * */
@Configuration
@EnableScheduling
public class CasePhaseTaskService {
    private static final Logger LOGGER = LoggerFactory.getLogger(CasePhaseTaskService.class);
    @Scheduled(cron = "${case.phase.cron}")
    public void execScheduledTask() {
        LOGGER.info("task开始...");

        LOGGER.info("task结束!");
    }
}

 

Spring boot创建定时任务

原文:https://www.cnblogs.com/lyhero11/p/8856008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!