spring boot: 计划任务@ EnableScheduling和@Scheduled
spring boot: 计划任务@ EnableScheduling和@Scheduled
@Scheduled中的参数说明
@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行; @Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行; @Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行; @Scheduled(cron="* * * * * ?"):按cron规则执行。
常用Cron表达式( 秒/分/时/日/月/周/年 )
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 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触发
实例:
service
package ch2.scheduler; //时间处理,时间格式化
import java.util.Date;
import java.text.SimpleDateFormat; //spring计划任务声明(针对方法声明)
import org.springframework.scheduling.annotation.Scheduled;
//spring组件声明
import org.springframework.stereotype.Service; //组件什么
@Service
public class SchedulerService { //创建日期模板
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH::MM::ss"); //每5秒钟执行一次
@Scheduled(fixedRate = )
public void reportCurrentTime()
{
System.out.println("每五秒执行一次: " + dateFormat.format( new Date() ));
} //按照cron属性指定执行时间:秒分时
@Scheduled(cron = "0 34 18 ? * *")
public void fixTimeExecution()
{
System.out.println("在指定的时间内执行: " + dateFormat.format( new Date()) );
} }
config
package ch2.scheduler; //自动引入包
import org.springframework.context.annotation.ComponentScan;
//配置类声明
import org.springframework.context.annotation.Configuration; //计划任务类声明
import org.springframework.scheduling.annotation.EnableScheduling; //配置类声明
@Configuration
//自动引入包
@ComponentScan("ch2.scheduler")
//通过@EnableScheduling开启对计划任务的支持
@EnableScheduling
public class TaskSchedulerConfig { }
main
package ch2.scheduler;
//引入容器
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args)
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TaskSchedulerConfig.class);
//SchedulerService schedulerService = context.getBean(SchedulerService.class); }
}
spring boot: 计划任务@ EnableScheduling和@Scheduled的更多相关文章
- Spring Boot 计划任务中的一个“坑”
计划任务功能在应用程序及其常见,使用Spring Boot的@Scheduled 注解可以很方便的定义一个计划任务.然而在实际开发过程当中还应该注意它的计划任务默认是放在容量为1个线程的线程池中执行, ...
- Spring Boot入门(三):使用Scheduled注解实现定时任务
在程序开发的过程中,经常会使用定时任务来实现一些功能,比如: 系统依赖于外部系统的非核心数据,可以定时同步 系统内部一些非核心数据的统计计算,可以定时计算 系统内部的一些接口,需要间隔几分钟或者几秒执 ...
- Spring boot @EnableScheduling 和 @Scheduled 注解使用例子
前言 Spring Boot提供了@EnableScheduling和@Scheduled注解,用于支持定时任务的执行,那么接下来就让我们学习下如何使用吧: 假设我们需要每隔10秒执行一个任务,那么我 ...
- 【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 ...
- spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明
spring boot: @EnableScheduling开启计划任务支持, @Scheduled计划任务声明 package ch2.scheduler2; //日期转换方式 import jav ...
- Spring boot中的定时任务(计划任务)
从Spring3.1开始,计划任务在Spring中实现变得异常的简单.首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled ...
- spring boot注解之@Scheduled定时任务实现
java实现定时任务一般使用timer,或者使用quartz组件.现在在spring boot提供了更加方便的实现方式. spring boot已经集成了定时任务.使用@Secheduled注解. @ ...
- spring boot @Scheduled未生效原因以及相关坑、及相对其他定时任务架构的优势
在spring boot中,支持多种定时执行模式(cron, fixRate, fixDelay),在Application或者其他Autoconfig上增加@EnableScheduling注解开启 ...
- Spring Boot中使用@Scheduled创建定时任务
我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时 ...
随机推荐
- 让vs只启动自己想调试的站点
VS中里面多个WEB项目如何只启动一个? 每次启动时,右下角都会出现一堆的 网站有10来个.即使设置了默认启动项目, 但每次按F5启动,或者哪怕是在项目上右键启动新实例 右下角都会出现这一堆的站点 有 ...
- dbUtils 工具类介绍
导包: commons-dbutils.jar 核心类: QueryRunner 常用方法: // 执行增,删,改语句, 返回影响的行数 int update(String sql,Object... ...
- qt sql 模块有哪些类?
Class Description translate.google QSqlDatabase Handles a connection to a database 处理与数据库的连接 QSqlDri ...
- 批处理 ECHO命令输出空行
众所周知,如果echo后面跟一个环境变量,但是该变量却为空时,相当于不加任何参数的echo,即输出当前echo是on还是off.很多文章或者教程给出的解决方案都是在echo后面加一个点号echo.,这 ...
- PyQt4 调用串口API pySerial API说明
pySerial API官方介绍链接 http://pyserial.readthedocs.io/en/latest/pyserial_api.html
- 爬虫五 Beautifulsoup模块
一 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你 ...
- Django——缓存机制
1.缓存介绍 (1)概论 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次的的后台 ...
- 静态库引入引起的错误解决方案,ld: warning: ignoring file ”…/XXX.a”, file was built for archive which is not the architecture being linked (armv7): “…/XXX.a” Undefined symbols for architecture armv7: "_OBJC_CLASS_$
想目中不免会引入一些静态库,可是有时加入'.a'文件后编译便会报以下错误 ld: warning: ignoring file ”…/XXX.a”, file was built for archiv ...
- 【leetcode刷题笔记】Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 0731 #Django rest framework
FBV:Function base viewsdef index(request): if request.method == 'POST': return HTTPrespons ...