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. todocmvc的安装

    安装依赖 官网 安装依赖的css,js $npm install 引入vue <script src="js/vue.js"></script> 定义初始化 ...

  2. css第二篇:样式的特殊性、重要性、继承和层叠

    特殊性:   假设有几个不同的规则改变的都是同一个元素的值,那么哪一个规则将会胜出呢?这就得靠特殊值啦,什么是特殊值呢?特殊值的大小呢?如下图: 值越大代表越牛,如1,0,0,0永远大于0,X,X,X ...

  3. 让windows登陆界面显示administrator账户

    如果windowsXP只有一个administrator账户,在开机登陆windows的欢迎界面,会出现这个账户名,点击,输入密码就可登陆到windows桌面: 如果新建了另一个管理员账户,在欢迎界面 ...

  4. [转] Jenkins pipeline 中获取 exit code, stdout and stderr 返回值和输出

    [From] https://issues.jenkins-ci.org/browse/JENKINS-44930 其做法是,把stdout定向到一个文件,sh 配置 returnStatus: tr ...

  5. js定时器执行

    第一种:问题请求代表执行打印出来的是什么? //定时器执行页面崩溃 var bo = true; setTimeout(function () { console.log("定时器执行&qu ...

  6. pycharm入门的简易使用教程

    1.安装python3.5包: 本人安装到C盘,如下所示: Ps:安装时候,路径一定要添加到环境变量中,安装完,需要更新环境变量,所以要重启电脑 2.安装pycharm 下载相应的安装包,步骤不重复了 ...

  7. JS框架设计之加载器所在路径的探知一模块加载系统

    1.要加载一个模块,我们需要一个URL作为加载地址,一个script作为加载媒介,但用户在require是都用ID,我们需要一个将ID转换为URL的方法,思路很简单,强加个约定,URL的合成规则是为: ...

  8. IDEA里运行代码时出现Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger的解决办法(图文详解)

    不多说,直接上干货! 问题详情 运行出现log4j的问题 -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib\charsets.jar ...

  9. p2p的UDP打洞原理

    >>>>>>>>>>>>>>>>>>>>>>>>> ...

  10. 《JavaScript语言精粹》读书笔记

    第三章:对象 //1.定义一个方法 method Function .prototype.method=function(name, func){ this.prototype[name]=func; ...