Spring Boot 定时任务单线程和多线程

Stella981
• 阅读 525

最近在写springboot项目中一个数据转移的组件,本来是用java中的timer和Executor实现

可以有个springboot测试时关闭单例工厂的现象。现在试一试spring自己的线程管理是否可是不包上面的错误

帖子中内容直接粘贴就可以实现

原贴连接已经附上

Spring Boot 的定时任务:

第一种:把参数配置到.properties文件中:

代码:

  1. package com.accord.task;

  2. import java.text.SimpleDateFormat;

  3. import java.util.Date;

  4. import org.springframework.scheduling.annotation.Scheduled;

  5. import org.springframework.stereotype.Component;

  6. /**

  7. * 从配置文件加载任务信息

  8. * [@author](https://my.oschina.net/arthor) 王久印

  9. * 2018年3月1日

  10. */

  11. [@Component](https://my.oschina.net/u/3907912)

  12. public class ScheduledTask {

  13. private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

  14. //@Scheduled(fixedDelayString = "${jobs.fixedDelay}")

  15. @Scheduled(fixedDelayString = "2000")

  16. public void getTask1() {

  17. System.out.println("任务1,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));

  18. }

  19. @Scheduled(cron = "${jobs.cron}")

  20. public void getTask2() {

  21. System.out.println("任务2,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));

  22. }

  23. }

application.properties文件:

  1. jobs.fixedDelay=5000

  2. jobs.cron=0/5 * * * * ?

SpringBootCron2Application.java中:

  1. package com.accord;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.scheduling.annotation.EnableScheduling;

  5. @SpringBootApplication

  6. @EnableScheduling

  7. public class SpringBootCron2Application {

  8. public static void main(String[] args) {

  9. SpringApplication.run(SpringBootCron2Application.class, args);

  10. }

  11. }

注:@EnableScheduling  这个一定要加上;否则,不会定时启动任务!

@Scheduled中的参数说明:

  1. @Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;

  2. @Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;

  3. @Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;

  4. @Scheduled(cron="* * * * * ?"):按cron规则执行。

在线Cron表达式生成器:http://cron.qqe2.com/

第二种定时任务:单线程和多线程

1、创建定时任务:

  1. package com.accord.task;

  2. import org.slf4j.Logger;

  3. import org.slf4j.LoggerFactory;

  4. import org.springframework.scheduling.annotation.Scheduled;

  5. import org.springframework.stereotype.Component;

  6. /**

  7. * 构建执行定时任务

  8. * @author 王久印

  9. * 2018年3月1日

  10. * TODO

  11. */

  12. @Component

  13. public class ScheduledTask2 {

  14. private Logger logger = LoggerFactory.getLogger(ScheduledTask2.class);

  15. private int fixedDelayCount = 1;

  16. private int fixedRateCount = 1;

  17. private int initialDelayCount = 1;

  18. private int cronCount = 1;

  19. @Scheduled(fixedDelay = 5000) //fixedDelay = 5000表示当前方法执行完毕5000ms后,Spring scheduling会再次调用该方法

  20. public void testFixDelay() {

  21. logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);

  22. }

  23. @Scheduled(fixedRate = 5000) //fixedRate = 5000表示当前方法开始执行5000ms后,Spring scheduling会再次调用该方法

  24. public void testFixedRate() {

  25. logger.info("===fixedRate: 第{}次执行方法", fixedRateCount++);

  26. }

  27. @Scheduled(initialDelay = 1000, fixedRate = 5000) //initialDelay = 1000表示延迟1000ms执行第一次任务

  28. public void testInitialDelay() {

  29. logger.info("===initialDelay: 第{}次执行方法", initialDelayCount++);

  30. }

  31. @Scheduled(cron = "0 0/1 * * * ?") //cron接受cron表达式,根据cron表达式确定定时规则

  32. public void testCron() {

  33. logger.info("===initialDelay: 第{}次执行方法", cronCount++);

  34. }

  35. }

使用 @Scheduled来创建定时任务 这个注解用来标注一个定时任务方法。 
通过看 @Scheduled源码可以看出它支持多种参数:
    (1)cron:cron表达式,指定任务在特定时间执行;
    (2)fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
    (3)fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
    (4)fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
    (5)fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
    (6)initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
    (7)initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
    (8)zone:时区,默认为当前时区,一般没有用到。

2、开启定时任务:

  1. package com.accord;

  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.scheduling.annotation.EnableScheduling;

  5. @SpringBootApplication

  6. @EnableScheduling

  7. public class SpringBootCron2Application {

  8. public static void main(String[] args) {

  9. SpringApplication.run(SpringBootCron2Application.class, args);

  10. }

  11. }

注:这里的 @EnableScheduling  注解,它的作用是发现注解 @Scheduled的任务并由后台执行。没有它的话将无法执行定时任务。
引用官方文档原文:
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.

3、执行结果(单线程)

就完成了一个简单的定时任务模型,下面执行springBoot观察执行结果:

Spring Boot 定时任务单线程和多线程

从控制台输入的结果中我们可以看出所有的定时任务都是在同一个线程池用同一个线程来处理的,那么我们如何来并发的处理各定时任务呢,请继续向下看。

4、多线程处理定时任务:

看到控制台输出的结果,所有的定时任务都是通过一个线程来处理的,我估计是在定时任务的配置中设定了一个SingleThreadScheduledExecutor,于是我看了源码,从ScheduledAnnotationBeanPostProcessor类开始一路找下去。果然,在ScheduledTaskRegistrar(定时任务注册类)中的ScheduleTasks中又这样一段判断:

  1. if (this.taskScheduler == null) {

  2. this.localExecutor = Executors.newSingleThreadScheduledExecutor();

  3. this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);

  4. }

这就说明如果taskScheduler为空,那么就给定时任务做了一个单线程的线程池,正好在这个类中还有一个设置taskScheduler的方法:

  1. public void setScheduler(Object scheduler) {

  2. Assert.notNull(scheduler, "Scheduler object must not be null");

  3. if (scheduler instanceof TaskScheduler) {

  4. this.taskScheduler = (TaskScheduler) scheduler;

  5. }

  6. else if (scheduler instanceof ScheduledExecutorService) {

  7. this.taskScheduler = new ConcurrentTaskScheduler(((ScheduledExecutorService) scheduler));

  8. }

  9. else {

  10. throw new IllegalArgumentException("Unsupported scheduler type: " + scheduler.getClass());

  11. }

  12. }

这样问题就很简单了,我们只需用调用这个方法显式的设置一个ScheduledExecutorService就可以达到并发的效果了。我们要做的仅仅是实现SchedulingConfigurer接口,重写configureTasks方法就OK了;

package com.pingan.ph.cis.data.transfer.selfoperation;

import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;

@Configuration //所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。 public class ScheduleConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { //设定一个长度10的定时任务线程池 taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10)); } }

5、执行结果(并发)

Spring Boot 定时任务单线程和多线程

通过控制台输出的结果看出每个定时任务都是在通过不同的线程来处理了。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java日期时间API系列31
  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前
Stella981 Stella981
2年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Easter79 Easter79
2年前
Spring注解@Scheduled 多线程异步执行
一、前言:Spring定时任务@Schedule的使用方式,默认是单线程同步执行的,启动过程是一个单线程同步启动过程,一旦中途被阻塞,会导致整个启动过程阻塞,其余的定时任务都不会启动。二、@Schedule注解多线程的实现:多个定时任务的执行,通过使用@Async注解来实现多线程异步调用。@Scheduled(
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Stella981 Stella981
2年前
HIVE 时间操作函数
日期函数UNIX时间戳转日期函数: from\_unixtime语法:   from\_unixtime(bigint unixtime\, string format\)返回值: string说明: 转化UNIX时间戳(从19700101 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式举例:hive   selec
京东云开发者 京东云开发者
6个月前
Java服务总在半夜挂,背后的真相竟然是... | 京东云技术团队
最近有用户反馈测试环境Java服务总在凌晨00:00左右挂掉,用户反馈Java服务没有定时任务,也没有流量突增的情况,Jvm配置也合理,莫名其妙就挂了
Python进阶者 Python进阶者
4个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这