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的定时任务实现(非分布式)的更多相关文章

  1. 基于SpringBoot实现定时任务的设置(常用:定时清理数据库)

    1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...

  2. 分布式 redis 延时任务 基于 springboot 示例

    Lilishop 技术栈 官方公众号 & 开源不易,如有帮助请点Star 介绍 官网:https://pickmall.cn Lilishop 是一款Java开发,基于SpringBoot研发 ...

  3. 基于SpringBoot AOP面向切面编程实现Redis分布式锁

    基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式锁 基于SpringBoot AOP面向切面编程实现Redis分布式 ...

  4. 基于SpringBoot+SSM实现的Dota2资料库智能管理平台

    Dota2资料库智能管理平台的设计与实现 摘    要 当今社会,游戏产业蓬勃发展,如PC端的绝地求生.坦克世界.英雄联盟,再到移动端的王者荣耀.荒野行动的火爆.都离不开科学的游戏管理系统,游戏管理系 ...

  5. 【spring boot】【redis】spring boot基于redis的LUA脚本 实现分布式锁

    spring boot基于redis的LUA脚本 实现分布式锁[都是基于redis单点下] 一.spring boot 1.5.X 基于redis 的 lua脚本实现分布式锁 1.pom.xml &l ...

  6. springboot实现定时任务的方式

    springboot实现定时任务的方式 a   Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程 ...

  7. SpringBoot执行定时任务@Scheduled

    SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...

  8. MyBatis 进阶,MyBatis-Plus!(基于 Springboot 演示)

    这一篇从一个入门的基本体验介绍,再到对于 CRUD 的一个详细介绍,在介绍过程中将涉及到的一些问题,例如逐渐策略,自动填充,乐观锁等内容说了一下,只选了一些重要的内容,还有一些没提及到,具体可以参考官 ...

  9. 搭建基于springboot轻量级读写分离开发框架

    何为读写分离 读写分离是指对资源的修改和读取进行分离,能解决很多数据库瓶颈,以及代码混乱难以维护等相关的问题,使系统有更好的扩展性,维护性和可用性. 一般会分三个步骤来实现: 一. 主从数据库搭建 信 ...

随机推荐

  1. mysql的MVCC多版本并发控制机制

    MVCC多版本并发控制机制 全英文名:Multi-Version Concurrency Control MVCC不会通过加锁互斥来保证隔离性,避免频繁的加锁互斥. 而在串行化隔离级别为了保证较高的隔 ...

  2. SROP例题

    具体攻击原理可以参考安全客这篇文章:入口 刚学了一点,也是懵懵懂懂的,拿几道题来练练手. ciscn_2019_es_7 64位程序,只开启了NX保护. 相当于执行了read(0,buf,0x400) ...

  3. Docker 快速删除无用(none)镜像

    Dockerfile 代码更新频繁,自然docker build构建同名镜像也频繁的很,产生了众多名为none的无用镜像. 分别执行以下三行可清除 docker ps -a | grep " ...

  4. [源码解析] PyTorch 分布式之弹性训练(1) --- 总体思路

    [源码解析] PyTorch 分布式之弹性训练(1) --- 总体思路 目录 [源码解析] PyTorch 分布式之弹性训练(1) --- 总体思路 0x00 摘要 0x01 痛点 0x02 难点 0 ...

  5. CF173A Rock-Paper-Scissors 题解

    Content 有 \(2\) 个人在玩石头剪刀布,已知他们的出手都有一定的规律,求 \(n\) 局之后两个人各输了几局. 数据范围:\(1\leqslant n\leqslant 2\times 1 ...

  6. HTML5 head标签meta标签、title的功能

    <!DOCTYPE html> <!-- 解释器--> <html lang="en"> <head> <!--meta标签中 ...

  7. SQL Server日志恢复还原数据

    通过日志还原,首先要注意的是: 1,在数据库更新和删除之前有一个完整的备份. 2,在更新和删除之后,做一个日志备份. 3,该日志只能用于还原数据库备份和日志备份时间之间的数据. 下面看整个数据库备份和 ...

  8. C语言之可变长参数格式化

    概述 本文演示环境: win10 + Vs2015 可变长参数格式化 两个概念: 1. 参数长度不定, 2. 参数格式化. 使用函数 vsnprintf 结合 va_list. 源码 写好了函数, 照 ...

  9. nim_duilib(2)之xml目录结构理解

    introduction 本文将总结我对nim_duilib的xml配置. 更多控件和控件属性的具体说明, 请参考 here before starting 1 You should clone th ...

  10. 51Nod 1279:扔盘子(二分||单调栈)

    1279 扔盘子 1.0 秒 131,072.0 KB 5 分 1级题 有一口井,井的高度为N,每隔1个单位它的宽度有变化.现在从井口往下面扔圆盘,如果圆盘的宽度大于井在某个高度的宽度,则圆盘被卡住( ...