参考: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实现定时任务的一种方式的更多相关文章

  1. SpringBoot配置定时任务的两种方式

    一.导入相关的jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  2. springboot实现定时任务的两种方式

    方式一:在springboot启动类上添加@EnableScheduling注解,然后创建具体的任务类,在方法上添加@Scheduled注解,并指明执行频率即可.如下: @Componentpubli ...

  3. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

  4. springboot成神之——mybatis在spring-boot中使用的几种方式

    本文介绍mybatis在spring-boot中使用的几种方式 项目结构 依赖 WebConfig DemoApplication 方式一--@Select User DemoApplication ...

  5. SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)

    前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...

  6. 【SpringBoot】05.SpringBoot整合Listener的两种方式

    SpringBoot整合Listener的两种方式: 1.通过注解扫描完成Listener组件的注册 创建一个类实现ServletContextListener (具体实现哪个Listener根据情况 ...

  7. 【SpringBoot】03.SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...

  8. Springboot下载Excel的3种方式

    Springboot下载Excel的3种方式 汇总一下浏览器下载和代码本地下载实现的3种方式. (其实一般都是在代码生成excel,然后上传到oss,然后传链接给前台,但是我好像没有实现过直接点击就能 ...

  9. Spring Boot 实现定时任务的 4 种方式

    作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...

随机推荐

  1. Go sqlx库

    sqlx is a library which provides a set of extensions on go's standard database/sql library. sqlx sup ...

  2. linux 安装mysql服务

    1.检查是否已安装,grep的-i选项表示匹配时忽略大小写 rpm -qa|grep -i mysql *可见已经安装了库文件,应该先卸载,不然会出现覆盖错误.注意卸:载时使用了--nodeps选项, ...

  3. struts2实现文件的上传和下载实例[转]

    实现原理 Struts 2是通过Commons FileUpload文件上传. Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器 ...

  4. pylab.show()没有显示图形图像(python的matplotlib画图包)

    no display name and no $DISPLAY environment variable ============================ @Neil's answer is ...

  5. 元素 "context:component-scan" 的前缀 "context" 未绑定的解决方案

    在动态web项目(Dynamic Web Project)中,使用SpringMVC框架,新建Spring的配置文件springmvc.xml,添加扫描控制器 <context:componen ...

  6. Sqoop将MySQL表结构同步到hive(text、orc)

    Sqoop将MySQL表结构同步到hive sqoop create-hive-table --connect jdbc:mysql://localhost:3306/sqooptest --user ...

  7. Codeforces Round #370 (Div. 2) A , B , C 水,水,贪心

    A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. spring启动加载类,手动加载bean

    方法一: public final class Assembler implements BeanFactoryPostProcessor { private static ConfigurableL ...

  9. Secret Code

    Secret Code 一.题目 [NOIP模拟赛A10]Secret Code 时间限制: 1 Sec  内存限制: 128 MB 提交: 10  解决: 6 [提交][状态][讨论版] 题目描述 ...

  10. 2016-02-20WebForm登陆验证,判断用户是否登陆 PageBase类

    http://blog.csdn.net/fanbin168/article/details/49404233 很多时候,WebFrom页面,我们需要判断用户是否已经登陆了.假如有很多页面,难道我们要 ...