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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值