基于springboot的定时任务实现(非分布式)
1. 核心注解
在springboot项目中我们可以很方便地使用spring自己的注解@Scheduled
和@EnableScheduling
配合来实现便捷开发定时任务。
@EnableScheduling
注解的作用是发现注解@Scheduled
的任务并后台执行,此注解可以加到启动类上也可以加到执行调度任务类上。
经测试,当有多个包含定时任务的类时,@EnableScheduling
注解加在其中一个类上就可以保证所有定时任务的成功实现。
注意:定时任务的类上还需要配合使用@Configuration
或@Component
注解,这两个注解都可以。
2. 实例代码:
2.1 @EnableScheduling
加在启动类上;
import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @description:
* @author: Karl
* @date: 2020/10/10
*/
@Component
public class TestSchedule01 {
@Scheduled(cron = "0 * * * * ? ")
public void test() {
System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date()));
}
}
import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @description:
* @author: Karl
* @date: 2020/10/10
*/
@Configuration
public class TestSchedule02 {
@Scheduled(cron = "1 * * * * ? ")
public void test() {
System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date()));
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.1 @EnableScheduling
加在任务类上;
import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @description:
* @author: Karl
* @date: 2020/10/10
*/
@Component
@EnableScheduling
public class TestSchedule01 {
@Scheduled(cron = "0 * * * * ? ")
public void test() {
System.out.println("我是定时任务01,我执行了" + DateUtil.formatDateByDateTime(new Date()));
}
}
import com.my.common.util.DateUtil;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @description:
* @author: Karl
* @date: 2020/10/10
*/
@Configuration
public class TestSchedule02 {
@Scheduled(cron = "1 * * * * ? ")
public void test() {
System.out.println("我是定时任务02,我执行了" + DateUtil.formatDateByDateTime(new Date()));
}
}
注意:只需要在其中一个任务类上加上@EnableScheduling
注解,所有的定时任务就都可以正常运行。
3. @Scheduled
的几种用法
@Scheduled
这个注解支持3种定时方式,即:cron、fixedRate和fixedDelay
cron:是以表达式的形式来表示时间,最常见;
fixedRate:表示Scheduled隔多长时间调用一次,不管任务是否执行完;
fixedDelay:表示该任务执行完后隔多长时间再调用;
基于springboot的定时任务实现(非分布式)的更多相关文章
- 基于SpringBoot实现定时任务的设置(常用:定时清理数据库)
1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...
- 分布式 redis 延时任务 基于 springboot 示例
Lilishop 技术栈 官方公众号 & 开源不易,如有帮助请点Star 介绍 官网:https://pickmall.cn Lilishop 是一款Java开发,基于SpringBoot研发 ...
- 基于SpringBoot AOP面向切面编程实现Redis分布式锁
基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式 ...
- 基于SpringBoot+SSM实现的Dota2资料库智能管理平台
Dota2资料库智能管理平台的设计与实现 摘 要 当今社会,游戏产业蓬勃发展,如PC端的绝地求生.坦克世界.英雄联盟,再到移动端的王者荣耀.荒野行动的火爆.都离不开科学的游戏管理系统,游戏管理系 ...
- 【spring boot】【redis】spring boot基于redis的LUA脚本 实现分布式锁
spring boot基于redis的LUA脚本 实现分布式锁[都是基于redis单点下] 一.spring boot 1.5.X 基于redis 的 lua脚本实现分布式锁 1.pom.xml &l ...
- springboot实现定时任务的方式
springboot实现定时任务的方式 a Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程 ...
- SpringBoot执行定时任务@Scheduled
SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...
- MyBatis 进阶,MyBatis-Plus!(基于 Springboot 演示)
这一篇从一个入门的基本体验介绍,再到对于 CRUD 的一个详细介绍,在介绍过程中将涉及到的一些问题,例如逐渐策略,自动填充,乐观锁等内容说了一下,只选了一些重要的内容,还有一些没提及到,具体可以参考官 ...
- 搭建基于springboot轻量级读写分离开发框架
何为读写分离 读写分离是指对资源的修改和读取进行分离,能解决很多数据库瓶颈,以及代码混乱难以维护等相关的问题,使系统有更好的扩展性,维护性和可用性. 一般会分三个步骤来实现: 一. 主从数据库搭建 信 ...
随机推荐
- pipeline 共享库
目录 一.简介 二.共享库扩展 共享库使用 共享库结构 pipeline模板 一些小问题 三.共享库例子 使用公共变量 使用共享库的src方法 使用共享库的vars方法 四.插件实现pipeline ...
- pipeline脚本管理
目录 一.代码仓库 二.远程拉取 一.代码仓库 1.使用gitlab做pipeline脚本的存储,新建一个仓库 2.新建文件,把代码放进去 脚本名可以按照规律填写,环境_应用名_类型,例如:test_ ...
- 层次分析法、模糊综合评测法实例分析(涵盖各个过程讲解、原创实例示范、MATLAB源码公布)
目录 一.先定个小目标 二.层次分析法部分 2.1 思路总括 2.2 构造两两比较矩阵 2.3 权重计算方法 2.3.1 算术平均法求权重 2.3.2 几何平均法求权重 2.3.3 特征值法求权重 2 ...
- .NET内存性能分析宝典
.NET Memory Performance Analysis 知道什么时候该担心,以及在需要担心的时候该怎么做 译者注 **作者信息:Maoni Stephens ** - 微软架构师,负责.NE ...
- jarvisoj_fm(格式字符串)
我又回来了,前几天被timeout问题折磨,还是放弃了 拿到题目还是file一下 看到时32位的程序,于是把程序放入ida*32中 可以看到当x等于4的时候可以拿到shell,上面的printf(bu ...
- linux 编程随笔
Linux 命令: 在linux 系统中,所有的命令都是人为编写的程序,如 who 和 ls ,而且绝大多数都是C写的.在Linux 中增加新的命令是很简单的事,把程序的可执行文件放到以下目录就可以了 ...
- jenkins+docker+k8s项目发布
目录 一.简介 二.新建docker-build工程 三.项目部署 四.访问测试 一.简介 1.该章节基于jenkins.Harbor.pipeline.k8s来做发布,如对这些不熟悉,请按以下进入学 ...
- java 输入输出IO流 字节流| 字符流 的缓冲流:BufferedInputStream;BufferedOutputStream;BufferedReader(Reader in);BufferedWriter(Writer out)
什么是缓冲流: 缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率. 图解: 1.字节缓冲流BufferedInputStr ...
- 『学了就忘』Linux日志管理 — 91、日志服务rsyslogd说明
目录 1.日志文件格式 2.rsyslogd服务的配置文件 (1)rsyslog.conf文件内容 (2)rsyslog.conf配文件内容说明 (3)定义自己的日志 1.日志文件格式 只要是由日志服 ...
- Qt5绘制仪表盘dashboard
说明 本文演示Qt版本: Qt5.14. 本文将使用QPainter一步一步绘制仪表盘:刻度.指针.刻度值 注意: 绘制顺序,如果先绘制,则后来绘制的将会覆盖住先前绘制的. 如果需要绘制半透明, 请设 ...