springboot实现定时任务的一种方式
参考:https://www.yoodb.com/news/detail/1205
目前觉得这种还是很好用,所以就记录。
最好新建一个项目测试:
1. pom中添加
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-task-core</artifactId>
</dependency>
2. 启动类添加注释:@EnableScheduling
3. 配置
server:
port: 55550 #日志
logging:
file: ./logs/apple.log spring:
application:
name: asfood-apple eureka:
client:
serviceUrl:
defaultZone: http://localhost:55580/eureka/ #详情[parent/asfood]/doc_nn/cron.txt
tasktime_cron: 0/20 * 14,15 * * ?
4. 新建类
@Configuration
public class TaskTest {
private static int i = 0; /**
cron:指定cron表达式。
fixedDelay:
官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task.
The time unit value is measured in milliseconds.
即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
fixedRate:
官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task.
The time unit value is measured in milliseconds.
即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
*/
@Scheduled(cron="${tasktime_cron}")
// @Scheduled(fixedRate=2000) // 每5分钟执行一次 5*60*1000
// @Scheduled(fixedRate=1000) // 每1s
public void dual() {
System.out.println(i++);
}
}
运行,试试? 哈哈哈,估计可以成功,里面没什么多的东西,至于cron表达式可以百度学习。
springboot实现定时任务的一种方式的更多相关文章
- SpringBoot配置定时任务的两种方式
一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- springboot实现定时任务的两种方式
方式一:在springboot启动类上添加@EnableScheduling注解,然后创建具体的任务类,在方法上添加@Scheduled注解,并指明执行频率即可.如下: @Componentpubli ...
- SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...
- springboot成神之——mybatis在spring-boot中使用的几种方式
本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- 【SpringBoot】05.SpringBoot整合Listener的两种方式
SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...
- 【SpringBoot】03.SpringBoot整合Servlet的两种方式
SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...
- Springboot下载Excel的3种方式
Springboot下载Excel的3种方式 汇总一下浏览器下载和代码本地下载实现的3种方式. (其实一般都是在代码生成excel,然后上传到oss,然后传链接给前台,但是我好像没有实现过直接点击就能 ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
随机推荐
- 发现一个小坑的地方,unity的协程,想要停止,必须以字符串启动
今天想要停止一个协成,发现调用 StopCoroutine(ShowDebug()); 竟然不管用,后来看了文档才知道,原来想要停止协成,必须用字符启动协程 StartCoroutine(" ...
- 定时任务 Linux cron job 初步使用
查看定时任务的命令为:crontab -l 编辑定时任务的命令为:crontab -e (编辑后立即生效 若注释可在行首加# 同vi) 定时任务说明 每一行为一 ...
- Elasticsearch6.4.3安装
Linux内存一定要1g以上! 首先要有jdk环境 要求1.8版本以上 elasticsearch是Java写的 将上传的 elasticSearch安装包解压 cd /home/elastics ...
- springcloud-声明式调用服务Feign
springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir 作用: 基于Netflix Feign整合了Rib ...
- Difference between stem and lemma
lemma与stem的区别 Difference between stem and lemma 先从wikipedia上看看什么是stem,什么是lemma? Lemma(morphology):In ...
- 汇编笔记 CALL(1)
assume cs:code code segment start: mov ax, ;将AX通用寄存器设0 call s inc ax s: pop ax ;将数据从桟中取出 code ends e ...
- JMeter 3.0 POST Body Data 中文乱码问题
现象如下图所示:
- Python DB API 连接数据库
Python DB API Mysql,Oracle,SqlServer 不关闭,会浪费资源.
- JavaScrip 原生多文件上传及预览 兼容多浏览器
JavaScrip 原生多文件上传及预览 兼容多浏览器 html代码块 <div class="container"> <label>请选择一个图像文件:& ...
- J2EE配置tomcat