前言

之前在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

在SpringBoot中配置定时任务的更多相关文章

  1. springBoot中的定时任务

    springBoot中的定时任务 1:在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 2:新建ScheduledTasks任务类 : package c ...

  2. 在SpringBoot中配置aop

    前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...

  3. springboot中配置主从redis

    测试redis的主从配置 redis实例 文件夹名称如下 redis_master_s redis_slaver1_s redis_slaver2_s redis.conf文件 master的redi ...

  4. SpringBoot中执行定时任务

    一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...

  5. SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理

    SpringBoot中配置字符编码一共有两种方式 方式一: 使用传统的Spring提供的字符编码过滤器(和第二种比较,此方式复杂,由于时间原因这里先不介绍了,后续补上) 方式二(推荐使用) 在appl ...

  6. springBoot中使用定时任务

    简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...

  7. SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10659045.html,否则将追究法律责任!!! 一.在JAVA开发领域,目前可以通过以下 ...

  8. springboot中配置过滤器以及可能出现的问题

    在springboot添加过滤器有两种方式: 1.通过创建FilterRegistrationBean的方式(建议使用此种方式,统一管理,且通过注解的方式若不是本地调试,如果在filter中需要增加c ...

  9. SpringBoot中的定时任务与Quartz的整合

    SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...

随机推荐

  1. JavaScript事件高级绑定

    js 进行事件绑定,其中一种不常见的写法是: <div id="father" style="width: 300px; height: 200px; backgr ...

  2. 无废话XML--XML解析(DOM和SAX)

    XML处理模式 处理XML有2种方式,DOM和SAX.一般的实际开发中,这2种使用的不多,直接用dom4j来解析XML就好了,包括CRUD等操作都很方便的.这里介绍的DOM和SAX是比较底层的,具体的 ...

  3. php 在foreach中循环数组的时候添加元素的属性

    foreach($arr as $k => &$v){ //注意,由于上面遍历的时候写了地址传值符&, //所以下面可以直接给$v 赋值;如果不写&符号,下面这样写是没有 ...

  4. JSP错误页面的处理和exception对象

    exception对象可以使用的主要方法如下所示. l        getMessage():该方法返回错误信息. l        printStackTrace():该方法以标准错误的形式输出一 ...

  5. linux命令之read

    对于写bash脚本的朋友,read命令是不可或缺的,需要实践一下就可以了解read命令的大致用途: 编写一个脚本: #!/bin/bash # hao32 test read echo -e &quo ...

  6. redis数据类型-列表类型

    列表类型 列表类型(list)可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素,或者获得列表的某一个片段. 列表类型内部是使用双向链表(double linked list)实现的,所以向 ...

  7. ABP官方文档翻译 9.3 NHibernate集成

    NHibernate集成 Nuget包 配置 实体映射 仓储 默认实现 自定义仓储 应用程序特定基础仓储类 ABP可以使用任何ORM框架,它内置集成NHibernate.此文档将讲解ABP如何使用NH ...

  8. SpringMVC源码情操陶冶-InterceptorsBeanDefinitionParser拦截器解析器

    解析mvc:interceptors节点 观察下InterceptorsBeanDefinitionParser的源码备注 /** * {@link org.springframework.beans ...

  9. CAS基础和原子类

    基于CAS实现的AtomicInteger. AtomicLong. AtomicReference. AtomicBoolean也被称为乐观锁. CAS的语义是“我认为V的值应该为A,如果是,那么将 ...

  10. RandomAccess接口的使用

    RandomAccess在类Collections的shuffle()方法中的使用:(jdk源码如下) /** * Randomly permute the specified list using ...