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和jquery获取属性的区别
一.获取元素: js获取元素: 根据id获取:document.getElementById("id"); 根据类名获取:document.getElementsByClassNa ...
- spider_keeper定时任务
# Define apscheduler app 下的 __init__.py 文件中, 三个主要函数
- Python——Django学习笔记
Django——一个封装好的神奇框架 若本文有任何内容错误,望各位大佬指出批评,并请直接联系作者修改,谢谢!小白学习不易. 一.简要模型 模型类操作数据表: python manage.py shel ...
- 前端cookie、localStorage、sessionStorage缓存技术总结
转载自:https://www.cnblogs.com/belove8013/p/8134067.html 1.Cookie JavaScript是运行在客户端的脚本,因此一般是不能够设置Sessio ...
- 【云计算】impala建表,文件关联,查询
[cloudil@hadoop164 caixianfeng]$ hdfs dfs -mkdir -p /csv-pig/tab1 /csv-pig/tab1 tab1.csv: 1,true,123 ...
- cuda9.0 中不存在libnppi.so
编译一个caffe版本,报错找不到 -lnppi 发现使用打是cuda9.0, 但是cuda9.0 中不存在libnppi.so. 只好换成cuda8.0.
- Oracle 多行、多列子查询
本文使用到的是oracle数据库scott方案所带的表,scott是oracle数据库自带的方案,使用前请确保其解锁 一.多行子查询 多行子查询子查询是嵌入在其他Sql语句中的select语句,Ora ...
- android volley 发送 POST 请求
Map<String, String> params = new HashMap<String, String>(); params.put("fromUser&qu ...
- 关于echar彩色柱状图颜色配置问题
在做配置页面的时候,有这样一个需求,用户可以选择配置是单色柱状图或者彩色的柱状图,并且颜色可以自定义的 思路:首先默认构建一个一个空的图表,然后根据记录上一次操作保存过来的参数重新组建option的值 ...
- unity鼠标拖动物体旋转
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> / ...