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

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

能够有个springboot测试时关闭单例工厂的现象。如今试一试spring本身的线程管理是否但是不包上面的错误spring

 

帖子中内容直接粘贴就能够实现springboot

原贴链接已经附上多线程

Spring Boot 的定时任务:并发

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

代码:ide

 

 
  1. package com.accord.task;测试

  2.  
  3. import java.text.SimpleDateFormat;this

  4. import java.util.Date;spa

  5.  
  6. import org.springframework.scheduling.annotation.Scheduled;

  7. import org.springframework.stereotype.Component;

  8.  
  9. /**

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

  11. * @author 王久印

  12. * 2018年3月1日

  13. */

  14. @Component

  15. public class ScheduledTask {

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

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

  20. @Scheduled(fixedDelayString = "2000")

  21. public void getTask1() {

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

  23. }

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

  26. public void getTask2() {

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

  28. }

  29. }

application.properties文件:

 

 
  1. jobs.fixedDelay=5000

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

SpringBootCron2Application.java中:

 

 
  1. package com.accord;

  2.  
  3. import org.springframework.boot.SpringApplication;

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

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

  6.  
  7. @SpringBootApplication

  8. @EnableScheduling

  9. public class SpringBootCron2Application {

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

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

  12. }

  13. }

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

@Scheduled中的参数说明:

 

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

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

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

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

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

 

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

一、建立定时任务:

 

 
  1. package com.accord.task;

  2.  
  3. import org.slf4j.Logger;

  4. import org.slf4j.LoggerFactory;

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

  6. import org.springframework.stereotype.Component;

  7.  
  8. /**

  9. * 构建执行定时任务

  10. * @author 王久印

  11. * 2018年3月1日

  12. * TODO

  13. */

  14. @Component

  15. public class ScheduledTask2 {

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

  18.  
  19. private int fixedDelayCount = 1;

  20. private int fixedRateCount = 1;

  21. private int initialDelayCount = 1;

  22. private int cronCount = 1;

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

  25. public void testFixDelay() {

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

  27. }

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

  30. public void testFixedRate() {

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

  32. }

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

  35. public void testInitialDelay() {

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

  37. }

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

  40. public void testCron() {

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

  42. }

  43.  
  44. }

使用 @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:时区,默认为当前时区,通常没有用到。

二、开启定时任务:

 

 
  1. package com.accord;

  2.  
  3. import org.springframework.boot.SpringApplication;

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

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

  6.  
  7. @SpringBootApplication

  8. @EnableScheduling

  9. public class SpringBootCron2Application {

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

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

  12. }

  13. }

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

三、执行结果(单线程)

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

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

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

看到控制台输出的结果,全部的定时任务都是经过一个线程来处理的,我估计是在定时任务的配置中设定了一个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));
    }
}

 

五、执行结果(并发)

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

相关文章
相关标签/搜索