springboot与任务(定时任务)
描述:
项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor 、TaskScheduler 接口。
两个注解:
@EnableScheduling、@Scheduled
cron表达式:
写法:
主类: @EnableScheduling//开启基于注解的定时任务
@SpringBootApplication
public class Springboot04TaskApplication { public static void main(String[] args) {
SpringApplication.run(Springboot04TaskApplication.class, args);
} } service:
@Service
public class ScheduledService { /*
* second(秒),minute(分),hour(时),day of month(日),month(月),day of week(周)
* 0 * * * * MON-FRI 周一到周五每一分钟都启动一次
* * * * * * MON-FRI 周一到周五每一秒钟都启动一次
* 【0 0/5 14 18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
* 【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
* 【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
* 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
* 【0 0 2-4 ? * 1#1】 每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次
*
* */
// @Scheduled(cron = "0 * * * * MON-FRI") 周一到周五每一分钟都启动一次
//@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT") 枚举,每分钟的0 , 1 ,2 , 3 ,4 秒都执行
//@Scheduled(cron = "0-4 * * * * MON-SAT") 区间 , 每分钟的0 , 1 ,2 , 3 ,4 秒都执行
//@Scheduled(cron = "0/4 * * * * MON-SAT") 步长,从0秒启动,每4秒启动一次
public void hello(){
System.out.println("hello ... ");
}
}
springboot与任务(定时任务)的更多相关文章
- SpringBoot几种定时任务的实现方式
定时任务实现的几种方式: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行, ...
- springBoot中的定时任务
springBoot中的定时任务 1:在Spring Boot的主类中加入@EnableScheduling注解,启用定时任务的配置 2:新建ScheduledTasks任务类 : package c ...
- SpringBoot中的定时任务与Quartz的整合
SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...
- SpringBoot中执行定时任务
一:在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. @SpringBootApplication @EnableSchedu ...
- springboot整合@Scheduled定时任务的使用
1.启动类里面添加注解@EnableScheduling ,例如: @SpringBootApplication@EnableScheduling@MapperScan("com.examp ...
- SpringBoot系列——动态定时任务
前言 定时器是我们项目中经常会用到的,SpringBoot使用@Scheduled注解可以快速启用一个简单的定时器(详情请看我们之前的博客<SpringBoot系列--定时器>),然而这种 ...
- Springboot的默认定时任务——Scheduled注解
本博客参考博文地址. 1.pom依赖: 引入springboot starter包即可 <dependencies> <dependency> <groupId>o ...
- 在SpringBoot中配置定时任务
前言 之前在spring中使用过定时任务,使用注解的方式配置很方便,在SpringBoot中的配置基本相同,只是原来在spring中的xml文件的一些配置需要改变,在SpringBoot中也非常简单. ...
- springBoot中使用定时任务
简单示例 导入依赖 springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务. <parent> <groupId>org.springfram ...
- SpringBoot中并发定时任务的实现、动态定时任务的实现(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10659045.html,否则将追究法律责任!!! 一.在JAVA开发领域,目前可以通过以下 ...
随机推荐
- ps-手捧城堡滴水云雾图
1打开背景图 置入第二张图片 栅格化-加入蒙版-渐变 置入第三张图片 栅格化-用快速选择工具选取-加入蒙版 置入第四张图片 栅格化-调整图层-点击城堡建立蒙版-点击手的蒙版 ctrl-点击城堡的蒙版- ...
- maven中报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0
在检索完工程后报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0. 由于oracle的ojdbc收费,所以在maven项目导入时没有ojdbc14 ...
- 2019-7-29-asp-dotnet-core-从-Frp-获取用户真实-IP-地址
title author date CreateTime categories asp dotnet core 从 Frp 获取用户真实 IP 地址 lindexi 2019-07-29 08:28: ...
- JS对象随机数 random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。 注意:返回一个大于或等于 0但小于1的符号为正的数值
随机数 random() random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数. 语法: Math.random(); 注意:返回一个大于或等于 0 但小于 1 ...
- JS对象 返回/设置年份方法 get/setFullYear() 返回/设置年份,用四位数表示。.顺序格式依次为:星期、月、日、年、时、分、秒、时区。(火狐浏览器)
返回/设置年份方法 get/setFullYear() 返回/设置年份,用四位数表示. var mydate=new Date();//当前时间2014年3月6日 document.write(myd ...
- 快速创建vuepress项目(使用vuepress写文档)
vuepress的官方文档:https://vuepress.vuejs.org/zh/guide/ 参考:https://segmentfault.com/a/1190000016333850 ht ...
- css---flex布局--容器
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool 容器设置 新版的为display为flex ...
- mysql出现You can’t specify target table for update in FROM clause
在mysql执行下面语句时报错: You can’t specify target table for update in FROM clause UPDATE edu_grade_hgm_1 ' W ...
- mysql 的特殊函数
1. FORMAT() 函数 函数用于对字段的显示进行格式化 语法: SELECT FORMAT(column_name,format) FROM table_name column_name 必需 ...
- JSON 单例类
MD5JSON.h #pragma once #include "include/json/json.h" #include "include/md5/md5.h&quo ...