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 ...
随机推荐
- H3C 路由优先级
- LOGO的浮空显示-Verilog
为了方便生成准确的mif数据,以实现特定的透明效果.使用Photoshop将网上下载的Logo修改颜色,保存大小为120*120像素,如图1所示. 图1 ps修改后的Logo 使用Pic2mif软件, ...
- eslint在webstorm中有错误警告
1. 报错Missing space before function parentheses的问题 解决:在代码目录中,打开.eslint文件,并在rules中添加如下一行代码即可: "sp ...
- 天使玩偶/SJY摆棋子
P4169 [Violet]天使玩偶/SJY摆棋子 CDQ分治的题目. 我们发现题目要我们求的\(|A_x-B_x|+|A_y-B_y|\)的绝对值号比较恶心. 试想一下怎么去掉 如果所有的点都在我们 ...
- oracle 包 简单使用
理解PL/SQL包 简介 包(package)的主要作用是用于逻辑组合相关的PL/SQL类型 比如记录类型或者集合类型,PL/SQL游标或游标声明以及PL/SQL子程序 还可以包含任何可以在块的声明区 ...
- SAPI(PHP常见的四种运行模式)
SAPI(Server Application Programming Interface)服务器应用程序编程接口,即PHP与其他应用交互的接口,PHP脚本要执行有很多方式,通过Web服务器,或者直接 ...
- JMM中的Happens-Before原则
在java内存模型中,happens-before应该理解为:前一个操作的结果,可以被后续的操作获取,即内存可见性. 为了解决多线程的内存可见性问题,就提出了happens-before原则, ...
- ELK学习实验010:Logstash简介
Logstash是具有实时流水线功能的开源数据收集引擎.Logstash可以动态统一来自不同来源的数据,并将数据规范化为您选择的目标.清除所有数据并使其民主化,以用于各种高级下游分析和可视化用例. 虽 ...
- spring boot(一)创建项目
网上有很多springboot的入门教程,自己也因为项目要使用springboot,所以利用业余时间自学了下springboot和springcloud,使用下来发现springboot还是挺简单的, ...
- 分析CPU使用率不断增加的原因
工程中发现引起的问题: 结合别的朋友的意见,我的优化思路是: 1.排查是否内存泄漏 经过反复查询代码,未发现有内存泄漏(可以自己百度搜索C#内存泄漏的原因).可以通过任务管理器分析是否有内存泄漏,打开 ...
