在日常的开发过程中经常使用到定时任务,在springMVC的开发中,经常和quartz框架进行集成使用,但在springboot中没有这么做,而是使用了java的线程池来实现定时任务。

一、概述

在springboot中使用定时任务非常简单,只需要简单的几步即可完成。

二、详述

在springboot中要使用定时任务,首先要保证环境是springboot的,这里使用的是springboot-2.1.2.release版本。在启动类上加@EnableScheduling注解,如下,

package com.example.demo;

import com.example.demo.properties.ApplicationPro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication
@EnableConfigurationProperties({ApplicationPro.class})
//引入开启定时任务的注解
@EnableScheduling
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }

@EnableScheduling注解的作用就是开启对定时任务的支持,这个注解的作用是开启定时任务的自动配置。

在使用了@EnableScheduling注解后便可以编写具体的定时任务的job类,该job类无需继承或实现任何接口,只要是一个被spring管理的类即可。为了使spring可以管理统一使用@Component注解标识。在定时任务的类中的方法上标识@Scheduled注解便可以定时执行该方法,@Scheduled注解上有几种不同的属性,看具体的该注解的定义,

fixedDelay

@Scheduled(fixedDelay=1000)/@Scheduled(fixedDelay="1000")的意思是该方法执行完后每隔1000ms执行一次。看具体的代码

package com.example.demo.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SchedulFixedRelayTest { //@Scheduled(fixedDelay = 5000)
public void jobTest(){ try {
log.info("使用fixedDelay的定时任务");
Thread.sleep(10*1000); } catch (InterruptedException e) {
e.printStackTrace();
} } }

看执行结果,

2020-12-09 22:02:47.511  INFO 7368 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 940 ms
2020-12-09 22:02:47.681 INFO 7368 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-12-09 22:02:47.782 INFO 7368 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler'
2020-12-09 22:02:47.802 INFO 7368 --- [ scheduling-1] c.e.demo.job.SchedulFixedRelayTest : 使用fixedDelay的定时任务
2020-12-09 22:02:47.820 INFO 7368 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-09 22:02:47.823 INFO 7368 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.627 seconds (JVM running for 2.51)
2020-12-09 22:03:02.819 INFO 7368 --- [ scheduling-1] c.e.demo.job.SchedulFixedRelayTest : 使用fixedDelay的定时任务
2020-12-09 22:03:17.834 INFO 7368 --- [ scheduling-1] c.e.demo.job.SchedulFixedRelayTest : 使用fixedDelay的定时任务

看上面打印的执行时间,第一次在2020-12-09 22:02:47,由于程序会睡眠10秒,也就是说回在22:02:57执行完,那么下次执行应该在22:03:02,看第二次打印的时间刚好和上面的一样,那就证明了,该配置是在方法执行完成后每隔XX秒执行一次。

fixedRate

@Scheduled(fixedRate=1000)/@Scheduled(fixedRate="1000")的意思是该方法执行完后每隔1000ms执行一次,但是如果任务执行的时间超过了配置的时间,则在任务执行完会再次执行。

任务时间小于配置的时间

package com.example.demo.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Slf4j
@Component
public class SchedulFixedRateTest { @Scheduled(fixedRate = 5000)
public void jobTest(){ try {
log.info("使用fixedRate的定时任务");
Thread.sleep(1*1000); } catch (InterruptedException e) {
e.printStackTrace();
} } }

任务会睡眠1s也就是任务耗时1s,fixedRate配置的是每隔5s,由于任务时间小于配置的时间,所以会每隔5s执行一次,看执行结果,

2020-12-09 22:16:16.156  INFO 2800 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务
2020-12-09 22:16:16.188 INFO 2800 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-09 22:16:16.188 INFO 2800 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.625 seconds (JVM running for 2.462)
2020-12-09 22:16:21.178 INFO 2800 --- [ scheduling-1] c.example.demo.job.SchedulFixedRateTest : 使用fixedRate的定时任务
2020-12-09 22:16:26.180 INFO 2800 --- [ scheduling-1] c.example.demo.job.SchedulFixedRateTest : 使用fixedRate的定时任务

