特别说明:SpringBoot 中创建一个定时任务,这个时候千万呀明白一个概念,在springBoot中的定时任务本身属于一个service如果要用到SpringBoot中的注解的话只能引入Mapper。
1、springboot 的定时任务:
一、静态:基于注解(@Scheduled)
二、动态:基于接口(SchedulingConfigurer)
三、多线程定时任务
一、基于注解:
说明:基于注解@Scheduled默认为单线程,开启多个任务时,任务的执行时机会受上一个任务执行时间的影响。
1、创建定时器
使用springBoot 基于注解来创建定时任务非常简单,
@Component
@Configuration //1、主要用于标记配置类,兼备Component的效果
@EnableScheuling //2、开启定时任务
public class StaticSecheduleTask{
//添加定时任务
@Scheduled(cron ="0/5 * * * * ?")
//或者直接指定时间间隔,例如5秒
//@Scheduled(fixedRate=5000)
private void configureTasks() {
System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
}
}
Cron表达式参数分别表示:
秒(0~59) 例如0/5表示每5秒
分(0~59)
时(0~23)
日(0~31)的某天,需计算
月(0~11)
周几( 可填1-7 或 SUN/MON/TUE/WED/THU/FRI/SAT)
@Scheduled:除了支持灵活的参数表达式cron之外,还支持简单的延时操作,例如 fixedDelay ,fixedRate 填写相应的毫秒数即可。
说明;使用@Scheduled 注解很方便,但缺点是当我们调整了执行周期的时候,需要重启应用才能生效,这多少有些不方便。为了达到实时生效的效果,可以使用接口来完成定时任务。
二、动态:基于接口(SchedulingConfigurer)
1、导入依赖包:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency><!--添加Web依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><!--添加MySql依赖 -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency><!--添加Mybatis依赖 配置mybatis的一些初始化的东西-->
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency><!-- 添加mybatis依赖 -->
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
在项目中的application.yml添加数据源:
spring:
datasource:
url: jdbc:mysql://localhost:3306/socks
username: root
password: 123456
创建定时器:
@Component
@Configuration //主要用于标记配置类,兼备Component 的效果
@EnableScheduling //开启定时任务
public class DynamicScheduleTask implements SchedulingConfigurer{
@Mapper
public interface CronMapper{
@Select("select cron from cron limit 1")
public String getCron();
}
@Autowired //注入mapper
@SuppressWarnings("all")
CronMapper cronMapper;
/**执行定时任务*/
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar){
taskRegistrar.addTriggerTask(
//1.添加任务内容(Runnable)
() -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),
//2.设置执行周期(Trigger)
triggerContext -> {
//2.1 从数据库获取执行周期
String cron = cronMapper.getCron();
//2.2 合法性校验.
if (StringUtils.isEmpty(cron)) {
// Omitted Code ..
}
//2.3 返回执行周期(Date)
return new CronTrigger(cron).nextExecutionTime(triggerContext);
}
);
}
}
三、多线程定时任务
springBoot 中的定时任务就是一个service 所以运用的时候在service 只能引用注解Mapper
基于注解设定多线程定时任务
1、创建多线程定时任务
//@Component注解用于对那些比较中立的类进行注释;
//相对与在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释
@Component
@EnableScheduling // 1.开启定时任务
@EnableAsync // 2.开启多线程
public class MultithreadScheduleTask {
@Async
@Scheduled(fixedDelay = 1000) //间隔1秒
public void first() throws InterruptedException {
System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
System.out.println();
Thread.sleep(1000 * 10);
}
@Async
@Scheduled(fixedDelay = 2000)
public void second() {
System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "\r\n线程 : " + Thread.currentThread().getName());
System.out.println();
}
}
@Scheduled介绍:
@Scheduled 为设置定时任务周期的注解,参数常用的常为2种
第一种就是fixedRate 他表示以一种固定频率去执行,单位毫秒,例如@Scheduled(fixedRate =5000)表示为每5秒执行一次
第二种为cron,他可以表示某种特定频率,,例如每天晚上三点执行,每个星期三中午十二点等
具体cron表达式用法大家可以百度,这里列出几个常用的:
每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?
每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?
每隔5分钟执行一次:0 0/5 * * * ?
例子:
@Component
/**
* 开启定时任务的注解
*/
@EnableScheduling
public class tesk {
@Scheduled(fixedRate = 5000)
public void job1(){
System.out.println("定时任务1" + new Date());
}
@Scheduled(cron = "0/5 * * * * ?")
public void job2(){
System.out.println("定时任务2" + new Date());
}
}