springBoot(19):定时任务
一、依赖
1
2
3
4
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter</ artifactId >
</ dependency >
|
二、实现
启动类上需加上@EnableScheduling注解
SpringBootSchedulingApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class SpringBootSchedulingApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootSchedulingApplication. class , args);
}
}
|
Task1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.example.demo.utils.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 每隔六秒执行(cron表达式)
*
* @Author: 我爱大金子
* @Description: 每隔六秒执行(cron表达式)
* @Date: Create in 18:03 2017/7/4
*/
@Component
public class Task1 {
private int count= 0 ;
@Scheduled (cron= "*/6 * * * * ?" )
private void process(){
System.out.println( "this is scheduler task runing " +(count++));
}
}
|
Task2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package com.example.demo.utils.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 每隔六秒执行(fixed方式)
*
* @Author: 我爱大金子
* @Description: 每隔六秒执行(fixed方式)
* @Date: Create in 18:03 2017/7/4
*/
@Component
public class Task2 {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "HH:mm:ss" );
@Scheduled (fixedRate = 6000 )
public void reportCurrentTime() {
System.out.println( "现在时间:" + dateFormat.format( new Date()));
}
}
|
效果:
三、说明
3.1、@Scheduled参数
@Scheduled参数可以接受两种定时的设置:一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。
fixedRate说明
@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行
@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行
@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按fixedRate的规则每6秒执行一次
3.2、Could not find default TaskScheduler bean异常处理
就会在debug级别的日志输出一个异常:
1
|
logger.debug( "Could not find default TaskScheduler bean" , ex);
|
当然,这个异常并不影响应用程序运行,如果你不想看到这个异常,就可以通过提升org.springframework.scheduling这个包下日志级别来屏蔽这个不合理的异常。
本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1944645如需转载请自行联系原作者
我爱大金子
springBoot(19):定时任务相关推荐
- SpringBoot | :定时任务的使用
前言 上一章我们简单的讲解了关于异步请求相关知识点.这一章节,我们来讲讲开发过程也是经常会碰见的定时任务.比如每天定时清理无效数据.定时发送短信.定时发送邮件.支付系统中的定时对账等等,往往都会定义一 ...
- springboot之定时任务
定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...
- SpringBoot 实战定时任务 Scheduled
序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...
- springboot配置定时任务及常用的cron表达式
springboot引入定时任务 springboot引入定时任务主要需要以下几步: 1.引入相关的依赖 2.配置程序开启定时任务 3. 编写定时任务 引入相关的依赖 只用引一个基础的web的依赖就可 ...
- SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊)
SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 目录 SpringBoot整合定时任务和邮件发送(邮箱 信息轰炸 整蛊) 1.概述 2.最佳实践 2.1创建项目引入依赖(mail) ...
- SpringBoot实现定时任务的三种方式,总有一款适合你!
点击关注公众号,利用碎片时间学习 序言 SpringBoot创建定时任务,目前主要有以下三种实现方式: 基于注解(@Scheduled): 基于注解@Scheduled默认为单线程,开启多个任务时,任 ...
- SpringBoot整合定时任务和Emil发送
SpringBoot整合定时任务和Emil发送 定时任务 任务系统指的是定时任务.定时任务是企业级开发中必不可少的组成部分,诸如长周期业务数据的计算,例如年度报表,诸如系统脏数据的处理,再比如系统 ...
- linux定时任务重复率,基于SpringBoot实现定时任务的设置(常用:定时清理数据库)...
1.构建SpringBoot工程项目 1)创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务. @SpringBootApplication @Ena ...
- springboot多线程定时任务
我们经常会遇到定时任务,在某个特定的时间点,程序会自主触发去执行一些机械重复的工作,这样就可以将人力与精力彻底的解放出来了. 在最近的工作中,先是在后台报表工程中用到了springboot自带的定时器 ...
- SpringBoot中定时任务与异步定时任务的实现
场景 若依前后端分离版手把手教你本地搭建环境并运行项目: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662 在上面 ...
最新文章
- new/delete和malloc/free的区别(举例说明)(简单点)
- python算法和数据结构_Python中的数据结构和算法
- 为什么我推荐你用Ubuntu开发?
- 技术交底书(一)-----一种移动终端无需充电及移动终端
- ARQ协议与滑动窗口协议
- php画图抗锯齿,关于抗锯齿的详细介绍
- Atlas:Ubuntu18.04使用过程中空间爆满的处理(.cache)
- OCCT培训笔记(刘星讲)--第1天
- TLF 使用详解!!
- 置信区间的临界值_在用正态分布进行置信区间估计时,临界值2.58所对应的置信水平是( )。...
- 一个有趣公众号的简介-网络灯下黑
- 程序模板(20210603笔记)
- ubuntu18.04安装opencv的viz模块
- skfuzzy.cmeans与sklearn.KMeans聚类效果对比以及使用方法
- Windows程式开发设计指南(二十一)动态连结程式库
- 背包九讲之二:完全背包问题
- 解决AutoCAD软件打开CASS软件成图字体显示成问号的问题
- 豆瓣的架构—专访豆瓣网站的技术总监洪强宁
- 支持HEVC/H265 RTMP接收的FFMPEG/FFPLAY WINDOWS版本
- ECC校验有什么作用
热门文章
- linux内核网络协议栈--数据包的接收过程(二十二)
- HTTPS 的工作原理
- python random模块中的指令_10分钟让你掌握python编程中random模块功能使用,非常详细...
- php函数scandir_使用PHP函数scandir排除特定目录
- python number函数_Python3 数据类型-Number
- FastDFS蛋疼的集群和负载均衡(十五)之lvs四层+Nginx七层负载均衡
- MyBatis:参数传递 [转]
- WDS配置时遇到的问题
- 项目开发之git配置
- oralce 表空间解锁