美文网首页
springboot2.x | 定时任务

springboot2.x | 定时任务

作者: hk_faith | 来源:发表于2019-10-17 15:05 被阅读0次

简介:基与 springTask进行定时任务

1.启动类添加注解

@EnableScheduling

2.定义一个scheduled服务

@Service
@Slf4j
public class ScheduleTask {
    /**
     * 每5秒执行一次
     */
    @Scheduled(fixedRate = 5000)
    public void task1() {
        log.info("task1");
    }
    /**
     * 每天上午10点,下午2点,4点
     */
    @Scheduled(cron = "0 0 10,14,16 * * ?")
    public void task2() {
        log.info("task2");
    }
}

3.@scheduled() 参数参数详解

fixedRate 定义一个按一定是频率执行任务
fixedDelay 定义一个按一定频率执行的定时任务,与上面不同的是,改属性可以配合initialDelay, 定义该任务延迟执行时间
cron 通过表达式来配置任务执行时间

4.cron 表达式

cron在线生成器
cron博客参考

自定义线程池

从控制台可以看出。多个任务时用的是同一个线程,可以自定义线程池不同的任务用不同线程来执行
启动类添加

@EnableAsync

添加配置

@Configuration
public class ThreadPoolConfig {

    @Bean(name = "asyncThreadPool")
    public ThreadPoolTaskExecutor getAsyncThreadPoolTaskExecutor() {

        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(20);
        taskExecutor.setMaxPoolSize(200);
        taskExecutor.setQueueCapacity(25);
        taskExecutor.setKeepAliveSeconds(200);
        taskExecutor.setThreadNamePrefix("asyncPool-");
        // 线程池对拒绝任务(无线程可用)的处理策略,目前只支持AbortPolicy、CallerRunsPolicy;默认为后者
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 调度器shutdown被调用时等待当前被调度的任务完成
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 等待时长
        taskExecutor.setAwaitTerminationSeconds(60);
        taskExecutor.initialize();
        return taskExecutor;
    }
}

scheduled服务修改为

@Service
@Slf4j
public class ScheduleTask {

   /**
    * 每5秒执行一次
    */
   @Async("asyncThreadPool")
   @Scheduled(fixedRate = 5000)
   public void task1() {
       log.info("task1");
   }


   /**
    * 每天上午10点,下午2点,4点
    */
   @Async("asyncThreadPool")
//    @Scheduled(cron = "0 0 10,15,16 * * ?")
   @Scheduled(fixedRate = 2000)
   public void task2() {
       log.info("task2");
   }
   
}

看控制台输出日志。。。

还有一种是基于Quartz实现定时调度

分布式定时任务解决方案

现在Quartz也有基于Redis的集群方案

  • 分布式锁。可通过使用Redis或者ZooKeeper实现一个分布式锁的机制,使得只有获取到锁的实例方能运行定时任务,避免任务重复执行。可查看下开源的基于Redis实现的分布式锁项目:redisson。github地址:https://github.com/redisson/redisson有兴趣的同学可以了解下。

  • 统一调度中心
    todo

定时任务怎么监控??

todo

springboot-task

相关文章

  • SpringBoot2.x填坑(四): Scheduled定时任

    一、问题描述 在SpringBoot2.x 的项目上写了个定时任务,每天凌晨1点执行,那么令人费解的是:定时任务在...

  • SpringBoot2.x简单实现定时任务功能

    SpringBoot2.x简单实现定时任务功能 1、新建一个SpringBoot项目 可以使用https://st...

  • springboot2.x | 定时任务

    简介:基与 springTask进行定时任务 1.启动类添加注解 2.定义一个scheduled服务 3.@sch...

  • springboot2.x 定时任务

    定时任务一般在java中三种方式实现1 timer2 Quartz3 spring 注解方式4 Schedule...

  • ScheduledThreadPoolExecutor线程池

    1. ScheduledThreadPoolExecutor线程池2. SpringBoot2.X整合定时线程池(...

  • 2019-07-31定时任务

    定时任务 定时任务实现方法 系统默认定时任务 用户自定义设置定时任务 定时任务配置文件 定时任务启动 定时任务样例...

  • 分布式定时调度-xxl-job

    一、定时任务概述 1.1 定时任务认识 1.1.1 什么是定时任务 定时任务是按照指定时间周期运行任务。使用场景为...

  • day 22 操作系统定时任务

    系统定时任务概念==生活中闹钟 系统定时任务实现方法: 实现定时任务配置: 定时任务如何进行设置 定时任务编写常见...

  • 7月30日 定时任务

    定时任务 代替人自动完成一些任务 定时任务实现的方法 定时任务软件:cronie定时任务软件:atd --- 设...

  • SpringBoot 定时任务

    1.如何定时任务 1.1 开启定时任务 1.2 @Scheduled(预定的)选择要定时执行的任务 == 定时在前...

网友评论

      本文标题:springboot2.x | 定时任务

      本文链接:https://www.haomeiwen.com/subject/aaimmctx.html