springboot整合@Scheduled定时任务的使用
1.启动类里面添加注解@EnableScheduling ,例如:
@SpringBootApplication
@EnableScheduling
@MapperScan("com.example.liuyi.mapper")
public class LiuyiApplication { public static void main(String[] args) {
SpringApplication.run(LiuyiApplication.class, args);
} }
2.方法添加注解@Scheduled ,并且实现类上要有组件的注解@Component,
例如cron的使用场景:
@Component
public class TaskTest { /**
* 定义一个按时间执行的定时任务,每隔5秒执行1次,
cron表达式配置了在哪一刻执行任务,会在配置的任务开始时间判断任务是否可以执行,如果能则执行,不能则会跳过本次执行;
*/
@Scheduled(cron = "0/5 * * * * ?")
public void doEat() throws InterruptedException {
System.out.println("开始吃饭啦"+new Date());
Thread.sleep(7*1000);
System.out.println("结束吃饭啦" +new Date());
}

fixedDelay的使用场景
/**
* fixedDelay是设定上一个任务结束后多久执行下一个任务,也就是fixedDelay只关心上一任务的结束时间和下一任务的开始时间。
*/
@Scheduled(fixedDelay = 5*1000)
public void doPlay() throws InterruptedException {
System.out.println("开始玩啦"+new Date());
Thread.sleep(7*1000);
System.out.println("结束玩啦"+new Date());
}

fixedRate的使用场景
/**
* 定义一个按频率执行的任务
* 两个任务的开始时间间隔是5s,当到达任务的开始执行时间,但上一个任务却没有完成时,
* spring会等待上一个任务执行完,并立即开始执行本次任务。
*/
@Scheduled(fixedRate = 1000 * 5)
public void doJob() throws InterruptedException {
System.out.println("开始工作啦"+new Date());
Thread.sleep(7*1000);
System.out.println("结束工作啦" + new Date());
}

注意点:
1.SpringBoot 默认就是定时任务同步执行的,只要将@Scheduled添加到需要配置的任务方法上,下次任务执行开始将在本次任务执行完毕后才开始
同一任务的异步执行需要在方法体上加@Async注解
/**
* 同步执行
* @throws InterruptedException
*/
@Scheduled(cron = "*/20 * * * * ?")
public void ipWriter() throws InterruptedException {
for(int i=;i<;i++){
System.out.println("i----同步执行 "+new Date());
Thread.sleep();
}
} /**
* 异步执行
* @throws InterruptedException
*/
@Async
@Scheduled(cron = "*/20 * * * * ?")
public void ipWriterSync() throws InterruptedException {
for(int i=;i<;i++){
System.out.println("i----异步执行 "+new Date());
Thread.sleep();
}
}
2.多任务并发执行,在使用SpringBoot配置定时任务的过程中,使用@Scheduled配置了多个定时任务,但是在项目启动的时候每次只会启动一个定时任务,
因为 ThreadPoolTaskScheduler的源码默认开启的线程数是 1 ,所以每次只能执行一个定时任务,以下是部分源码
public class ThreadPoolTaskScheduler extends ExecutorConfigurationSupport implements AsyncListenableTaskExecutor, SchedulingTaskExecutor, TaskScheduler {
private volatile int poolSize = ;
public void setPoolSize(int poolSize) {
Assert.isTrue(poolSize > , "'poolSize' must be 1 or higher");
this.poolSize = poolSize;
if (this.scheduledExecutor instanceof ScheduledThreadPoolExecutor) {
((ScheduledThreadPoolExecutor)this.scheduledExecutor).setCorePoolSize(poolSize);
}
}
}
在启动的时候重新配置,创建BeanConfig类,注意,需要在类上添加@Component注解,项目启动的时候类中的@Bean注解才会被扫描到,使配置生效
@Component
public class ThreadPoolTaskSchedulerConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(2);//这里设置的线程数是2,可以根据需求调整
return taskScheduler;
} }
springboot整合@Scheduled定时任务的使用的更多相关文章
- SpringBoot整合Quartz定时任务 系统job Spring Boot教程 调度任务
原文地址:https://www.cnblogs.com/allalongx/p/8477368.html 构建工程 创建一个Springboot工程,在它的程序入口加上@EnableScheduli ...
- SpringBoot整合Quartz定时任务
记录一个SpringBoot 整合 Quartz 的Demo实例 POM.XML文件 <!-- 定时器任务 quartz需要导入的坐标 --> <dependency> < ...
- SpringBoot学习18:springboot使用Scheduled 定时任务器
Scheduled 定时任务器:是 Spring3.0 以后自带的一个定时任务器. 1.在pom.xml文件中添加Scheduled依赖 <!-- 添加spring定时任务 Scheduled ...
- SpringBoot整合Quartz定时任务(持久化到数据库)
背景 最近在做项目,项目中有个需求:需要使用定时任务,这个定时任务需要即时生效.查看Quartz官网之后发现:Quartz提供两种基本作业存储类型: RAMJobStore :RAM也就是内存,默认情 ...
- SpringBoot整合Quartz定时任务 的简单实例
POM.XML文件 <!-- 定时器任务 quartz需要导入的坐标 --> <dependency> <groupId>org.quartz-scheduler& ...
- Spring Boot笔记(四) springboot 集成 @Scheduled 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.在SpringBoot 项目中使用@Scheduled注解执行定时任务: 配置pom.xml 依赖: ...
- springboot 整合task定时任务
一步:在启动类中加入 加入就会调用定时了. //开启定时任务 开启后就可以被扫描到 @EnableScheduling 二步:建一个tasks工具包 都会被扫描到的了 有三个类 Async ...
- SpringBoot整合Quartz定时任务 的简单实例 2
(1)什么是Quartz?(2)Quartz的特点:(3)Quartz专用词汇说明:(4)Quartz任务调度基本实现原理: 接下来看下具体的内容: (1)什么是Quartz? Quartz是一个完全 ...
- springboot之scheduled任务调度
springboot整合Scheduled可以方便的进行任务调度,话不多说,直接上代码 package com.rookie.bigdata; import org.springframework.b ...
随机推荐
- Vue 各个阶段生命周期函数
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 11-28\enum
1.创建一个枚举对象,对象中4个属性video视频.book书----(这2个属性可以用数字表示). 2.创建一个class对象,对象中有2个属性,一个是id属性(自己设置),第二个属性是type类型 ...
- linux 存取 I/O 内存
在一些平台上, 你可能逃过作为一个指针使用 ioremap 的返回值的惩罚. 这样的使用不 是可移植的, 并且, 更加地, 内核开发者已经努力来消除任何这样的使用. 使用 I/O 内 存的正确方式是通 ...
- ZR979B. 【十联测 Day 9】唯一睿酱
ZR979B. [十联测 Day 9]唯一睿酱 题目大意: 给定一个数组\(r_i\),表明对于第\(i\)个数来说,他是\([max(1,i - r_i),min(n,i+r_i)]\)中最大的,求 ...
- Koa搭建简单服务器
1. dependencies "co-mysql": "^1.0.0", "koa": "^2.7.0", " ...
- dotnet 通过 WMI 获取系统补丁
本文告诉大家如何通过 WMI 获取补丁 通过 Win32_QuickFixEngineering 可以获取系统启动的服务 下面代码只是获取补丁的 kb 字符 const string query = ...
- destoon信息或者公司归属多个类别的解决方式
有时候,一条信息属对应于多个类别,在destoon原生系统里是没有这个解决方案 在公司行业类别选择的地方实现了这个功能,但是选择体验不太好,不符合我当前的需求,目前我写了如下解决方案 系统代码如下: ...
- 【Docker】安装MySQL彻底解决3306端口占用问题
1.问题闪现: 初次up mysql报3306端口被占用 yunduo@YunDuo:~/Work/Learning/Docker/docker_compose$ docker-compose up ...
- linux查看文件内容跳到文件底部和回到文件顶部的快捷键
有时候需要查看一些日志文件,然后要从底部开始查看的话 可以按 shift+g 即可跳到文件底部 要返回文件顶部的时候 按 gg即可
- QString 转换为 char *
一.QString 转换为 char * 将 QString 转 char *,需要用到 QByteArray 类,QByteArray 类的说明详见 Qt 帮助文档. 因为 char * 最后都有一 ...
