Spring Boot 定时任务的使用

时间:2022-05-27 07:58:11
@Configuration
@EnableScheduling
public class ScheduleConfig {

    private final Logger logger = LoggerFactory.getLogger(ScheduleConfig.class);

    @Autowired
    private SBUXPingService sbuxPingService;

    @Scheduled(cron = "0/10 * * * * ?") // 每10秒执行一次
    public void scheduler() {
        if (sbuxPingService.schedulePing()) {
            logger.error("============== 定时Ping成功 ===============");
        } else {
            logger.error("============== 定时Ping失败 ===============");
        }
    }

}