看打印的任务时间都是每隔5s执行一次。

任务时间大于配置的时间

看任务的执行时间打印配置的时间的情况,

package com.example.demo.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Slf4j
@Component
public class SchedulFixedRateTest { @Scheduled(fixedRate = 5000)
public void jobTest(){ try {
log.info("使用fixedRate的定时任务");
Thread.sleep(10*1000); } catch (InterruptedException e) {
e.printStackTrace();
} } }

这里任务执行10s,配置的时间为5s。看执行结果,

2020-12-09 22:17:44.070  INFO 12952 --- [   scheduling-1] c.example.demo.job.SchedulFixedRateTest  : 使用fixedRate的定时任务
2020-12-09 22:17:44.102 INFO 12952 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-12-09 22:17:44.102 INFO 12952 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.626 seconds (JVM running for 2.462)
2020-12-09 22:17:54.071 INFO 12952 --- [ scheduling-1] c.example.demo.job.SchedulFixedRateTest : 使用fixedRate的定时任务
2020-12-09 22:18:04.085 INFO 12952 --- [ scheduling-1] c.example.demo.job.SchedulFixedRateTest : 使用fixedRate的定时任务

从上面的结果可以看到任务是每隔10s执行一次,由于任务耗时大于了配置的时长,所以任务执行完以后便会进入下次的执行。

cron

cron表达式共7位,分别是秒、分、小时、日、月、周、年。cron表达式可以从网上找,如下,

https://cron.qqe2.com/

通过cron在线生成,分别设置值,如,0/5 * * * * ?  每隔5s执行一次,

package com.example.demo.job;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; @Slf4j
@Component
public class SchedulCronTest { @Scheduled(cron = "0/5 * * * * ?")
public void jobTest(){ try {
log.info("使用cron的定时任务");
Thread.sleep(10*1000); } catch (InterruptedException e) {
e.printStackTrace();
} } }

每隔5s执行一次,任务耗时10s,看下执行结果,

2020-12-09 22:41:42.718  INFO 17828 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.609 seconds (JVM running for 2.453)
2020-12-09 22:41:45.015 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务
2020-12-09 22:42:00.004 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务
2020-12-09 22:42:15.000 INFO 17828 --- [ scheduling-1] com.example.demo.job.SchedulCronTest : 使用cron的定时任务

可以看到是每隔15s执行一次,也就是如果配置的时间间隔小于任务耗时,那么在任务执行完后的时间间隔后再执行,在此种情况下和fixDelay的用法一致。

三、总结

本文分析了springboot中定时任务的使用,

首先,使用@EnableScheduling开启定时任务的自动配置;

其次,任务类必须受spring管理(使用@Component、@Service等注解均可);

最后,任务方法使用@Scheduled注解标识,该注解有3中不同的属性配置,fixedDelay、fixedRate、cron;

有不正之处,欢迎指正,感谢!

