springboot 定时任务

一、使用springboot默认提供的线程执行 下面已经足够使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@EnableScheduling
@Component
public class SchedulingDemo {

    private Logger logger = LoggerFactory.getLogger(SchedulingDemo.class);
    /**
     * 删除数据定时
     */
    private static final String CORE_FORMAT = "0/5 * * * * ?";

    @Scheduled(cron = CORE_FORMAT)
    public void scheduleLimitRule() {
        try {
            System.out.println(" Timer:  Delete Export Template File With Schedule At {} Begin....."+ CORE_FORMAT);

            System.out.println("================= I already run");

        } catch (Exception e){
            e.printStackTrace();
            System.out.println(" Timer:  Delete Delete Export Template File Meets An Error {}"+ e.getMessage());
        }
    }


    private int fixedDelayCount = 1;
    private int fixedRateCount = 1;
    private int initialDelayCount = 1;
    private int cronCount = 1;

    @Scheduled(fixedDelay = 5000)        //fixedDelay = 5000表示当前方法执行完毕5000ms后,Spring scheduling会再次调用该方法
    public void testFixDelay() {
        logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
    }

    @Scheduled(fixedRate = 5000)        //fixedRate = 5000表示当前方法开始执行5000ms后,Spring scheduling会再次调用该方法
    public void testFixedRate() {
        logger.info("===fixedRate: 第{}次执行方法", fixedRateCount++);
    }

    @Scheduled(initialDelay = 1000, fixedRate = 5000)   //initialDelay = 1000表示延迟1000ms执行第一次任务
    public void testInitialDelay() {
        logger.info("===initialDelay: 第{}次执行方法", initialDelayCount++);
    }

    @Scheduled(cron = "0/1 * * * * ?")  //cron接受cron表达式,根据cron表达式确定定时规则
    public void testCron() {
        logger.info("===initialDelay: 第{}次执行方法", cronCount++);
    }


}
==============================================愚蠢的分割线======================================================
需要走自己规划好的线程池,加上如下部分即可
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.*;


@Configuration
public class ScheduleConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        //设定一个长度20的定时任务线程池
        scheduledTaskRegistrar.setScheduler(taskExecutor());
        //Executors.newScheduledThreadPool(20)
    }

    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
       // Executors.newScheduledThreadPool(15); //指定线程池大小
        ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10,Executors.defaultThreadFactory(), new  ThreadPoolExecutor.CallerRunsPolicy());

        return executor;
    }
}

参考:

//https://blog.csdn.net/u013456370/article/details/79411952
//https://segmentfault.com/a/1190000020299913?utm_source=tag-newest

你可能感兴趣的:(Java)