IDEA使用springboot自带scheduled实现任务调度

 

  之前写过使用quartz、xxl实现任务调度,最近有空总结了使用springboot自带的scheduled方式实现任务调度

  • 打开IDEA选择file->new->project

  • 写上group名和artifact名

  • 选择web项目,选择Spring Web,最后下一步完成即可

  • pom.xml文件中添加日志打印依赖
1
2
3
4
5
6
7
<!-- SLf4j 日志记录-->
   <dependency>
       <groupId>org.projectlombok</groupId>
       <artifactId>lombok</artifactId>
       <optional>true</optional>
       <version>1.18.12</version>
   </dependency>

  

  • 添加service包,并创建TestJob类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.springboot.scheduled.service;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.TimeUnit;
 
@Component
@Slf4j
public class TestJob {
    @Scheduled(initialDelay = 1000, fixedRate = 1000 * 10)   //启动后1秒后执行,每10秒执行一次
    public void Task1() throws InterruptedException {
        log.info("task1 任务开始, 线程执行ID:" + Thread.currentThread().getId() + "线程名称:" + Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(10);
        log.info("task1 任务结束, 线程执行ID:" + Thread.currentThread().getId() + "线程名称:" + Thread.currentThread().getName());
 
    }
 
    @Scheduled(initialDelay = 1000, fixedRate = 1000 * 5)   //每5秒执行一次
    public void Task2() throws InterruptedException {
        log.info("task2 任务开始, 线程执行ID:" + Thread.currentThread().getId() + "线程名称:" + Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(10);
        log.info("task2 任务结束, 线程执行ID:" + Thread.currentThread().getId() + "线程名称:" + Thread.currentThread().getName());
 
    }
}

  

  • 在入口程序,添加@EnableScheduling注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.springboot.scheduled;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
 
@SpringBootApplication
@EnableScheduling
public class ScheduledApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ScheduledApplication.class, args);
    }
 
}

  

  • 完成后运行效果输出:

  从上面的执行结果中,我们可以看出task1任务执行完成后,task2才开始执行,这是由于springboot中任务调度默认是单线程(串行)执行的,如果想多线程(并行)执行需要添加配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.springboot.scheduled.config;
 
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;
 
@Configuration
 
@EnableScheduling
public class ScheduleConfig implements SchedulingConfigurer {
 
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(100));
    }
 
}
  • 添加并行执行配置后,执行效果:

  从上图可以看出,task1和task2同时执行了

源码下载:https://download.csdn.net/download/haojuntu/14028584

posted @   低调码农哥!  阅读(763)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示