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 ...
随机推荐
- 20145230《java程序设计》第6周学习总结
20145230 <Java程序设计>第6周学习总结 教材学习内容 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出,可以 ...
- dll和lib
lib:里面包含了很多源代码,工程会将这些源代码加入自己的项目中编译: dll:动态编译库,允许可执行文件在运行中加载里面的资源. 使用lib需注意两个文件:(1).h头文件,包含lib中说明输出的类 ...
- 机器学习-chapter1机器学习的生态系统
1.机器学习工作流程 获取->检查探索->清理准备->建模->评估->部署 2.搭建机器学习环境 1..通过安装Python,配置相关环境变量 2.强烈建议直接安装ana ...
- java深入探究12-框架之Structs
注意每次修改配置文件后必须项目重启 Structs2=structs1+xwork Struct2框架预先实现了一些功能: 1.请求数据的封装:2.文件上传的功能3.对国际化功能的简化4.文件效验功能 ...
- 中文乱码问题(页面乱码,eclipse乱码,请求响应乱码)
1.首先在开发工具eclipse中设置工作空间和文件编码格式,详情参见 http://www.cnblogs.com/lixiang1993/p/7345161.html 2.在eclipse的安 ...
- MVC 控件系列
下拉框:@Html.DropDownList("GroupId"); 文本框:@Html.TextBox("RoleCode", "", n ...
- Advanced SQL: Relational division in jOOQ
i Rate This Relational algebra has its treats. One of the most academic features is the ...
- GIT 应用gitreview方式提交代码过程
t status -- 是不是修改的文件 git diff (文件名) -- 看文件修改位置 git add (文件名的空格串) git commit -- 提交到本地 git stash -- 暂存 ...
- static数据成员与const数据成员的定义与初始化
三种数据类型的初始化 1.static int a 的初始化 const int a 的初始化 static const int a的初始化 三种初始化方式 在类外初始化 在构造函数中通过初始化列表初 ...
- luogu 3375 KMP模板题
#include<bits/stdc++.h> using namespace std; ,M = ; int next[N]; void getNext(char s[]) //找nex ...