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. [Bash]LeetCode194. 转置文件 | Transpose File

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  2. [Swift]LeetCode677. 键值映射 | Map Sum Pairs

    Implement a MapSum class with insert, and sum methods. For the method insert, you'll be given a pair ...

  3. app自动化问题点整理

    1.配置SDK环境发现adb无法生效: 解决方案: 这个问题一般就是你的环境变量没有配置好,很多同学比较容易出现的点就是win10系统的环境变量配置: 一定要注意打开path添加:%ANDROID_H ...

  4. java中 try catch finally和return联合使用时,代码执行顺序的小细节

    代码1测试 public static void main(String[] args) { aa(); } static int aa() { try { int a=4/0; } catch (E ...

  5. Spring介绍

    Spring介绍 Spring的核心是一个轻量级(Lightweight)的容器(Container),它是实现IoC(Inversion of Control)容器和非入侵性(No intrusiv ...

  6. 单机部署redis5.0集群环境

    #安装rediscd redis-5.0.0makemake install #部署集群mkdir redis_clustermkdir -p redis_cluster/{7000,7001,700 ...

  7. SQLplus下命令输入错误如何删掉重新输入

    如下当我们输入命令出错时想删掉重新输入,按delete或者backspace键无法删除且出现如下情况: SQL> show urerej  ^H^H^H^H^H 可以是使用ctrl+backsp ...

  8. 『Pushing Boxes 双重bfs』

    Pushing Boxes Description Imagine you are standing inside a two-dimensional maze composed of square ...

  9. 华为oj之等差数列前n项和

    题目: 等差数列 热度指数:1010 时间限制:1秒 空间限制:32768K 题目描述 功能: 对于等差数列 2,5,8,11,14- 输入: 正整数N >0 输出: 求等差数列前N项和 返回: ...

  10. [Leetcode]100. Same Tree -David_Lin

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...