基于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轻量级读写分离开发框架
何为读写分离 读写分离是指对资源的修改和读取进行分离,能解决很多数据库瓶颈,以及代码混乱难以维护等相关的问题,使系统有更好的扩展性,维护性和可用性. 一般会分三个步骤来实现: 一. 主从数据库搭建 信 ...
随机推荐
- py脚本 获取当前运行服务的相关信息
一.简介 最近在统计系统中都部署了什么服务,但服务器太多,在没有标准化之前进行整理,还是写脚本收集方便一些. 当然还是需要人工去判断整理表格,为后面标准化做准备.脚本是python2.7的,默认的ce ...
- 微信小程序项目使用npm安装vant-weapp的正确步骤,简单易懂!!
微信小程序项目使用npm安装vant-weapp的正确步骤 1.在当前小程序项目目录npm init -y 构建npm项目 2.运行命令 npm install vant-weapp -S --pro ...
- Table.PromoteHeaders升降标题Table…Headers(Power Query 之 M 语言)
数据源: 任意数据源 目标: 将第一行提升为标题 操作过程: [主页](或[转换])>[将第一行用作标题] M公式: = Table.PromoteHeaders( 表, [PromoteAll ...
- 小迪安全 Web安全 基础入门 - 第四天 - 30余种加密编码进制&Web&数据库&系统&代码&参数值
一.密码存储加密 1.MD5值是32或16位由数字"0-9"和字母"a-f"所组成的字符串 2.SHA1加密的密文特征与MD5类似,但位数是40位 3.NTLM ...
- 40张图+万字,从9个数据类型帮你稳稳的拿捏Redis数据结构
摘要:本文把Redis新旧版本的数据结构说图解一遍,共有 9 种数据结构:SDS.双向链表.压缩列表.哈希表.跳表.整数集合.quicklist.listpack. 本文分享自华为云社区<为了拿 ...
- LuoguP2108 学英语 题解
Content 给出整数 \(x\) 的英文写法,求出这个整数 \(x\). 规则详见题面. 数据范围:\(|x|\leqslant 999999999\)(\(9\) 个 \(9\)). Solut ...
- ACwing1212. 地宫取宝
题目: X 国王有一个地宫宝库,是 n×m 个格子的矩阵,每个格子放一件宝贝,每个宝贝贴着价值标签. 地宫的入口在左上角,出口在右下角. 小明被带到地宫的入口,国王要求他只能向右或向下行走. 走过某个 ...
- ACwing1211. 蚂蚁感冒
题目: 长 100 厘米的细长直杆子上有 n 只蚂蚁. 它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是 1 厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁 ...
- 【LeetCode】949. Largest Time for Given Digits 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Codeforces 450E:Jzzhu and Apples(构造,数学)
E. Jzzhu and Apples time limit per test: 1 seconds memory limit per test: 256 megabytes input: stand ...