001Spring 定时任务 Scheduled
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的更多相关文章
- spring 定时任务@Scheduled
1.配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:/ ...
- spring boot 学习(八)定时任务 @Scheduled
SpringBoot 定时任务 @Scheduled 前言 有时候,我们有这样的需求,需要在每天的某个固定时间或者每隔一段时间让应用去执行某一个任务.一般情况下,可以使用多线程来实现这个功能:在 Sp ...
- Spring 定时任务Scheduled 开发详细图文
Spring 定时任务Scheduled 开发 文章目录 一.前言 1.1 定时任务 1.2 开发环境 1.3 技术实现 二.创建包含WEB.xml 的Maven 项目 2.1 创建多模块项目task ...
- 【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 ...
- 【使用篇二】SpringBoot定时任务Scheduled(14)
在日常项目运行中,我们总会有需求在某一时间段周期性的执行某个动作.比如每天在某个时间段导出报表,或者每隔多久统计一次现在在线的用户量.在springboot中可以有很多方案去帮我们完成定时器的工作,有 ...
- Spring定时任务(@Scheduled)
一.使用Spring的@Scheduled实现定时任务[1] 1.Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/s ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot执行定时任务@Scheduled
SpringBoot执行定时任务@Scheduled 在做项目时,需要一个定时任务来接收数据存入数据库,后端再写一个接口来提供该该数据的最新的那一条. 数据保持最新:设计字段sign的值(0,1)来设 ...
- spring源码分析之定时任务Scheduled注解
1. @Scheduled 可以将一个方法标识为可定时执行的.但必须指明cron(),fixedDelay(),或者fixedRate()属性. 注解的方法必须是无输入参数并返回空类型void的. @ ...
随机推荐
- js控制输入框只能输入数字不能输入其他字符
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- yolov3源码分析keras(二)损失函数计算
一.前言 损失函数计算主要分析两部分一部分是yolo_head函数的分析另一部分为ignore_mask的生成的分析. 二.重要细节分析 2.1损失函数计算具体代码及部分分析 def yolo_los ...
- appium +android例子
配置文件: # coding:utf-8 __author__ = 'Helen' """ description:配置全局参数 """ i ...
- 大数据平台-java、mysql安装
补充: 对于ssh登录不是特定端口22的,进行文件修改 vim /etc/ssh/sshd_config Port 61333 简化后序命令输入,修改文件如下: 一.java环境安装 一共5台服务器 ...
- 移动端尺寸新写法-rem
rem这是个低调的css单位,近一两年开始崭露头角,有许多同学对rem的评价不一,有的在尝试使用,有的在使用过程中遇到坑就弃用了.但是我对rem综合评价是用来做web app它绝对是最合适的人选之一. ...
- Sum(欧拉降幂+快速幂)
Input 2 Output 2 Hint 1. For N = 2, S(1) = S(2) = 1. 2. The input file consists of multiple test cas ...
- 通过遍历类向Aspose.cell模板中插入数据
/// <summary> /// 遍历类所有字段 /// </summary> /// <param name="designer">aspo ...
- 深入redis内部--内存管理
1. Redis内存管理通过在zmalloc.h和zmalloc.c中重写c语言对内存的管理来完成的. redis内存管理 c内存管理 原型 作用 zmalloc malloc void *mallo ...
- Linux中常用头文件的作用--转
http://blog.sina.com.cn/s/blog_5c93b2ab0100q62k.html 1. Linux中一些头文件的作用: <assert.h>:ANSI C.提供断言 ...
- js 获取 客户区 大小
js 获取 客户区 大小 本文内容来自<javascript高级程序设计(第二版)> 内容, 只是方便大家以后可能会用到... <script type="text/jav ...