基于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轻量级读写分离开发框架
何为读写分离 读写分离是指对资源的修改和读取进行分离,能解决很多数据库瓶颈,以及代码混乱难以维护等相关的问题,使系统有更好的扩展性,维护性和可用性. 一般会分三个步骤来实现: 一. 主从数据库搭建 信 ...
随机推荐
- Jenkins检测Maven项目是否引用快照包
目录 一.简介 二.具体 一.简介 生产环境不允许使用快照包,但人为规定终究不如脚本进行检测,所以在打war包,检测是否引用了快照包,如果引用了宣布打包失败 二.具体 1.在pipeline的scri ...
- py脚本 获取当前运行服务的相关信息
一.简介 最近在统计系统中都部署了什么服务,但服务器太多,在没有标准化之前进行整理,还是写脚本收集方便一些. 当然还是需要人工去判断整理表格,为后面标准化做准备.脚本是python2.7的,默认的ce ...
- useEffect无限调用问题
1.useEfect()的基本用法 const [test,setTest] = useState(1) const init=()=>{ setTest(2) } useEffect(()=& ...
- MySQL管理之道,性能调优,高可用与监控(第二版)pdf下载
MySQL管理之道,性能调优,高可用与监控(第二版) 书中内容以实战为导向,所有内容均来自于笔者多年实践经验的总结和新知识的拓展,同时也针对运维人员.DBA等相关工作者会遇到的有代表性的疑难问题给出了 ...
- IDEA 2021.2.1 破解版下载_激活安装图文教程(永久激活,亲测有效)
1.IntelliJ IDEA 2021 链接:https://pan.baidu.com/s/1Pwz3GrrkJdDZzg-wg5UjMw 提取码:56o6 无限重置 30 天试用期补丁 链接:h ...
- More Effective C++ 基础议题(条款1-4)总结
More Effective C++ 基础议题(条款1-4)总结 条款1:仔细区别pointers和references 如果有一个变量,其目的是用来指向(代表)另一个对象,但是也有可能它不指向(代表 ...
- List集合取其中指定几条数据
newList= oldList.subList(start, end); start,end分别是第几个到第几个,截取的内容包含前不包含结尾,用下标索引 此方法会改变原始list列表,返回的这个子列 ...
- JAVA里List集合中的对象根据对象的某个属性值降序或者升序排序
需要使用JDK1.8及以上 package com.stream; import java.util.Comparator; import java.util.List; public class T ...
- JAVA发送邮件报错,返回500 Error: bad syntax问题
把项目部署在Linux系统的centos服务器,发送邮件报错了. 500 Error: bad syntax 处理办法 [root@master bin]# hostname master 显示 ma ...
- c++之一个方便的日志库
概述 本文演示环境: win10 + vs2017 日志,我用的很少,通常是用作动态库调试使用. 日志记录下来,基本就没看过,除非模块出现了问题. 使用cmake管理的项目 使用C++封装了C语言读写 ...