spring boot 调度任务
1.引入spring boot所依赖的jar包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2.使用 @Scheduled 标记需要调度的方法
package com.mouzhi.springboot; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; import java.text.SimpleDateFormat;
import java.util.Date; @Component
public class ScheduledTasks {
private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000)
public void reportCurrentTime(){
logger.info("The time is now {}", dateFormat.format(new Date()));
}
}
3.在项目启动项配置 @EnableScheduling 确保创建后台任务执行程序。没有它,没有任何计划。
package com.mouzhi.springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}
4.@scheduled参数说明
@Scheduled(fixedRate=2000):上一次开始执行时间点后2秒再次执行;
@Scheduled(fixedDelay=2000):上一次执行完毕时间点后2秒再次执行;
@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延迟1秒执行,然后在上一次执行完毕时间点后2秒再次执行;
@Scheduled(cron="* * * * * ?"):按cron规则执行。
常用的cron表达式
0 10,14,16 * * ? 每天上午10点,下午2点,4点
0/30 9-17 * * ? 朝九晚五工作时间内每半小时
0 12 ? * WED 表示每个星期三中午12点
"0 0 12 * * ?" 每天中午12点触发
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
"0 15 10 15 * ?" 每月15日上午10:15触发
"0 15 10 L * ?" 每月最后一日的上午10:15触发
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
"0 15 10 ? * *" 每天上午10:15触发
"0 15 10 * * ?" 每天上午10:15触发
"0 15 10 * * ? *" 每天上午10:15触发
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发
在线Cron表达式生成器:http://cron.qqe2.com/
spring boot 调度任务的更多相关文章
- Spring Boot 调度器
Spring Boot 可以很简单的添加一个调度任务 首先需要添加maven依赖 <dependency> <groupId>org.springframework</g ...
- spring boot / cloud (十五) 分布式调度中心进阶
spring boot / cloud (十五) 分布式调度中心进阶 在<spring boot / cloud (十) 使用quartz搭建调度中心>这篇文章中介绍了如何在spring ...
- 使用Spring Boot搭建应用开发框架(一) —— 基础架构
Spring的简史 第一阶段:XML配置,在Spring1.x时代,使用Spring开发满眼都是xml配置的Bean,随着项目的扩大,我们需要把xml配置文件分放到不同的配置文件里,那时候需要频繁的在 ...
- spring Boot+spring Cloud实现微服务详细教程第二篇
上一篇文章已经说明了一下,关于spring boot创建maven项目的简单步骤,相信很多熟悉Maven+Eclipse作为开发常用工具的朋友们都一目了然,这篇文章主要讲解一下,构建spring bo ...
- Spring Boot定时任务应用实践
在Spring Boot中实现定时任务功能,可以通过Spring自带的定时任务调度,也可以通过集成经典开源组件Quartz实现任务调度. 一.Spring定时器 1.cron表达式方式 使用自带的定时 ...
- 详细介绍Spring Boot 2.0的那些新特性与增强
以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- Spring Boot Web 开发注解篇
本文提纲 1. spring-boot-starter-web 依赖概述 1.1 spring-boot-starter-web 职责 1.2 spring-boot-starter-web 依赖关系 ...
- spring boot(二):启动原理解析
我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static voi ...
随机推荐
- Tript协议|伯尔尼公约|著作权|立法宗旨|自动保护|著作权集体管理|
知识产权 国际条约: Tript协议是国际性公约,<与贸易有关的知识产权协定>(英文:Agreement on Trade-Related Aspects of Intellectual ...
- TPO4-1 Deer Populations of the Puget Sound
The causes of this population rebound are consequences of other human actions. First, the major pred ...
- JDK5.0 Annotation学习笔记(一)
背景知识: 从JDK5开始提供名为Annotation(注释)的功能,它被定义为JSR-175规范.注释是以"@注释名"在代码中存在的,还可以添加一些参数值,例如: ...
- git的命令操作指南
Git图形化界面我用的还可以,但是命令就不太会了,索性和大家一起学习下Git命令的用法...一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. fetch ...
- [C#] 动态指定泛型类型
前言 今天为了程序能写好看一点,一直在纠结怎么指定动态泛型, 但是想想实用性好像不太大,可是把这技术忘掉太可惜XD 还是记录下来,以防忘记 以下程序范例 类 cs 123456789101112131 ...
- OpenCV 特征描述
#include <stdio.h> #include <iostream> #include "opencv2/core/core.hpp" #inclu ...
- verilog的function使用
语法: function [range] function_id; input_declaration other_declarations procedural_statement ...
- elasticsearch用法
基本原理 搜索引擎的索引 倒排序 由value查找key 数据库的索引 由key查找value 用于解决分库分表后的排序分页 like查找 性能问题 日志库的全文搜索 spring集成时使用的不是re ...
- 17)将index.php中的代码放到Framework中封装起来
目录结构: 发生改动的类代码: 新增类:Framework.class.php <?php /** * Created by PhpStorm. * User: Interact * Date: ...
- Java IO: PipedOutputStream
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) PipedOutputStream可以往管道里写入读取字节流数据,代码如下: 01 Outp ...