Spring Boot 使用 Spring Schedule

[toc]

依賴版本

Spring Boot版本:1.5.18

參考文檔

springboot 定時任務@Scheduled 和 異步@Async

springboot配置

# schedule
cron.one=0 30 10 * * ?

Spring Schedule 配置


import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.IntervalTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.ScheduledMethodRunnable;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;



@Configuration
@EnableScheduling
public class ScheduleConfigurer implements SchedulingConfigurer {
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleConfigurer.class);
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());

        /**
         * 啟動時打印任務
         */
        List<CronTask> cronTasks = taskRegistrar.getCronTaskList();
        List<IntervalTask> fixedDelayTasks = taskRegistrar.getFixedDelayTaskList();
        List<IntervalTask> fixedRateTasks = taskRegistrar.getFixedRateTaskList();

        for (CronTask cronTask : cronTasks){
            String expression = cronTask.getExpression();
            ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) cronTask.getRunnable();
            log.info("method: {},expression: {}",runnable.getMethod(),expression);
        }

        for (IntervalTask intervalTask : fixedDelayTasks){
            ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)intervalTask.getRunnable();
            log.info("method: {},InitialDelay: {}",runnable.getMethod(),intervalTask.getInitialDelay());
        }

        for (IntervalTask intervalTask : fixedRateTasks){
            long interval = intervalTask.getInterval();
            ScheduledMethodRunnable runnable = (ScheduledMethodRunnable)intervalTask.getRunnable();
            log.info("method: {},Initial: {}",runnable.getMethod(),intervalTask.getInterval());
        }
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        // 線程池
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
        //scheduledThreadPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
        //    @Override
        //    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        //
        //    }
        //});

        return scheduledThreadPoolExecutor;
    }
}

設置定時任務


import org.slf4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
public class ScheduleOne {
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(ScheduleOne.class);
    @Scheduled(cron = "${cron.one}")
    public void test() {
        log.info("計劃任務開始執(zhí)行");
        
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容