SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它。

开启定时任务

@SpringBootApplication
//设置扫描的组件的包
@ComponentScan(basePackages = {"com.tao"})
//开启定时器任务
@EnableScheduling
public class MybatisPlusApplication { public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}

配置定时任务组件

使用注解@Component可以定义一个组件,组件中,我们可以按照以下代码开始定时任务。

Cron 表达式可以使用:http://cron.qqe2.com/ 来生成,非常的方便


@Component
public class TaskScheduled { private static final SimpleDateFormat sm = new SimpleDateFormat("HH:mm:ss"); /**
* 开始时间表达式,从第三秒开始每隔10s执行一次
*/
@Scheduled(cron = "3/10 * * * * ? ")
public void inputTime(){
System.out.println("当前系统时间:"+sm.format(System.currentTimeMillis()));
} /**
* 时间间隔,每隔5秒执行一次
*/
@Scheduled(fixedRate = 5000)
public void inputTime2(){
System.out.println("当前系统时间:"+sm.format(System.currentTimeMillis()));
} }

SpringBoot 配置定时任务的更多相关文章

  1. SpringBoot配置定时任务的两种方式

    一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  2. springboot配置定时任务并发执行

    @Configuration public class ScheduleConfig implements SchedulingConfigurer { @Override public void c ...

  3. 在SpringBoot中配置定时任务

    前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...

  4. springboot整合Quartz实现动态配置定时任务

    前言 在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面就通过一个简单的示例,来实现这个功能. 一.新建一个springboot工程,并添加依赖 & ...

  5. 玩转SpringBoot之定时任务详解

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  6. SpringBoot整合定时任务和异步任务处理 3节课

    1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类        ...

  7. 十三、springboot集成定时任务(Scheduling Tasks)

    定时任务(Scheduling Tasks) 在springboot创建定时任务比较简单,只需2步: 1.在程序的入口加上@EnableScheduling注解. 2.在定时方法上加@Schedule ...

  8. SpringBoot创建定时任务

    之前总结过spring+quartz实现定时任务的整合http://www.cnblogs.com/gdpuzxs/p/6663725.html,而springboot创建定时任务则是相当简单. (1 ...

  9. SpringBoot整合定时任务和异步任务处理

    SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...

随机推荐

  1. 请求ajax失败的原因(进入到error)

    原因: dataType 定义类型和返回类型不一致,我定义的json格式数据. {data:[],num:0} 这种是不规则的字符串,不是严格的json格式 应该改成{"data" ...

  2. [Swift]LeetCode661. 图片平滑器 | Image Smoother

    Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother t ...

  3. [Swift]LeetCode699. 掉落的方块 | Falling Squares

    On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...

  4. [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  5. [Swift]LeetCode913.猫与老鼠 | Cat and Mouse

    A game on an undirected graph is played by two players, Mouse and Cat, who alternate turns. The grap ...

  6. linux入门--类UNIX系统详解

    有人说,这个世界上只有两种操作系统: UNIX 和类 UNIX 操作系统: 其它操作系统. 类 UNIX 系统(英文 Unix-like)既包括各种传统的 UNIX 系统,比如 FreeBSD.Ope ...

  7. RSA算法原理——(3)RSA加解密过程及公式论证

    上期(RSA简介及基础数论知识)为大家介绍了:互质.欧拉函数.欧拉定理.模反元素 这四个数论的知识点,而这四个知识点是理解RSA加密算法的基石,忘了的同学可以快速的回顾一遍. 一.目前常见加密算法简介 ...

  8. HBase篇--HBase操作Api和Java操作Hbase相关Api

    一.前述. Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下. 二.说明 Hbase shell中删除键是空格+Ctrl键. 三.代码 1.封装所有的API pa ...

  9. 让 CDN 更省流量的 Brotli 算法详解

    早年,我还是学生的时候,时常会鼓捣自己的个人网站,其中最困扰我的问题就是源站服务器易崩溃.作为学生,一方面我没有足够的钱购买高质量的服务器,另一方面一年的流量费用算下来也挺贵的,要花掉我不少的生活费. ...

  10. Javascript sort方法

    sort()方法用于对数组的元素进行排序 语法:array.Object.sort(sortBy) sortBy:可选.规定排序顺序.必须是函数 返回值:对数组的引用.数组在原数组上进行排序,不生成副 ...