01、@Scheduled注解参数

  @Scheduled支持fixedRate、fixedDelay、cron表达式参数。其中,fixedRate和fixedDelay没有区别,都是启动时执行1次,每隔n毫秒执行。cron表达式相对灵活复杂,下文会详细讲述。

02、POM配置

<!--继承Spring Boot父工程-->
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.6.RELEASE</version>
</parent>
<dependencies>
  <!--依赖Spring Boot Starter-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
</dependencies>

03、开启Scheduled功能

@SpringBootApplication
@EnableScheduling //开启Schedule功能
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}

04、Scheduled Tasks Bean

@Component
public class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 3000) //启动时执行,然后每隔3s执行一次
public void reportCurrentTimeWithFixedRate() {
log.info("FixedRate:Current Time:{}", dateFormat.format(new Date()));
} @Scheduled(fixedDelay = 1000) //启动时执行,然后每隔1s执行一次
public void reportCurrentTimeWithFixedDelay() {
log.info("FixedDelay:Current Time:{}", dateFormat.format(new Date()));
} @Scheduled(cron = "*/2 * * * * *") //启动时不执行,然后每隔2s执行一次
public void reportCurrentTimeUsingCronExpression() {
log.info("Cron Expression:Current Time:{}", dateFormat.format(new Date()));
}
}

05、cron表达式详解

  cron表达式由空格分开的6部分组成

  

  例1:星期六和星期天,7月-8月,1 3 5号,每秒钟执行1次

  例2:星期一-星期五,每天9点0分0秒执行1次

  例3:23-7时,每秒执行1次

  例4:从第3秒开始,每2秒执行一次

001Spring 定时任务 Scheduled的更多相关文章

  1. spring 定时任务@Scheduled

    1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...

  2. spring boot 学习(八)定时任务 @Scheduled

    SpringBoot 定时任务 @Scheduled 前言 有时候,我们有这样的需求,需要在每天的某个固定时间或者每隔一段时间让应用去执行某一个任务.一般情况下,可以使用多线程来实现这个功能:在 Sp ...

  3. Spring 定时任务Scheduled 开发详细图文

    Spring 定时任务Scheduled 开发 文章目录 一.前言 1.1 定时任务 1.2 开发环境 1.3 技术实现 二.创建包含WEB.xml 的Maven 项目 2.1 创建多模块项目task ...

  4. 【spring boot】使用定时任务@Scheduled 报错:Encountered invalid @Scheduled method 'dealShelf': Cron expression must consist of 6 fields (found 7 in "0 30 14 * * ? *")

    在spring boot中使用使用定时任务@Scheduled 报错: org.springframework.beans.factory.BeanCreationException: Error c ...

  5. 【使用篇二】SpringBoot定时任务Scheduled(14)

    在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...

  6. Spring定时任务(@Scheduled)

    一.使用Spring的@Scheduled实现定时任务[1] 1.Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/s ...

  7. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  8. SpringBoot执行定时任务@Scheduled

    SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...

  9. spring源码分析之定时任务Scheduled注解

    1. @Scheduled 可以将一个方法标识为可定时执行的.但必须指明cron(),fixedDelay(),或者fixedRate()属性. 注解的方法必须是无输入参数并返回空类型void的. @ ...

随机推荐

  1. js 实现tab栏切换效果

    效果图: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  2. springboot设置日志级别时报错

    配置springboot日志,输出级别为info,运行时报错: Caused by: org.springframework.boot.context.properties.bind.BindExce ...

  3. Windows搭建Go语言环境·

    对于Windows用户,Go语言提供两种安装方式(源码安装除外): .MSI安装:程序会自动配置你的安装 .ZIP安装:需要你手动设置一些环境变量 一.MSI安装 1.下载安装包(根据操作系统选择相应 ...

  4. 求最大公因数(辗转相除法&更相减损术)

    求最大公因数(辗转相除法&更相减损术) 辗转相除法 又名欧几里得算法 ,其原理其实是基于这个定理:\(gcd(a,b)=gcd(b,a\%b)\),详细证明,而任何数与0的最大公约数是它本身 ...

  5. 解决kvm虚拟机启动之后,网卡eth0变为eth1问题

    2018-12-19 故障前提 kvm虚拟机迁移到其他服务器上之后,重新启动网卡会出现问题 例如原网卡名称为eth0,迁移重启之后会自动变为eth1 为什么eth0会变成eth1? 很多Linux d ...

  6. mssqlServer大量数据快速插入:SqlBulkCopy

    SqlBulkCopy类,为微软的一个大量数据快速插入.直接上代码 表结构: namespace SqlBulkCopy的演示 { using System.Data.SqlClient; publi ...

  7. yum的repo文件详解、以及epel简介、yum源的更换

    一.什么是repo文件        repo文件是Fedora中yum源(软件仓库)的配置文件,通常一个repo文件定义了一个或者多个软件仓库的细节内容,例如我们将从哪里下载需要安装或者升级的软件包 ...

  8. c# 闲谈异常处理

    今天在阅读 .net 源码时发现微软对所有使用枚举类型的地方对枚举值进行了检测,在检测不通过时抛出了异常. , )) { throw new InvalidEnumArgumentException( ...

  9. openerp学习笔记 按客户电话、名称模糊查找选择客户(name_search)及客户名称自定义显示(name_get)

    #同时按手机.电话.名称模糊查找选择客户    def name_search(self, cr, user, name, args=None, operator='ilike', context=N ...

  10. 用泛型T替代object做为万能参数传递

    using System;using System.Collections;using System.Collections.Generic;using UnityEngine; public cla ...