zoukankan      html  css  js  c++  java
  • 在SpringBoot中配置定时任务

    前言

    之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单。

    已经加入我的github模版中:https://github.com/LinkinStars/springBootTemplate

    定时任务的分类

    所谓定时任务,就是在项目启动之后,定时的去执行一个任务,从而满足业务的需要。

    定时任务分为下面几种,串行,并行,同步,异步

    串行,并行:当配置了多个定时任务的时候,串行方式只会有一个线程去执行所有的任务,并且前一个任务执行完成,后一个任务才会开始执行;而并行是指会启动多个线程去执行所有的任务,各个任务同时进行执行

    同步,异步:这是对于一个定时任务来说,同步是指,这个定时任务本身自己完成之后才能再次执行自己;异步是指任务本身即使没有完成,当定时到达的时候也会再次执行

    举例来说(定时任务都设置为每两秒执行一次,但是执行中睡眠3秒)

    [2018-02-08 09:45:14.005] [pool-1-thread-1] [INFO ] my_info - 任务1:执行
    [2018-02-08 09:45:17.007] [pool-1-thread-1] [INFO ] my_info - 任务1:执行完成
    [2018-02-08 09:45:17.009] [pool-1-thread-1] [INFO ] my_info - 任务2:执行
    [2018-02-08 09:45:20.012] [pool-1-thread-1] [INFO ] my_info - 任务2:执行完成
    [2018-02-08 09:45:20.013] [pool-1-thread-1] [INFO ] my_info - 任务3:执行
    [2018-02-08 09:45:23.016] [pool-1-thread-1] [INFO ] my_info - 任务3:执行完成
    [2018-02-08 09:45:23.017] [pool-1-thread-1] [INFO ] my_info - 任务4:执行
    [2018-02-08 09:45:26.021] [pool-1-thread-1] [INFO ] my_info - 任务4:执行完成
    [2018-02-08 09:45:26.022] [pool-1-thread-1] [INFO ] my_info - 任务1:执行
    [2018-02-08 09:45:29.026] [pool-1-thread-1] [INFO ] my_info - 任务1:执行完成

    可以看到这四个任务是同一个线程执行的,并且 只有当前一个任务完成的时候,下一个任务才会开始,所以当前是串行同步的

    [2018-02-08 10:02:42.004] [pool-1-thread-1] [INFO ] my_info - 任务3:执行
    [2018-02-08 10:02:42.004] [pool-1-thread-3] [INFO ] my_info - 任务4:执行
    [2018-02-08 10:02:42.004] [pool-1-thread-4] [INFO ] my_info - 任务1:执行
    [2018-02-08 10:02:42.004] [pool-1-thread-2] [INFO ] my_info - 任务2:执行
    [2018-02-08 10:02:45.011] [pool-1-thread-4] [INFO ] my_info - 任务1:执行完成
    [2018-02-08 10:02:45.011] [pool-1-thread-2] [INFO ] my_info - 任务2:执行完成
    [2018-02-08 10:02:45.011] [pool-1-thread-3] [INFO ] my_info - 任务4:执行完成
    [2018-02-08 10:02:45.011] [pool-1-thread-1] [INFO ] my_info - 任务3:执行完成
    [2018-02-08 10:02:46.005] [pool-1-thread-2] [INFO ] my_info - 任务2:执行
    [2018-02-08 10:02:46.005] [pool-1-thread-4] [INFO ] my_info - 任务1:执行
    [2018-02-08 10:02:46.005] [pool-1-thread-5] [INFO ] my_info - 任务4:执行
    [2018-02-08 10:02:46.005] [pool-1-thread-8] [INFO ] my_info - 任务3:执行
    [2018-02-08 10:02:49.011] [pool-1-thread-2] [INFO ] my_info - 任务2:执行完成
    [2018-02-08 10:02:49.011] [pool-1-thread-4] [INFO ] my_info - 任务1:执行完成
    [2018-02-08 10:02:49.011] [pool-1-thread-8] [INFO ] my_info - 任务3:执行完成
    [2018-02-08 10:02:49.011] [pool-1-thread-5] [INFO ] my_info - 任务4:执行完成

    可以看到这四个任务是不同线程执行的,并且当前也是只有在前一个任务完成的情况下才会执行下一个任务,所以当前是并行同步的

    我们项目中喜欢使用的是第二种方式

    配置方式

    如果使用串行方式如下

    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    
    /**
     * 定时任务配置
     * @author LinkinStar
     */
    @Configuration
    @EnableScheduling
    public class TimeTaskConfig {
    
        @Scheduled(cron = "0/5 * * * * ?")
        public void test1(){
            System.out.println("任务1:执行");
        }
    }

    如果使用并行方式如下

    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    
    import java.util.concurrent.Executors;
    
    /**
     * 定时任务配置
     * @author LinkinStar
     */
    @Configuration
    @EnableScheduling
    public class TimeTaskConfig implements SchedulingConfigurer {
    
        /**
         * 配置定时任务线程池大小
         */
        @Override
        public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
            taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
        }
    }
    import com.linkinstars.springBootTemplate.util.LogUtil;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.Scheduled;
    
    /**
     * 定时任务配置
     * @author LinkinStar
     */
    @Configuration
    public class TimeTask {
    
        @Scheduled(cron = "0/2 * * * * ?")
        public void test1(){
            LogUtil.printLog("任务1:执行");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LogUtil.printLog("任务1:执行完成");
        }
    
    }

    参考

    https://www.jianshu.com/p/ef18af5a9c1d 

    https://www.cnblogs.com/slimer/p/6222485.html

  • 相关阅读:
    ExtJS 4.0 改善Ext.grid.plugin.RowEditing (重构,v1.4版本,20110911)
    GeoServer源码解析和扩展 (四)文件系统
    Ext蒙板效果
    Ext Grid表格的自动宽度及高度的实现
    Extjs4用RowEditing对数据进行增加,修改
    ExtJs4.0 Grid分頁詳解
    canvas的处理图片功能
    cookie的设置和读取
    html5的canvas知识
    初学jquery mobile
  • 原文地址:https://www.cnblogs.com/linkstar/p/8435958.html
Copyright © 2011-2022 走看看