Spring Boot 使用 Spring Schedule

依赖版本

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("计划任务开始执行");
        
    }
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值