springBoot实现定时任务

springBoot使用Task实现定时任务

引用:https://blog.csdn.net/jeikerxiao/article/details/86170384

小工具在线cron表达式生成:http://qqe2.com/cron/index

定时任务代码

@Slf4j
@Component
public class ScheduledService {

    /**
     * cron:通过表达式来配置任务执行时间
     */
    @Scheduled(cron = "0/5 * * * * *")
    public void scheduled() {
        log.info("==>{} 使用cron。", System.currentTimeMillis());
    }

    /**
     * fixedRate:指定两次任务执行的时间间隔(毫秒),
     * 此时间间隔指的是,前一个任务开始与下一个任务开始的间隔
     */
    @Scheduled(fixedRate = 5000)
    public void scheduled1() {
        log.info("==>{} 使用fixedRate。", System.currentTimeMillis());
    }

    /**
     * fixedDelay:指定两次任务执行的时间间隔(毫秒),
     * 此时间间隔指的是,前一次任务结束与下一个任务开始的间隔
     * 此属性可以配合 initialDelay, 定义该任务延迟执行时间。
     */
    @Scheduled(fixedDelay = 5000)
    public void scheduled2() {
        log.info("==>{} 使用fixedDelay。", System.currentTimeMillis());
    }
}

开启定时任务代码

在主类SpringBootApplication上使用@EnableScheduling注解开启对定时任务的支持,然后启动项目。

@SpringBootApplication
@EnableScheduling // 开启Scheduling
public class SpringBootTaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootTaskApplication.class, args);
    }
}

运行结果

2019-01-08 17:24:58.207  [   scheduling-1] c.jeiker.task.service.ScheduledService   : ==>1546939498207 使用fixedRate。
2019-01-08 17:24:58.208  [   scheduling-1] c.jeiker.task.service.ScheduledService   : ==>1546939498208 使用fixedDelay。
2019-01-08 17:25:00.004  [   scheduling-1] c.jeiker.task.service.ScheduledService   : ==>1546939500004 使用cron。
2019-01-08 17:25:03.208  [   scheduling-1] c.jeiker.task.service.ScheduledService   : ==>1546939503208 使用fixedRate。
2019-01-08 17:25:03.209  [   scheduling-1] c.jeiker.task.service.ScheduledService   : ==>1546939503209 使用fixedDelay。
2019-01-08 17:25:05.002  [   scheduling-1] c.jeiker.task.service.ScheduledService   : ==>1546939505002 使用cron。

以上是单线程执行结果

配置多线程

@Configuration:表明该类是一个配置类

@EnableAsync:开启异步事件的支持

@Configuration
@EnableAsync
public class AsyncConfig {

    /**
    * 此处成员变量应该使用@Value从配置中读取,将值配在配置文件中,见【配置线程池】
    */
    private int corePoolSize = 10;
    private int maxPoolSize = 200;
    private int queueCapacity = 10;
    
    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}

然后在定时任务的类或者方法上添加@Async。最后重启项目,每一个任务都是在不同的线程中。

配置线程池

application.yml 文件配置线程池

asyncConfig:
    corePoolSize: 10
    maxPoolSize: 200
    queueCapacity: 10       # 配置定时线程
    #threadNamePrefix: Async=========
    #awaitTerminationSeconds: 60 * 15

如果配置了,下面这样拿值就OK了

    @Value("${asyncConfig.corePoolSize}")
    private int corePoolSize;
	@Value("${asyncConfig.maxPoolSize}")
    private int maxPoolSize;
	@Value("${asyncConfig.queueCapacity}")
    private int queueCapacity;

异步注解

@Slf4j
@Component
public class ScheduledService {

    @Async
    @Scheduled(cron = "0/5 * * * * *")
    public void scheduled() {
        log.info("==>{} 使用cron。", System.currentTimeMillis());
    }

    @Async
    @Scheduled(fixedRate = 5000)
    public void scheduled1() {
        log.info("==>{} 使用fixedRate。", System.currentTimeMillis());
    }

    @Async
    @Scheduled(fixedDelay = 5000)
    public void scheduled2() {
        log.info("==>{} 使用fixedDelay。", System.currentTimeMillis());
    }
}

日志输出

2019-01-08 18:20:50.006  [ taskExecutor-6] c.jeiker.task.service.ScheduledService   : ==>1546942850006 使用cron。
2019-01-08 18:20:53.714  [ taskExecutor-7] c.jeiker.task.service.ScheduledService   : ==>1546942853714 使用fixedRate。
2019-01-08 18:20:53.716  [ taskExecutor-8] c.jeiker.task.service.ScheduledService   : ==>1546942853716 使用fixedDelay。
2019-01-08 18:20:55.003  [ taskExecutor-9] c.jeiker.task.service.ScheduledService   : ==>1546942855003 使用cron。
2019-01-08 18:20:58.711  [taskExecutor-10] c.jeiker.task.service.ScheduledService   : ==>1546942858711 使用fixedRate。
2019-01-08 18:20:58.717  [ taskExecutor-1] c.jeiker.task.service.ScheduledService   : ==>1546942858717 使用fixedDelay。
2019-01-08 18:21:00.005  [ taskExecutor-2] c.jeiker.task.service.ScheduledService   : ==>1546942860005 使用cron。
2019-01-08 18:21:03.711  [ taskExecutor-3] c.jeiker.task.service.ScheduledService   : ==>1546942863711 使用fixedRate。
2019-01-08 18:21:03.719  [ taskExecutor-4] c.jeiker.task.service.ScheduledService   : ==>1546942863719 使用fixedDelay。
2019-01-08 18:21:05.003  [ taskExecutor-5] c.jeiker.task.service.ScheduledService   : ==>1546942865003 使用cron。
2019-01-08 18:21:08.714  [ taskExecutor-6] c.jeiker.task.service.ScheduledService   : ==>1546942868714 使用fixedRate。
2019-01-08 18:21:08.721  [ taskExecutor-7] c.jeiker.task.service.ScheduledService   : ==>1546942868721 使用fixedDelay。

以上是多线程执行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值