在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现

1、pom 包配置

pom 包里面只需要引入 Spring Boot Starter 包即可

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2、启动类启用定时

在启动类上面加上@EnableScheduling即可开启定时

@SpringBootApplication
@EnableScheduling
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

3、创建定时任务实现类

定时任务1:

@Component
public class SchedulerTask { private int count=0; @Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+(count++));
} }

定时任务2:

@Component
public class Scheduler2Task { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 6000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
} }

结果如下:

this is scheduler task runing  0
现在时间:09:44:17
this is scheduler task runing 1
现在时间:09:44:23
this is scheduler task runing 2
现在时间:09:44:29
this is scheduler task runing 3
现在时间:09:44:35

参数说明

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。

fixedRate 说明

  • @Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行
  • @Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按 fixedRate 的规则每6秒执行一次

文章内容已经升级到 Spring Boot 2.x

Spring Boot:定时任务的更多相关文章

  1. Spring Boot定时任务应用实践

    在Spring Boot中实现定时任务功能,可以通过Spring自带的定时任务调度,也可以通过集成经典开源组件Quartz实现任务调度. 一.Spring定时器 1.cron表达式方式 使用自带的定时 ...

  2. spring boot.定时任务问题记录(TaskScheduler/ScheduledExecutorService异常)

    一.背景 spring boot的定时任务非常简单,只需要在启动类中加上@EnableScheduling注解,然后在对应的方法上配置@Scheduled就可以了,系统会自动处理并按照Schedule ...

  3. (14)Spring Boot定时任务的使用【从零开始学Spring Boot】

    本文介绍在 Spring Boot 中如何使用定时任务,使用非常简单,就不做过多说明了. com.kfit.base.scheduling.SchedulingConfig: package com. ...

  4. Spring Boot 定时任务单线程和多线程

    Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...

  5. Spring Boot (十一): Spring Boot 定时任务

    在实际的项目开发工作中,我们经常会遇到需要做一些定时任务的工作,那么,在 Spring Boot 中是如何实现的呢? 1. 添加依赖 在 pom.xml 文件中只需引入 spring-boot-sta ...

  6. Spring Boot 定时任务 @Scheduled

    项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...

  7. Spring Boot定时任务运行一段时间后自动关闭的解决办法

    用Spring Boot默认支持的 Scheduler来运行定时任务,有时在服务器运行一段时间后会自动关闭.原因:Schedule默认是单线程运行定时任务的,即使是多个不同的定时任务,默认也是单线程运 ...

  8. Spring Boot 定时任务 Quartz 使用教程

    Quartz是一个完全由java编写的开源作业调度框架,他使用非常简单.本章主要讲解 Quartz在Spring Boot 中的使用. 快速集成 Quartz 介绍 Quartz 几个主要技术点 Qu ...

  9. spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现

    spring 定时任务 quartz 基于  MethodInvokingJobDetailFactoryBean 实现 依赖包 如下 <dependencies> <depende ...

  10. spring boot 定时任务

    定时任务实现方式 三种: 1) Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 最早的时候就是这样写定时任务的. 2) 开源的第三方框 ...

随机推荐

  1. 论文笔记【三】A Deep Dive into Word Sense Disambiguation with LSTM

    深入理解LSTM词义消歧 Minh Le,Marten Postma,Jacopo Urbani和Piek Vossen 阿姆斯特丹自由大学语言,文学和传播系 阿姆斯特丹自由大学计算机科学系 摘要 基 ...

  2. Echarts 柱状图配置详解

    1.基本柱状图 // 指定图表的配置项和数据 var option = { // ---- 标题 ----- title: { text: '主标题', textStyle: { color: 're ...

  3. 快速安装 Laravel 5.7

    $ git clone https://github.com/laravel/laravel.git $ cd laravel $ cp .env.example .env $ composer in ...

  4. open-falcon部署v0.2.1版本

    环境准备 安装redis yum install redis -y systemctl start redis 安装mysql rpm -ivh http://dev.mysql.com/get/my ...

  5. Python3 tkinter基础 Radiobutton 创建三个单选钮

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. win10 开机自启指定软件

    开机自启 %programdata%\Microsoft\Windows\Start Menu\Programs\StartUp

  7. [java]第一个程序

    HelloWorld 主函数 学习一门语言第一部分就是学习主函数,Java的主函数是main函数有如下形式: public class HelloWorld{ public static void m ...

  8. SAP 成套销售&按项目销售

    http://blog.sina.com.cn/s/blog_95ac31e30102x5we.html   分类: SAP_SD SAP 成套销售&按项目销售 一.业务简介 成套销售(KIT ...

  9. Nginx 504 Gateway Time-out分析及解决方法

    一.场景还原php程序在执行抓取远程图片库并保存至本地服务器的时候,出现了“504 Gateway Time-out”错误提示. 问题定位:由于图片巨多,所以下载时间很长(10分钟以上),引起网关超时 ...

  10. Mac连接非22端口linux服务器

    Mac连接非22端口linux服务器 原文链接:https://www.cnblogs.com/blog5277/p/9507080.html 原文作者:博客园--曲高终和寡 1:ssh连接 打开终端 ...