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. Java高并发缓存架构,缓存雪崩、缓存穿透之谜

    面试题 了解什么是 redis 的雪崩.穿透和击穿?redis 崩溃之后会怎么样?系统该如何应对这种情况?如何处理 redis 的穿透? 面试官心理分析 其实这是问到缓存必问的,因为缓存雪崩和穿透,是 ...

  2. Centos7 Nginx开机启动

    1.简易安装nginx: ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf ...

  3. [Swift]LeetCode75. 颜色分类 | Sort Colors

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  4. [Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  5. iOS学习——(转)多线程

    转载自:iOS多线程全套:线程生命周期,多线程的四种解决方案,线程安全问题,GCD的使用,NSOperation的使用 一.多线程的基本概念 进程:可以理解成一个运行中的应用程序,是系统进行资源分配和 ...

  6. Linux中断程序命令

    在运行 python 脚本的时候想要中断程序,发现如下情况: ctrl+c 居然无法中断程序! 这时候尝试 ctrl+d 还是毫无效果,最后尝试 ctrl+\: 查看该程序是否还在运行 ps aux ...

  7. 【转】ret,retf,iret的区别

    ret RET, and its exact synonym RETN, pop IP or EIP from the stack and transfer control to the new ad ...

  8. qt deleterLater

    原文链接:浅谈 Qt 内存管理     Qt 内存管理是本文将要介绍的内容,在QT的程序中经常会看到只有new而不delete的情况,其实是因为QT有一套回收内存的机制,主要的规则如下: 1.所有继承 ...

  9. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  10. 带着新人学springboot的应用12(springboot+Dubbo+Zookeeper 下)

    上半节已经下载好了Zookeeper,以及新建了两个应用provider和consumer,这一节我们就结合dubbo来测试一下分布式可不可以用. 现在就来简单用一下,注意:这里只是涉及最简单的部分, ...