1.引入spring boot所依赖的jar包 


<parent>
    <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


2.使用 @Scheduled 标记需要调度的方法

package com.mouzhi.springboot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; @Component
public class ScheduledTasks {
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000)
public void reportCurrentTime(){
logger.info("The time is now {}", dateFormat.format(new Date()));
}
}


3.在项目启动项配置 @EnableScheduling 确保创建后台任务执行程序。没有它,没有任何计划。

package com.mouzhi.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}


4.@scheduled参数说明

  @Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;

@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;

@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;

@Scheduled(cron="* * * * * ?"):按cron规则执行。

  常用的cron表达式 

  0 10,14,16 * * ? 每天上午10点,下午2点,4点
  0/30 9-17 * * ? 朝九晚五工作时间内每半小时
  0 12 ? * WED 表示每个星期三中午12点
  "0 0 12 * * ?" 每天中午12点触发
  "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
  "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
  "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
  "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
  "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
  "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
  "0 15 10 15 * ?" 每月15日上午10:15触发
  "0 15 10 L * ?" 每月最后一日的上午10:15触发
  "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
  "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
  "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
  "0 15 10 ? * *" 每天上午10:15触发
  "0 15 10 * * ?" 每天上午10:15触发
  "0 15 10 * * ? *" 每天上午10:15触发
  "0 15 10 * * ? 2005" 2005年的每天上午10:15触发

  在线Cron表达式生成器:http://cron.qqe2.com/

spring boot 调度任务的更多相关文章

  1. Spring Boot 调度器

    Spring Boot 可以很简单的添加一个调度任务 首先需要添加maven依赖 <dependency> <groupId>org.springframework</g ...

  2. spring boot / cloud (十五) 分布式调度中心进阶

    spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...

  3. 使用Spring Boot搭建应用开发框架(一) —— 基础架构

    Spring的简史 第一阶段:XML配置,在Spring1.x时代,使用Spring开发满眼都是xml配置的Bean,随着项目的扩大,我们需要把xml配置文件分放到不同的配置文件里,那时候需要频繁的在 ...

  4. spring Boot+spring Cloud实现微服务详细教程第二篇

    上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...

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

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

  6. 详细介绍Spring Boot 2.0的那些新特性与增强

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

  7. Spring Boot 2.0 配置图文教程

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...

  8. Spring Boot Web 开发注解篇

    本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...

  9. spring boot(二):启动原理解析

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...

随机推荐

  1. 【mysql学习】InnoDB数据结构

    原来知道有一些索引失效的条件,最近看了看mysql底层数据结构,明白了为什么会失效 ,记录之.众所周知,常用的mysql数据引擎有两种,今天全是以InnoDB为基础开启探索之旅的,另一种有时间再说吧. ...

  2. Ubuntu 12.04 编译bcm93349dcm软件包

    1.准备工作操作系统:Ubuntu 12.04 获取bcm93349dcm软件包: bootloader源代码:Bootloader_2_2_0.zip CM源代码:ProdD20_BFC4.4.10 ...

  3. 关于使用MyEclipse自动生成Hibernate和Struts出现的jar不兼容的问题(antlr.collections.AST.getLine()I)

    今天做Hibernate和Struts2结合的练习,使用MyEclipse自动创建Hibernate和Struts的相关配置文件和jar包,写完一个查询方法后,准备测试一下结果,简单的查询那种,很诡异 ...

  4. [LC] 95. Unique Binary Search Trees II

    Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ...

  5. [2015普及组-D]推销员 思维que

    题:https://www.cometoj.com/problem/0221 #include<iostream> #include<cstring> #include< ...

  6. [Windows] Access SMBIOS

    SMBIOS architecture System Management BIOS (SMBIOS) is the premier standard for delivering managemen ...

  7. Java基础 带你深刻理解自动装箱,拆箱含义

    1.什么是装箱,什么是拆箱装箱:把基本数据类型转换为包装类.拆箱:把包装类转换为基本数据类型.基本数据类型所对应的包装类:int(几个字节4)- Integerbyte(1)- Byteshort(2 ...

  8. 4K时代,你不能不知道的HEVC

    最近追的美剧更新啦!但手机没连wifi,看视频心疼流量:画面不清晰,老是卡机:真是令人苦恼不已.别着急,或许在HEVC大范围普及之后,这一切烦恼都将不复存在了. HEVC是什么?它是High Effi ...

  9. Waymo

    技术优势 Waymo在自己的激光雷达系统上投入了大量资金,它认为这项技术对自动驾驶汽车的长期成功至关重要.实际上,该公司声称它已经将专有激光雷达传感器的成本降低了90%,这种传感器以前的制造成本为7. ...

  10. Cobalt Strike学习笔记

    Cobalt Strike 一款以metasploit为基础的GUI的框架式渗透测试工具,集成了端口转发.服务扫描,自动化溢出,多模式端口监听,win exe木马生成,win dll木马生成,java ...