Spring Boot笔记(四) springboot 集成 @Scheduled 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有)
1、在SpringBoot 项目中使用@Scheduled注解执行定时任务:
配置pom.xml 依赖:
一般情况下,SpringBoot 的 相关依赖,如:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
是有包含对应 @Scheduled 定时注解启动所需要的jar包的,如下:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
2、创建定时类
(1)@Component : 将当前类实例化注入到容器中
(2)定时器的生效要素:
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
在程序启动时,定时器会开始计时,并根据设定时间执行设置好的任务内容,线程的执行是以单线程进行;
(3)创建以下定时类
package com.example.poiutis.quartz;
import com.example.poiutis.common.SystemLogHandler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @ClassName HandleTimeQuartz
* @Description TODO
* @Author wushaopei
* @Date 2019/7/25 20:23
* @Version 1.0
*/
@Component
public class HandleTimeQuartz {
// 系统日志
//private static SystemLogHandler systemLogger = SystemLogHandler
// .getLogger(HandleTimeQuartz.class);
private static Logger logger = LoggerFactory.getLogger(InvoiceController.class);
@Scheduled(cron="0/5 * * * * ? ")
public void userStatusJob() {
System.out.println("进入测试");
logger.info(System.currentTimeMillis()+"");
}
}
在 程序入口处添加注解
直接启动后会自动执行:效果如下:
Spring Boot笔记(四) springboot 集成 @Scheduled 定时任务的更多相关文章
- Spring Boot笔记(三) springboot 集成 Quartz 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1. 在 pom.xml 中 添加 Quartz 所需要 的 依赖 <!--定时器 quartz- ...
- Spring Boot笔记(六) springboot 集成 timer 定时任务
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.创建具体要执行的任务类: package com.example.poiutis.timer; im ...
- Spring Boot笔记(二) springboot 集成 SMTP 发送邮件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 笔记:乘着项目迭代的间隙,把一些可复用的功能从项目中抽取出来,这是其中之一, 一.添加SMTP 及 MA ...
- Spring Boot笔记(一) springboot 集成 swagger-ui
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.添加依赖 <!--SpringBoot整合Swagger-ui--> <depen ...
- Spring Boot笔记(五) SpringBoot 集成Lombok 插件
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 为了减少代码量,为当前项目添加 lombok 来优雅编码 Lombok 插件安装: a . 添加依赖: ...
- Spring Boot笔记四:日志框架介绍
我是一名程序员,我喜欢写System.out.println来打印一些重要的信息...后来我学了面向对象,我把这些输出语句整合到了一个工具类里面,可以打印,也可以保存日志. 我是一名积极思考的程序员, ...
- Spring Boot入门(三):使用Scheduled注解实现定时任务
在程序开发的过程中,经常会使用定时任务来实现一些功能,比如: 系统依赖于外部系统的非核心数据,可以定时同步 系统内部一些非核心数据的统计计算,可以定时计算 系统内部的一些接口,需要间隔几分钟或者几秒执 ...
- Spring Boot 2.X(十二):定时任务
简介 定时任务是后端开发中常见的需求,主要应用场景有定期数据报表.定时消息通知.异步的后台业务逻辑处理.日志分析处理.垃圾数据清理.定时更新缓存等等. Spring Boot 集成了一整套的定时任务工 ...
- Spring Boot 2.x 快速集成Kafka
1 Kafka Kafka是一个开源分布式的流处理平台,一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者在网站中的所有动作流数据.Kafka由Scala和Java编写,2012年成为Apache ...
随机推荐
- 聚合类型与POD类型
Lippman在<深度探索C++对象模型>的前言中写道: I have heard a number of people over the years voice opinions sim ...
- qt creator源码全方面分析(4-4)
目录 统计接口实现 统计接口实现 我们知道,插件架构必不可少的是定义接口类,即抽象基类,描述用户需要自定义实现的内容.此外,一般还有一个管理器类,对接口类的所有实现类进行管理,并调用其中的接口进行.源 ...
- 【Scala】isInstanceOf 与 classOf的对比,代码+注解简洁明了
class Animal { } class Cat extends Animal { } object Cat { def main(args: Array[String]): Unit = { / ...
- STM32F767ZI NUCLEO144 基于CubeIDE快速开发入门指南
刚入手的NUCLEO-F767ZI:整合官网资源,理清思路,便于快速进行快发: 文章目录 1 NUCLEO 系列 2 NUCLEO-F767ZI 3 环境搭建 3.1 Keil/IAR安装 3.2 C ...
- python --RecursionError: maximum recursion depth exceeded in comparison
在学习汉娜塔的时候,遇到一个error RecursionError: maximum recursion depth exceeded in comparison 经过百度,百度的方法: 加上: i ...
- Xshell 与 Xftp 的安装与使用
我们在日常工作中,不管是系统管理员.程序员.还是技术工程师,如果想登陆到 Linux 服务器,不可能总是跑到机房去工作,通常我们需要一个工具帮我们去做远程连接,这样我们只需要用笔记本电脑就可以连接到服 ...
- 51Nod1127 最小包含字符串
51Nod1127 #include <iostream> #include <string> using namespace std; const int inf = 0x3 ...
- 数据结构----二叉树Tree和排序二叉树
二叉树 节点定义 class Node(object): def __init__(self, item): self.item = item self.left = None self.right ...
- Hyperledger Fabric开发(一):环境配置
macOS系统下配置hyperledger fabric环境 cURL mac中自带curl,如果需要下载最新版本,则可按照以下命令,并将路径添加在系统环境变量的最前面即可 brew install ...
- poj2455 k条路最小化最长边
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12414 Accepted ...