spring boot(十):定时任务
springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现。
1、pom包配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2、启动类启用定时
在启动类上面加上@EnableScheduling即可开启定时
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()));
} }
测试
private int count=0;
@RequestMapping("/Scheduled")
@Scheduled(cron="*/6 * * * * ?")
public void process(){
System.out.println("this is scheduler task runing "+(count++));
}

参数说明
@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000):上一次开始执行时间点之后6秒再执行@Scheduled(fixedDelay = 6000):上一次执行完毕时间点之后6秒再执行@Scheduled(initialDelay=1000, fixedRate=6000):第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
spring boot(十):定时任务的更多相关文章
- Spring Boot配置定时任务
在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...
- spring Boot(十九):使用Spring Boot Actuator监控应用
spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...
- Spring Boot(十八):使用Spring Boot集成FastDFS
Spring Boot(十八):使用Spring Boot集成FastDFS 环境:Spring Boot最新版本1.5.9.jdk使用1.8.tomcat8.0 功能:使用Spring Boot将文 ...
- Spring Boot(十六):使用Jenkins部署Spring Boot
Spring Boot(十六):使用Jenkins部署Spring Boot jenkins是devops神器,介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署 ...
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例
Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例 一.快速上手 1,配置文件 (1)pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 ...
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理
Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...
- Spring Boot(十二):spring boot如何测试打包部署
Spring Boot(十二):spring boot如何测试打包部署 一.开发阶段 1,单元测试 在开发阶段的时候最重要的是单元测试了,springboot对单元测试的支持已经很完善了. (1)在p ...
- 【Spring Boot】定时任务
[Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...
- spring boot 创建定时任务
@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...
- Spring Boot:定时任务
在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...
随机推荐
- python 协程库gevent学习--源码学习(一)
总算还是要来梳理一下这几天深入研究之后学习到的东西了. 这几天一直在看以前跟jd对接的项目写的那个gevent代码.为了查错,基本上深入浅出了一次gevent几个重要部件的实现和其工作的原理. 这里用 ...
- __new__ __init__区别
1 class A(object): 2 def __init__(self,*args, **kwargs): 3 print "init A" 4 def __new__(cl ...
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):3. 随机颜色及清除按钮
1. 随机颜色 通过前面的教程,咪博士已经带大家实现了画板的绘图功能.但是,现在画板只能画出黄色的图案,还十分单调,接下来咪博士就教大家,如何使用随机颜色,让画板变得五彩斑斓. 改进后的代码如下: f ...
- VS2008中英文转换
设置Visual Studio的语言: 工具=>选项=>环境=>区域设置=>语言 如图: 对于英文不好的朋友还是挺好用的
- 初学者学习C++的50条忠告
1.把C++当成一门新的语言学习(和C没啥关系!真的.); 2.看<Thinking In C++>,不要看<C++变成死相>; 3.看<The C++ Programm ...
- Oracle数据库导入导出 imp/exp备份还原
Oracle数据导入导出imp/exp Oracle数据导入导出imp/exp 在cmd的dos命令提示符下执行,而不是在sqlplus里面,但是格式一定要类似于: imp/exp 用户名/密 ...
- Word2010设置题注和交叉引用方法
设置题注 点击图片-->右键-->插入题注-->新建标签:“图”-->选择新建标签“图”-->修改“编号”-->勾选包含章节号-->设置章节起始样式:标题2- ...
- Quartz.NET 前一次任务未执行完成时不触发下次的解决方法
如图所示,在Job 上 加 [DisallowConcurrentExecution] 特性
- windows下安装PyTorch0.4.0
PyTorch框架,据说2018.4.25刚刚上架windows,安个玩玩 我的环境: windows 10 anaconda虚拟环境python3.6 cuda9.1 cudnn7 pytorch ...
- Domino 邮箱服务器接收不存在的邮箱账号的邮件
背景: domino邮箱服务器需要设置多个邮件账号邮件转到同一个邮箱账号. 比如:现在没有了 abc@mail.com 的邮箱账号.但是当用户发邮件给它的时候就发给Support@mail.com 原 ...