springboot:定时任务的更多相关文章

  1. springboot 定时任务部署至linux服务器上后会执行两次问题

    springboot定时任务在本地运行时,正常执行且只执行一次,但是在maven打包成war包,部署至linux服务器上之后,定时任务奇怪的执行了两次. 由于未做负载均衡,所以可以先排除是因为多台服务 ...

  2. Spring boot(三) springboot 定时任务

    这个不多说,springboot 定时任务非常简单就可以实现了. 30s运行一次 , @Scheduled(cron="0,30 * * * * ?") 通过这个控制定时时间 cr ...

  3. springboot定时任务之旅

    springboot定时任务 假设场景:单体应用的定时任务,假设我们已经有了一个搭建好的springboot应用,但是需要添加一个定时执行的部分(比如笔者遇到的是定时去请求一个接口数据来更新某个表), ...

  4. SpringBoot定时任务@Scheduled

    SpringBoot定时任务主要由两个注解完成. @Scheduled加在方法上面. @EnableScheduling加在类上面.可以是Application类,也可以是@Component类,还可 ...

  5. 小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解

    笔记 1.SpringBoot定时任务schedule讲解     简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类            ...

  6. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  7. SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)

    最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...

  8. SpringBoot定时任务 - 什么是ElasticJob?如何集成ElasticJob实现分布式任务调度?

    前文展示quartz实现基于数据库的分布式任务管理和job生命周期的控制,那在分布式场景下如何解决弹性调度.资源管控.以及作业治理等呢?针对这些功能前当当团队开发了ElasticJob,2020 年 ...

  9. SpringBoot定时任务 - 开箱即用分布式任务框架xxl-job

    除了前文介绍的ElasticJob,xxl-job在很多中小公司有着应用(虽然其代码和设计等质量并不太高,License不够开放,有着个人主义色彩,但是其具体开箱使用的便捷性和功能相对完善性,这是中小 ...

  10. 带着新人学springboot的应用10(springboot+定时任务+发邮件)

    接上一节,环境一样,这次来说另外两个任务,一个是定时任务,一个是发邮件. 1.定时任务 定时任务可以设置精确到秒的准确时间去自动执行方法. 我要一个程序每一秒钟说一句:java小新人最帅 于是,我就写 ...

随机推荐

  1. “3+3”看华为云FusionInsight如何引领“数据新基建”持续发展

    摘要:一个统一的现代化的数据基建需要三类架构来实践三种不同的应用场景. 近期,美国知名科技企业风投机构A16Z总结出一套通用的技术架构服务,分为以下三种场景. 一.数据基建架构全景 数据流向显示,左侧 ...

  2. Socket 套接字的系统调用

    socket 结构 /** * struct socket - general BSD socket * @state: socket state (%SS_CONNECTED, etc) * @ty ...

  3. 一文带你玩转对象存储COS文档预览

    随着"互联网+"的发展,各行各业纷纷"去纸化",商务合同.会议纪要.组织公文.商品图片.培训视频.学习课件.随堂讲义等电子文档无处不在.而要查看文档一般需要先下 ...

  4. error while loading shared libraries解決方法

    在linux下运行程序时,发现了error while loading shared libraries这种错误,一时间不知道解决办法,在网上搜索,终于解决了. error while loading ...

  5. Qiskit 安装指南

    内容参考官方文档 https://qiskit.org/documentation/install.html conda create -n name_of_my_env python=3 创建虚拟环 ...

  6. guitar pro系列教程(三):Guitar Pro7乐谱页面显示模式设置

    大家好,又到了guitar pro系列教程的时间 本章节我们采用图文结合的方式为大家讲解一下guitar pro 7乐谱的页面显示设置,有兴趣的小伙伴都可以进来看看哦.首让我们先看下图: 如上图所示, ...

  7. symfony框架中使用service

    在config文件里面的service.yml写入自己service 1 chat.group_list: //service的名字 2 class: Chat\Service\GroupListSe ...

  8. 电脑adb命令给智能电视安装APK

    配置环境 1.电脑需要配置好adb系统环境 具体操作较复杂,请自行百度. 2.电视打开adb命令 在电视的关于界面通过遥控器「上上下下左右左右」进入工厂模式,在「高级设置」-「其他」中的「adb开关」 ...

  9. ubuntu解决安装速度问题

    速度慢得原因:linux系统很多的软件源链接都是在国外服务器上,由于国内防火墙影响导致下载速度极慢,甚至超时. 解决办法一:购买梯子 这样你就可以快速的下载国外服务器的软件包了,但是你得有个可靠得梯子 ...

  10. selenium+python自动化元素定位

    最近学习自动化测试,终于初步学习完成,需要进行博客日志总结,加深巩固自己的知识. 元素的八种定位方式 1.id 以百度为例子 我们在python输入的元素定位语法:bs.find_element_by ...