Spring boot创建定时任务

基于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结束!");
    }
}

猜你喜欢

转载自www.cnblogs.com/lyhero11/p/8856008.html