Spring Boot(九):定时任务
Spring Boot(九):定时任务
一、pom包配置
pom包里面只需要引入springboot 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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
二、启动类启用定时
在启动类上面加上@EnableScheduling即可开启定时
@SpringBootApplication
@EnableScheduling
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
三、创建定时任务实现类
定时任务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(九):定时任务的更多相关文章
- spring boot(九)定时任务
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom包配置 pom包里面只需要引入springboot ...
- Spring Boot配置定时任务
在项目开发过程中,经常需要定时任务来做一些内容,比如定时进行数据统计(阅读量统计),数据更新(生成每天的歌单推荐)等. Spring Boot默认已经实现了,我们只需要添加相应的注解就可以完成定时任务 ...
- 【Spring Boot】定时任务
[Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...
- spring boot 创建定时任务
@Scheduled默认创建的线程是单线程,任务的执行会受到上一个任务的影响,创建定时任务也比较简单 123456789101112 @Component@Configuration //1.主要用于 ...
- (转)Spring Boot(九):定时任务
http://www.ityouknow.com/springboot/2016/12/02/spring-boot-scheduler.html 在我们开发项目过程中,经常需要定时任务来帮助我们来做 ...
- Spring Boot 实现定时任务的 4 种方式
作者:Wan QingHua wanqhblog.top/2018/02/01/SpringBootTaskSchedule/ 定时任务实现的几种方式: Timer:这是java自带的java.uti ...
- Spring Boot:定时任务
在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom 包配置 pom 包里面只需要引入 Spring ...
- Spring boot创建定时任务
基于spring boot的应用创建定时任务不要太简单,给一个类加上@Configuration @EnableScheduling注解,然后给该类需要定时执行的方法加上@Scheduled(cron ...
- Spring Boot (29) 定时任务
使用场景:数据定时增量同步,定时发送邮件,爬虫定时抓取 定时任务概述 定时任务:顾名思义就是在特定/指 定的时间进行工作,比如我们的手机闹钟,他就是一种定时的任务. 实现方式: 1.Timer:JDK ...
随机推荐
- NSA Fuzzbunch中EternalRomance工具复现过程
自Shadow Brokers公布NSA泄露工具后,各路大神陆陆续续发表复现过程,这几天也仔细试了各种套路,一直想弄明白DoublePulsar中的shellcode到底是如何用的,刚好又在模拟环境中 ...
- Selenium基础知识(八)文本域处理textarea
textarea文本域selenium有时处理不了,但是我们可以采用迂回,解决这个问题 那么,我们用到了execute_script()方法来调用javascript来解决这个问题 这其中涉及DOM, ...
- pandas常用函数
1. df.head(n): 显示数据前n行,不指定n,df.head则会显示所有的行 2. df.columns.values获取所有列索引的名称 3. df.column_name: 直接获取列c ...
- hbase-java-api001
package api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfig ...
- <1>lua编译环境 数据类型和局部变量
1.编译环境 http://www.lua.org/download.html下载 解压后 bin目录中lua.exe运行 luac.exe编译成lua字节码 2.基本数据类型 整数,小数,布尔值 ...
- cmd下 mysql操作命令大全详解
启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库:show datab ...
- libvirt_python
一.Connections 连接函数接口libvirt.open(name); //可读写方式连接上QEMU 参数说明: name:连接名称libvirt.openAuth(uri, auth, fl ...
- eclipse更换workspace需要重新设置的内容
.jdk Window-->java-->Installed JREs,新增或修改你所需要的jdk版本,点击需要的jdk-->edit 在Default VM arguments里面 ...
- Linux中常用的50个命令
1. [命令]:cat [功能说明]: concatenate files and print on the standard output #连接文件并打印到标准输出,有标准输出的都可以用重定向定向 ...
- python: ImportError: cannot import name 'Style' from 'openpyxl.styles' 解决方法
import os, openpyxl from openpyxl.styles import Font, Style os.chdir("C:\\") wb = openpyxl ...