时间:2021-05-19
在我们开发项目过程中,经常需要定时任务来帮助我们来做一些内容, Spring Boot 默认已经帮我们实行了,只需要添加相应的注解就可以实现
pom 包里面只需要引入 Spring Boot Starter 包即可
<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>定时任务1:
@Configuration@EnableSchedulingpublic class SchedulerTask { private int count=0; @Scheduled(cron="*/6 * * * * ?") private void process(){ System.out.println("this is scheduler task runing "+(count++)); }}定时任务2:
@Configuration@EnableSchedulingpublic 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:17this is scheduler task runing 1现在时间:09:44:23this is scheduler task runing 2现在时间:09:44:29this is scheduler task runing 3现在时间:09:44:35@Configuration用于标记配置类,兼备Component的效果。
@EnableScheduling表示开启定时任务
@Scheduled 参数可以接受两种定时的设置,一种是我们常用的cron="*/6 * * * * ?",一种是 fixedRate = 6000,两种都表示每隔六秒打印一下内容。
fixedRate 说明
@Scheduled(fixedRate = 6000) :上一次开始执行时间点之后6秒再执行
@Scheduled(fixedDelay = 6000) :上一次执行完毕时间点之后6秒再执行
@Scheduled(initialDelay=1000, fixedRate=6000) :第一次延迟1秒后执行,之后按 fixedRate 的规则每6秒执行一次
cron属性,通过表达式形式指定任务执行规则,同样也是从上一次任务执行完毕开始计时。表达式规则如下:
每个位置的规则如下:
备注:
开启本地数据库mysql,随便打开查询窗口,然后执行脚本内容,如下:
DROP DATABASE IF EXISTS `socks`;CREATE DATABASE `socks`;USE `SOCKS`;DROP TABLE IF EXISTS `cron`;CREATE TABLE `cron` ( `cron_id` varchar(30) NOT NULL PRIMARY KEY, `cron` varchar(30) NOT NULL );INSERT INTO `cron` VALUES ('1', '0/5 * * * * ?');然后在application.properties文件添加数据源
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/socks?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=falsespring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver数据库准备好数据之后,我们编写定时任务,注意这里添加的是TriggerTask,目的是循环读取我们在数据库设置好的执行周期,以及执行相关定时任务的内容。 具体代码如下:
package com.cn.service;import com.cn.dao.CronMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import org.springframework.scheduling.support.CronTrigger;import org.springframework.util.StringUtils;import java.time.LocalDateTime;@Configuration //1.主要用于标记配置类,兼备Component的效果。@EnableScheduling // 2.开启定时任务public class DynamicScheduleTask implements SchedulingConfigurer { @Autowired private CronMapper cronMapper; /** * 执行定时任务. */ @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask( //1.添加任务内容(Runnable) () -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()), //2.设置执行周期(Trigger) triggerContext -> { //2.1 从数据库获取执行周期 String cron = cronMapper.selectByPrimaryKey("1").getCron(); //2.2 合法性校验. if (StringUtils.isEmpty(cron)) { // Omitted Code .. } //2.3 返回执行周期(Date) return new CronTrigger(cron).nextExecutionTime(triggerContext); } ); }}然后打开Navicat ,将执行周期修改为每10秒执行一次,查看控制台,发现执行周期已经改变,并且不需要我们重启应用,十分方便。如图:
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。
每隔5秒打印一次"hello quartz"
以上就是springboot定时任务详解的详细内容,更多关于springboot定时任务的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
前言好几天没写了,工作有点忙,最近工作刚好做一个定时任务统计的,所以就将springboot如何创建定时任务整理了一下。总的来说,springboot创建定时任
刚刚看了下SpringBoot实现定时任务的文章,感觉还不错。SpringBoot使用Spring自带的Schedule来实现定时任务变得非常简单和方便。在这里
@schedule注解是springboot常用的定时任务注解,使用起来简单方便,但是如果定时任务非常多,或者有的任务很耗时,会影响到其他定时任务的执行,因为s
在SpringBoot项目中,通过@EnableScheduling可启用Spring自带的定时任务支持,在通过@Scheduled注解定义定时任务,但是通过注
假设我们已经搭建好了一个基于SpringBoot项目,首先我们要在Application中设置启用定时任务功能@EnableScheduling。启动定时任务p