http://www.ityouknow.com/springboot/2016/12/02/spring-boot-scheduler.html

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

1、pom 包配置

pom 包里面只需要引入 Spring Boot Starter 包即可

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2、启动类启用定时

在启动类上面加上@EnableScheduling即可开启定时

@SpringBootApplication
@EnableScheduling
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

3、创建定时任务实现类

定时任务1:

@Component
public class SchedulerTask { private int count=0; @Scheduled(cron="*/6 * * * * ?")
private void process(){
System.out.println("this is scheduler task runing "+(count++));
} }

定时任务2:

@Component
public class Scheduler2Task { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 6000)
public void reportCurrentTime() {
System.out.println("现在时间:" + dateFormat.format(new Date()));
} }

结果如下:

this is scheduler task runing  0
现在时间:09:44:17
this is scheduler task runing 1
现在时间:09:44:23
this is scheduler task runing 2
现在时间:09:44:29
this is scheduler task runing 3
现在时间:09:44:35

参数说明

@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。

fixedRate 说明

  • @Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行
  • @Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按 fixedRate 的规则每6秒执行一次

文章内容已经升级到 Spring Boot 2.x

示例代码-github

示例代码-码云

(转)Spring Boot(九):定时任务的更多相关文章

  1. spring boot(九)定时任务

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

  2. Spring Boot(九):定时任务

    Spring Boot(九):定时任务 一.pom包配置 pom包里面只需要引入springboot starter包即可 <dependencies> <dependency> ...

  3. Spring Boot配置定时任务

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

  4. 【Spring Boot】定时任务

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

  5. spring boot 创建定时任务

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

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

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

  7. Spring Boot:定时任务

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

  8. Spring boot创建定时任务

    基于spring boot的应用创建定时任务不要太简单,给一个类加上@Configuration @EnableScheduling注解,然后给该类需要定时执行的方法加上@Scheduled(cron ...

  9. Spring Boot (29) 定时任务

    使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...

随机推荐

  1. [android] 短信的广播接收者

    比较重要的一个广播事件,短信 界面布局,比如播放视频,默认是横屏全屏的,清单文件中进行设置, 在<activity/>节点设置屏幕朝向属性,android:screenOrientatio ...

  2. 填一个laravel视图缓存没有及时更新的坑

    1.此坑背景 laravel在渲染blade模板后,会将渲染好的结果存到storage/framework/views(默认路径,也可在配置中修改的)中,以便下次使用.但我最近总是发现修改了blade ...

  3. C#字节图片互转、字节转换图片、图片转换字节、byte[]转换图片、图片转换成byte[]

    原文地址:http://www.cnblogs.com/wifi/articles/3522773.html /// <summary> /// 图片转换成字节流 /// </sum ...

  4. ajaxJson(常用)

    function ajaxJson(method, url, data, callback) { var options = { type: method, url: url, dataType: ' ...

  5. select&epoll

    内核空间和用户空间 现在操作系统都是采用虚拟存储器,那么对 32 位操作系统而言,它的寻址空间(虚拟地址空间,或叫线性地址空间)为 4G(2的32次方).也就是说一个进程的最大地址空间为 4G.操作系 ...

  6. SAP MM GR-based IV, 无GR不能IV?

    SAP MM GR-based IV, 无GR不能IV? 如下的采购订单, 没有做过收货, ITEM的details里却勾选了’GR-Bsd IV’ 选项. 此时试图直接针对该PO#450260713 ...

  7. 也说Socket

    网上有大量socket相关文章,茫茫多,大多交代不清,最近自我整理了一下socket相关知识,附加了大量代码注释,先看效果. 上代码,客户端: Socket socket1 = null;//一个全局 ...

  8. 使用Nginx实现服务器反向代理和负载均衡

    前言 同事总问我Nginx做反向代理负载均衡的问题,因此特意留下一篇扫盲贴! 直接部署服务器的风险 假设,我开发了一个网站,然后买了一台Web服务器和一台数据库服务器,直接部署到公共网络上.如下图,网 ...

  9. Kotlin入门(24)如何自定义视图

    Android提供了丰富多彩的视图与控件,已经能够满足大部分的业务需求,然而计划赶不上变化,总是有意料之外的情况需要特殊处理.比如PagerTabStrip无法在布局文件中指定文本大小和文本颜色,只能 ...

  10. 学习用Node.js和Elasticsearch构建搜索引擎(6):实际项目中常用命令使用记录

    1.检测集群是否健康. curl -XGET 'localhost:9200/_cat/health?v' #后面加一个v表示让输出内容表格显示表头 绿色表示一切正常,黄色表示所有的数据可用但是部分副 ...