一、spring task单线程
在Spring Boot中集成Spring Task非常简单。Spring Boot本身就是基于Spring构建的,因此已经内置了Spring Task的功能。以下是在Spring Boot项目中集成Spring Task的步骤:
1、Spring Boot配置文件添加配置
(例如application.properties或application.yml)中,添加以下属性来启用Spring Task:
spring.task.enabled=true
2、添加@Scheduled注解
在Spring Boot项目中创建一个类,用于定义需要执行的任务。这个类可以是一个普通的Java类,并使用@Scheduled注解来指定任务的执行时间。
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyTask {
//启动后,延迟10s调用方法,每间隔15s执行一次
@Scheduled(initialDelay = 10000,fixedRate = 15000)
public void executeTask() {
// 在这里编写需要执行的任务逻辑
System.out.println("执行任务...");
}
//启动后,延迟10s调用方法,上个任务结束后,每隔15s执行一次
@Scheduled(initialDelay = 10000,fixedDelay = 15000)
public void executeTask() {
System.out.println("执行任务...");
}
@Scheduled(cron = "0/5 * * * * *")
public void scheduled(){
System.out.println("=====>>>>>使用cron {}",System.currentTimeMillis());
}
}
fixedDelay:表示上一个任务执行结束后隔一段时间再开始下一次任务。即当任务执行完毕后,会等待一段时间,然后再执行下一次任务。如果上一个任务执行的时间很长,那么下一个任务会延迟执行。
fixedRate:表示以固定的频率执行任务。即无论上一个任务执行的时间长短,下一次任务都会在固定的时间间隔后开始执行。如果上一个任务执行的时间很长,那么下一个任务也会按照固定的时间间隔开始执行,可能会导致并发执行多个任务。
因此,fixedDelay适合任务执行时间较长的场景,而fixedRate适合任务执行时间较短的场景。
3、使用@EnableScheduling注解来启用任务调度功能。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class MySpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApplication.class, args);
}
}
在上述代码中,@EnableScheduling注解用于启用任务调度功能。
运行Spring Boot应用程序。一旦应用程序启动,定义的任务将会按照指定的时间间隔自动执行。
这就是在Spring Boot中集成Spring Task的基本步骤。通过这种方式,你可以轻松地在Spring Boot应用程序中执行计划任务。请注意,在使用Spring Task时,还需要注意任务的性能和资源消耗,以确保应用程序的稳定性和可靠性。
二、spring task 多线程
在传统的Spring项目中,我们可以在xml配置文件添加task的配置,而在SpringBoot项目中一般使用config配置类的方式添加配置,所以新建一个AsyncConfig
类.
@Configuration
@EnableAsync
public class AsyncConfig {
/*
*此处成员变量应该使用@Value从配置中读取
*/
private int corePoolSize = 10;
private int maxPoolSize = 200;
private int queueCapacity = 10;
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.initialize();
return executor;
}
}
@Configuration
:表明该类是一个配置类
@EnableAsync
:开启异步事件的支持
然后在定时任务的类或者方法上添加@Async
。最后重启项目,每一个任务都是在不同的线程中。
写在最后:
cron表达式详解
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。按顺序依次为:
秒(0~59)
分钟(0~59)
小时(0~23)
天(0~31)
月(0~11)
星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于”月份中的日期”和”星期中的日期”这两个元素互斥的,必须要对其中一个设置。推荐: Spring快速开启计划。
配置实例:
每隔5秒执行一次:/5 * ?
每隔1分钟执行一次:0 /1 ?
0 0 10,14,16 ? 每天上午10点,下午2点,4点
0 0/30 9-17 ? 朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点
“0 0 12 ?” 每天中午12点触发
“0 15 10 ? “ 每天上午10:15触发
“0 15 10 ?” 每天上午10:15触发
“0 15 10 ? *” 每天上午10:15触发
“0 15 10 ? 2005” 2005年的每天上午10:15触发
“0 14 * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 ?” 在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
“0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
“0 15 10 15 * ?” 每月15日上午10:15触发
“0 15 10 L * ?” 每月最后一日的上午10:15触发
“0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发