http://start.spring.io/ 中新建一个Group为com.zifeiy,Artifact为task的工程。

然后在TaskApplication中添加注释:@EnableScheduling ,用以开启定时任务:

package com.zifeiy.task;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
public class TaskApplication { public static void main(String[] args) {
SpringApplication.run(TaskApplication.class, args);
}
}

然后新建一个类用于部署定时任务:

package com.zifeiy.task.component;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Component
public class TaskComponent { @Scheduled(cron="0 * * * * *")
public void schedule1() {
System.out.println("schedule 1: " + new Date());
} @Scheduled(fixedRate = 5000)
public void schedule2() {
System.out.println("schedule 2: " + new Date());
} @Scheduled(fixedDelay = 5000)
public void schedule3() {
System.out.println("schedule 3: " + new Date());
} @Scheduled(initialDelay=1000, fixedRate=5000)
public void schedule4() {
System.out.println("schedule 4: " + new Date());
}
}
  • @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
  • @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
  • @Scheduled(cron="0 * * * * *") :通过cron表达式定义规则

注意,这里的时间,单位是毫秒,1秒=1000毫秒

cron属性

  • 第一位,表示秒,取值0-59
  • 第二位,表示分,取值0-59
  • 第三位,表示小时,取值0-23
  • 第四位,日期天/日,取值1-31
  • 第五位,日期月份,取值1-12
  • 第六位,星期,取值1-7,星期一,星期二...,注:不是第1周,第二周的意思 另外:1表示星期天,2表示星期一。
  • 第7为,年份,可以留空,取值1970-2099

参考链接:

Spring Boot设置定时任务的更多相关文章

  1. spring boot 创建定时任务

    @Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...

  2. Spring Boot配置定时任务

    在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...

  3. 【Spring Boot】定时任务

    [Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...

  4. Spring Boot:定时任务

    在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...

  5. 【Spring Boot学习之六】Spring Boot整合定时任务&异步调用

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2一.定时任务1.启动类添加注解@EnableScheduling 用于开启定时任务 package com.wjy; i ...

  6. spring boot 实现定时任务

    定时任务或者说定时调度,是系统中比较普遍的一个功能,例如数据归档.清理,数据定时同步(非实时),定时收发等等都需要用到定时任务,常见的定时调度框架有Quartz.TBSchedule等. 如何在Spr ...

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

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

  8. Spring Boot 创建定时任务(配合数据库动态执行)

    序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库 ...

  9. Spring Boot 设置静态资源访问

    问题描述 当使用spring Boot来架设服务系统时,有时候也需要用到前端页面,当然就不可或缺地需要访问其他一些静态资源,比如图片.css.js等文件.那么如何设置Spring Boot网站可以访问 ...

随机推荐

  1. Java字节码方法表结构深度剖析

    继续上一次[https://www.cnblogs.com/webor2006/p/9459681.html]的字节码分析,这次来分析一下最为复杂的方法表的信息,如下: 而上一次分析到了属性表的位置在 ...

  2. 单元测试框架之unittest(六)

    一.摘要 本片博文将介绍unittest框架的一些轻便有效的特性,在我们的测试中经常可以用到 如果有一些测试方法不想执行,如果有些测试方法在某些条件下不执行 该当如何? 如果有些方法未在unittes ...

  3. v-solt插槽

    https://www.jb51.net/article/157565.htm https://juejin.im/post/5c64e11151882562e4726d98

  4. Property or method "openPageOffice" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by

    Property or method "openPageOffice" is not defined on the instance but referenced during r ...

  5. java-十五周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  6. FTPClient上传下载等

    package com.lct.conference.controller.MonitorManagement.cofer; import org.apache.commons.net.ftp.FTP ...

  7. MySQL数据分析-(12)表操作补充:字段属性

    大家好,我是jacky朱元禄,很高兴继续跟大家学习MySQL数据分析实战,今天我们分享的主题是表操作补充之字段属性,依照惯例第一部分,jacky先跟大家分享本课时的学习逻辑 (一)学习逻辑 我们说创建 ...

  8. vim 操作命令

    #显示行号,设定之后,会在每一行的前缀显示该行的行号:set nu #取消行号显示:set nonu #将编辑的数据写入硬盘档案中(常用):w #若文件属性为『只读』时,强制写入该档案.不过,到底能不 ...

  9. mybatis中foreach参数过多效率很慢的优化

    foreach 后面in 传入的参数有1万条,#和$是有效率区别的,$的效率远高于#,上篇文章做了比较. 但没达到我的理想结果. 1. 更改方式,把foreach 去掉,改成拼装方式, 参数直接拼装成 ...

  10. 【java】简介(一)

    应用:web后端开发.android-app开发.大数据应用开发 学习:java会过时,但程序设计的思想不会过时 特点:1.面向对象,跨平台,语法比c++简单 2.以字节码的形式运行在虚拟机上 3.自 ...