如何优雅的实现 SpringBoot 并行任务

简介: 我是小假 期待与你的下一次相遇 ~

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

代码:

  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * 从配置文件加载任务信息
  7. */
  8. @Component
  9. public class ScheduledTask {
  10.    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
  11.    //@Scheduled(fixedDelayString = "${jobs.fixedDelay}")
  12.    @Scheduled(fixedDelayString = "2000")
  13.    public void getTask1() {
  14.        System.out.println("任务1,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));
  15.    }
  16.    @Scheduled(cron = "${jobs.cron}")
  17.    public void getTask2() {
  18.        System.out.println("任务2,从配置文件加载任务信息,当前时间:" + dateFormat.format(new Date()));
  19.    }
  20. }

application.properties文件:

  1. jobs.fixedDelay=5000
  2. jobs.cron=0/5 * *  * * ?

SpringBootCron2Application.java中:

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.scheduling.annotation.EnableScheduling;
  4. @SpringBootApplication
  5. @EnableScheduling
  6. public class SpringBootCron2Application {
  7.    public static void main(String[] args) {
  8.        SpringApplication.run(SpringBootCron2Application.class, args);
  9.    }
  10. }

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

@Scheduled中的参数说明:

  • **@Scheduled(fixedRate=2000)**:上一次开始执行时间点后2秒再次执行;
  • **@Scheduled(fixedDelay=2000)**:上一次执行完毕时间点后2秒再次执行;
  • **@Scheduled(initialDelay=1000, fixedDelay=2000)**:第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;
  • **@Scheduled(cron="* * * * * ?")**:按cron规则执行。

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

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

1、创建定时任务:

  1. import org.slf4j.Logger;
  2. import org.slf4j.LoggerFactory;
  3. import org.springframework.scheduling.annotation.Scheduled;
  4. import org.springframework.stereotype.Component;
  5. /**
  6. * 构建执行定时任务
  7. * TODO
  8. */
  9. @Component
  10. public class ScheduledTask2 {
  11.    private Logger logger = LoggerFactory.getLogger(ScheduledTask2.class);
  12.    private int fixedDelayCount = 1;
  13.    private int fixedRateCount = 1;
  14.    private int initialDelayCount = 1;
  15.    private int cronCount = 1;
  16.    @Scheduled(fixedDelay = 5000)        //fixedDelay = 5000表示当前方法执行完毕5000ms后,Spring scheduling会再次调用该方法
  17.    public void testFixDelay() {
  18.        logger.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
  19.    }
  20.    @Scheduled(fixedRate = 5000)        //fixedRate = 5000表示当前方法开始执行5000ms后,Spring scheduling会再次调用该方法
  21.    public void testFixedRate() {
  22.        logger.info("===fixedRate: 第{}次执行方法", fixedRateCount++);
  23.    }
  24.    @Scheduled(initialDelay = 1000, fixedRate = 5000)   //initialDelay = 1000表示延迟1000ms执行第一次任务
  25.    public void testInitialDelay() {
  26.        logger.info("===initialDelay: 第{}次执行方法", initialDelayCount++);
  27.    }
  28.    @Scheduled(cron = "0 0/1 * * * ?")  //cron接受cron表达式,根据cron表达式确定定时规则
  29.    public void testCron() {
  30.        logger.info("===initialDelay: 第{}次执行方法", cronCount++);
  31.    }
  32. }

使用 @Scheduled来创建定时任务 这个注解用来标注一个定时任务方法。

通过看 @Scheduled源码可以看出它支持多种参数:

  • **cron**:cron表达式,指定任务在特定时间执行;
  • **fixedDelay**:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
  • **fixedDelayString**:与**fixedDelay**含义一样,只是参数类型变为String;
  • **fixedRate**:表示按一定的频率执行任务,参数类型为long,单位ms;
  • **fixedRateString**: 与**fixedRate**的含义一样,只是将参数类型变为String;
  • **initialDelay**:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
  • **initialDelayString**:与**initialDelay**的含义一样,只是将参数类型变为String;
  • **zone**:时区,默认为当前时区,一般没有用到。 2、开启定时任务:
    ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication @EnableScheduling public class SpringBootCron2Application { public static void main(String[] args) { SpringApplication.run(SpringBootCron2Application.class, args); } }

  1. 注:这里的 `@EnableScheduling`  注解,它的作用是发现注解 `@Scheduled`的任务并由后台执行。没有它的话将无法执行定时任务。<br />引用官方文档原文:
  2. > `@EnableScheduling` ensures that a background task executor is created. Without it, nothing gets scheduled.
  3. <a name="nM655"></a>
  4. #### 3、执行结果(单线程)
  5. 就完成了一个简单的定时任务模型,从控制台输入的结果中可以看出所有的定时任务都是在同一个线程池用同一个线程来处理的,那么如何来并发的处理各定时任务呢,请继续向下看。
  6. <a name="RXWwg"></a>
  7. #### 4、多线程处理定时任务:
  8. 看到控制台输出的结果,所有的定时任务都是通过一个线程来处理的,估计是在定时任务的配置中设定了一个`SingleThreadScheduledExecutor`,于是看了源码,从`ScheduledAnnotationBeanPostProcessor`类开始一路找下去。果然,在`ScheduledTaskRegistrar`(定时任务注册类)中的`ScheduleTasks`中又这样一段判断:
  9. ```java
  10. if (this.taskScheduler == null) {
  11.    this.localExecutor = Executors.newSingleThreadScheduledExecutor();
  12.    this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
  13. }

这就说明如果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了;

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  3. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  4. import java.util.concurrent.Executors;
  5. /**
  6. * 多线程执行定时任务
  7. */
  8. @Configuration
  9. //所有的定时任务都放在一个线程池中,定时任务启动时使用不同都线程。
  10. public class ScheduleConfig implements SchedulingConfigurer {
  11.    @Override
  12.    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  13.        //设定一个长度10的定时任务线程池
  14.        taskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
  15.    }
  16. }

5、执行结果(并发)

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


相关文章
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
7月前
|
Java 关系型数据库 MySQL
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
springboot项目集成dolphinscheduler调度器 实现datax数据同步任务
750 2
|
7月前
|
人工智能 安全 Java
Spring Boot 中使用 Function 和异步线程池处理列表拆分任务并汇总结果
在Java开发中,处理大规模数据时常常需要将列表拆分为多个子列表进行异步处理并汇总结果。本文介绍如何在Spring Boot中使用Function和异步线程池实现高效且可维护的代码,涵盖结果封装、线程池配置、列表拆分处理及结果汇总等关键步骤。
343 0
|
8月前
|
SQL JSON Java
|
分布式计算 大数据 Java
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
springboot项目集成大数据第三方dolphinscheduler调度器 执行/停止任务
174 0
|
Java 调度 数据库
SpringBoot整合XXL-JOB【05】- 任务分片
在实际业务中,批量定时任务可能因上一批任务未完成而影响业务。为解决此问题,本文介绍如何使用Xxl-job对批量任务进行分片处理,通过分片广播形式调度集群机器并行执行任务,大幅提升执行效率。具体步骤包括环境准备、添加依赖和配置、声明实体类与查询类,以及改造业务逻辑实现分片查询。测试结果显示,分片处理将两千条数据的执行时间从30秒缩短至15秒,性能提升显著。
1790 13
SpringBoot整合XXL-JOB【05】-  任务分片
|
前端开发 Java API
SpringBoot整合Flowable【07】- 驳回节点任务
本文通过绩效流程的业务场景,详细介绍了如何在Flowable工作流引擎中实现任务驳回功能。具体步骤包括:获取目标任务节点和当前任务节点信息,进行必要的判空和逻辑校验,调用API完成节点回退,并清理相关脏数据(如历史任务和变量)。最后通过测试验证了驳回功能的正确性,确保流程能够成功回退到指定节点并清除中间产生的冗余数据。此功能在实际业务中非常有用,能够满足上级驳回自评等需求。
2140 0
SpringBoot整合Flowable【07】- 驳回节点任务
|
消息中间件 缓存 监控
【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控
springboot的热部署、配置的宽松绑定和校验、任务、邮件、监控、springboot整合JdbcTemplate,h2等sql技术、整合redis,mongodb,es等nosql技术、整合redis,Memcached,jetcache,j2cache等缓存技术、整合ActiveMQ,RabbitMQ,RocketMQ,Kafka等消息的中间件的入门、整合缓存/任务/邮件/监控
【Java笔记+踩坑】SpringBoot基础3——开发。热部署+配置高级+整合NoSQL/缓存/任务/邮件/监控
|
SQL Java 调度
实时计算 Flink版产品使用问题之使用Spring Boot启动Flink处理任务时,使用Spring Boot的@Scheduled注解进行定时任务调度,出现内存占用过高,该怎么办
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
Java 数据安全/隐私保护
SpringBoot 自定义初始化任务 Runner
SpringBoot 自定义初始化任务 Runner
241 0

热门文章

最新文章