Spring Boot 定时任务与调度系统

Spring Boot 定时任务与调度系统

Spring Boot 定时任务与调度系统

在这里插入图片描述
29.1 学习目标与重点提示

学习目标:掌握Spring Boot定时任务与调度系统的核心概念与使用方法,包括定时任务的定义与特点、调度系统的定义与特点、Spring Boot与定时任务的集成、Spring Boot的实际应用场景,学会在实际开发中处理定时任务与调度系统问题。
重点:定时任务的定义与特点调度系统的定义与特点Spring Boot与定时任务的集成Spring Boot的实际应用场景

29.2 定时任务与调度系统概述

定时任务与调度系统是Java开发中的重要组件。

29.2.1 定时任务的定义

定义:定时任务是指按照预定的时间间隔或时间点执行的任务,用于处理周期性的业务逻辑。
作用

  • 提高系统的自动化程度。
  • 减少人工干预。
  • 提高系统的可靠性。

常见的定时任务

  • 数据库备份。
  • 数据清洗。
  • 报表生成。
  • 邮件发送。

✅ 结论:定时任务是指按照预定的时间间隔或时间点执行的任务,作用是提高系统的自动化程度、减少人工干预、提高系统的可靠性。

29.2.2 调度系统的定义

定义:调度系统是一种用于管理和调度定时任务的系统,用于监控任务的执行情况、处理任务的失败和重试。
作用

  • 实现任务的调度。
  • 实现任务的监控。
  • 实现任务的失败和重试。

常见的调度系统

  • Apache Airflow:Apache Airflow是一种开源的调度系统。
  • Quartz:Quartz是一种开源的调度系统。
  • Spring Task:Spring Task是Spring Boot内置的定时任务调度系统。
  • Elastic Job:Elastic Job是一种开源的调度系统。

✅ 结论:调度系统是一种用于管理和调度定时任务的系统,作用是实现任务的调度、监控、失败和重试。

29.3 Spring Boot与定时任务的集成

Spring Boot与定时任务的集成是Java开发中的重要内容。

29.3.1 集成Spring Task的步骤

定义:集成Spring Task的步骤是指使用Spring Boot与Spring Task集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置定时任务。
  4. 创建定时任务类。
  5. 测试应用。

示例
pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties文件中的配置:

# 服务器端口 server.port=8080 

定时任务类:

importorg.springframework.scheduling.annotation.Scheduled;importorg.springframework.stereotype.Component;importjava.text.SimpleDateFormat;importjava.util.Date;@ComponentpublicclassProductScheduledTask{privateSimpleDateFormat dateFormat =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Scheduled(fixedRate =5000)// 每隔5秒执行一次publicvoidfixedRateTask(){System.out.println("固定频率任务执行时间:"+ dateFormat.format(newDate()));// 处理固定频率任务的业务逻辑processFixedRateTask();}@Scheduled(fixedDelay =5000)// 任务完成后间隔5秒执行下一次publicvoidfixedDelayTask(){System.out.println("固定延迟任务执行时间:"+ dateFormat.format(newDate()));// 处理固定延迟任务的业务逻辑processFixedDelayTask();}@Scheduled(cron ="0 0/1 * * * ?")// 每分钟执行一次publicvoidcronTask(){System.out.println("Cron任务执行时间:"+ dateFormat.format(newDate()));// 处理Cron任务的业务逻辑processCronTask();}privatevoidprocessFixedRateTask(){// 模拟处理固定频率任务的业务逻辑try{Thread.sleep(2000);System.out.println("固定频率任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}privatevoidprocessFixedDelayTask(){// 模拟处理固定延迟任务的业务逻辑try{Thread.sleep(2000);System.out.println("固定延迟任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}privatevoidprocessCronTask(){// 模拟处理Cron任务的业务逻辑try{Thread.sleep(2000);System.out.println("Cron任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}}

应用启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableSchedulingpublicclassScheduledTaskApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ScheduledTaskApplication.class, args);}}

测试类:

importorg.junit.jupiter.api.Test;importorg.springframework.boot.test.context.SpringBootTest;importstaticorg.assertj.core.api.Assertions.assertThat;@SpringBootTestclassScheduledTaskApplicationTests{@TestvoidcontextLoads(){assertThat(true).isTrue();}}

✅ 结论:集成Spring Task的步骤包括创建Spring Boot项目、添加所需的依赖、配置定时任务、创建定时任务类、测试应用。

29.3.2 集成Quartz的步骤

定义:集成Quartz的步骤是指使用Spring Boot与Quartz集成的方法。
步骤

  1. 创建Spring Boot项目。
  2. 添加所需的依赖。
  3. 配置Quartz。
  4. 创建定时任务类。
  5. 测试应用。

示例
pom.xml文件中的依赖:

<dependencies><!-- Web依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Quartz依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency><!-- 测试依赖 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>

application.properties文件中的配置:

# 服务器端口 server.port=8080 # Quartz配置 spring.quartz.job-store-type=jdbc spring.quartz.jdbc.initialize-schema=always spring.quartz.scheduler-name=MyScheduler spring.quartz.properties.org.quartz.threadPool.threadCount=10 

定时任务类:

importorg.quartz.Job;importorg.quartz.JobExecutionContext;importorg.quartz.JobExecutionException;importorg.springframework.stereotype.Component;importjava.text.SimpleDateFormat;importjava.util.Date;@ComponentpublicclassProductQuartzJobimplementsJob{privateSimpleDateFormat dateFormat =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Overridepublicvoidexecute(JobExecutionContext jobExecutionContext)throwsJobExecutionException{System.out.println("Quartz任务执行时间:"+ dateFormat.format(newDate()));// 处理Quartz任务的业务逻辑processQuartzTask();}privatevoidprocessQuartzTask(){// 模拟处理Quartz任务的业务逻辑try{Thread.sleep(2000);System.out.println("Quartz任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}}

Quartz配置类:

importorg.quartz.*;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassQuartzConfig{@BeanpublicJobDetailproductJobDetail(){returnJobBuilder.newJob(ProductQuartzJob.class).withIdentity("productJob").storeDurably().build();}@BeanpublicTriggerproductTrigger(){returnTriggerBuilder.newTrigger().forJob(productJobDetail()).withIdentity("productTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?"))// 每分钟执行一次.build();}}

控制器类:

importorg.quartz.*;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;@RestController@RequestMapping("/api/quartz")publicclassQuartzController{@AutowiredprivateScheduler scheduler;@PostMapping("/start")publicStringstartJob(){try{JobDetail jobDetail =productJobDetail();Trigger trigger =productTrigger(); scheduler.scheduleJob(jobDetail, trigger);return"任务已启动";}catch(SchedulerException e){ e.printStackTrace();return"任务启动失败";}}@PostMapping("/stop")publicStringstopJob(){try{TriggerKey triggerKey =newTriggerKey("productTrigger"); scheduler.pauseTrigger(triggerKey);JobKey jobKey =newJobKey("productJob"); scheduler.deleteJob(jobKey);return"任务已停止";}catch(SchedulerException e){ e.printStackTrace();return"任务停止失败";}}privateJobDetailproductJobDetail(){returnJobBuilder.newJob(ProductQuartzJob.class).withIdentity("productJob").storeDurably().build();}privateTriggerproductTrigger(){returnTriggerBuilder.newTrigger().forJob(productJobDetail()).withIdentity("productTrigger").withSchedule(CronScheduleBuilder.cronSchedule("0 0/1 * * * ?")).build();}}

应用启动类:

importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublicclassQuartzApplication{publicstaticvoidmain(String[] args){SpringApplication.run(QuartzApplication.class, args);}}

测试类:

importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.boot.test.web.client.TestRestTemplate;importorg.springframework.boot.web.server.LocalServerPort;importstaticorg.assertj.core.api.Assertions.assertThat;@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)classQuartzApplicationTests{@LocalServerPortprivateint port;@AutowiredprivateTestRestTemplate restTemplate;@TestvoidcontextLoads(){}@TestvoidtestStartJob(){String response = restTemplate.postForObject("http://localhost:"+ port +"/api/quartz/start",null,String.class);assertThat(response).contains("任务已启动");}@TestvoidtestStopJob(){String response = restTemplate.postForObject("http://localhost:"+ port +"/api/quartz/stop",null,String.class);assertThat(response).contains("任务已停止");}}

✅ 结论:集成Quartz的步骤包括创建Spring Boot项目、添加所需的依赖、配置Quartz、创建定时任务类、测试应用。

29.4 Spring Boot的实际应用场景

在实际开发中,Spring Boot定时任务与调度系统的应用场景非常广泛,如:

  • 实现数据库备份的定时任务。
  • 实现数据清洗的定时任务。
  • 实现报表生成的定时任务。
  • 实现邮件发送的定时任务。

示例

importorg.springframework.scheduling.annotation.Scheduled;importorg.springframework.stereotype.Component;importjava.text.SimpleDateFormat;importjava.util.Date;@ComponentclassProductScheduledTask{privateSimpleDateFormat dateFormat =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Scheduled(fixedRate =5000)publicvoidfixedRateTask(){System.out.println("固定频率任务执行时间:"+ dateFormat.format(newDate()));processFixedRateTask();}@Scheduled(fixedDelay =5000)publicvoidfixedDelayTask(){System.out.println("固定延迟任务执行时间:"+ dateFormat.format(newDate()));processFixedDelayTask();}@Scheduled(cron ="0 0/1 * * * ?")publicvoidcronTask(){System.out.println("Cron任务执行时间:"+ dateFormat.format(newDate()));processCronTask();}privatevoidprocessFixedRateTask(){try{Thread.sleep(2000);System.out.println("固定频率任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}privatevoidprocessFixedDelayTask(){try{Thread.sleep(2000);System.out.println("固定延迟任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}privatevoidprocessCronTask(){try{Thread.sleep(2000);System.out.println("Cron任务处理完成:"+ dateFormat.format(newDate()));}catch(InterruptedException e){ e.printStackTrace();}}}@SpringBootApplication@EnableSchedulingpublicclassScheduledTaskApplication{publicstaticvoidmain(String[] args){SpringApplication.run(ScheduledTaskApplication.class, args);}}// 测试类@SpringBootTestclassScheduledTaskApplicationTests{@TestvoidcontextLoads(){assertThat(true).isTrue();}}

输出结果

  • 控制台输出:
    固定频率任务执行时间:2023-06-01 10:00:00
    固定频率任务处理完成:2023-06-01 10:00:02
    固定延迟任务执行时间:2023-06-01 10:00:02
    固定延迟任务处理完成:2023-06-01 10:00:04
    Cron任务执行时间:2023-06-01 10:01:00
    Cron任务处理完成:2023-06-01 10:01:02

✅ 结论:在实际开发中,Spring Boot定时任务与调度系统的应用场景非常广泛,需要根据实际问题选择合适的定时任务和调度系统。

总结

本章我们学习了Spring Boot定时任务与调度系统,包括定时任务的定义与特点、调度系统的定义与特点、Spring Boot与定时任务的集成、Spring Boot的实际应用场景,学会了在实际开发中处理定时任务与调度系统问题。其中,定时任务的定义与特点、调度系统的定义与特点、Spring Boot与定时任务的集成、Spring Boot的实际应用场景是本章的重点内容。从下一章开始,我们将学习Spring Boot的其他组件、微服务等内容。

Read more

Web 毕设篇-适合练手的 Spring Boot Web 毕业设计项目:智驿AI系统(前后端源码 + 数据库 sql 脚本)

Web 毕设篇-适合练手的 Spring Boot Web 毕业设计项目:智驿AI系统(前后端源码 + 数据库 sql 脚本)

🔥博客主页: 【小扳_-ZEEKLOG博客】 ❤感谢大家点赞👍收藏⭐评论✍ 文章目录         AI系统具有许多优势         1.0 项目介绍         1.1 项目功能         1.2 用户端功能         2.0 用户登录         3.0 首页界面         4.0 物件管理功能         5.0 用户管理功能         6.0 区域管理功能         7.0 物件日志管理功能         8.0 操作日志         AI系统具有许多优势         1)自动化:AI 系统能够自动化执行任务,减少人力和时间成本。它们可以自动处理大量数据并执行复杂的计算,从而提高效率。         2)智能决策:AI 系统可以通过学习和分析数据来做出智能决策。

Ostrakon-VL-8B实战手册:将Chainlit前端嵌入企业内网OA系统操作指南

Ostrakon-VL-8B实战手册:将Chainlit前端嵌入企业内网OA系统操作指南 1. 引言:当零售AI专家走进企业内网 想象一下这个场景:你是一家连锁零售企业的IT负责人,每天都要处理来自各个门店的海量图片数据——商品陈列照片、库存盘点图、店面环境照、促销活动素材。这些图片里藏着宝贵的信息:哪些商品卖得好、陈列是否规范、库存是否充足、促销效果如何。 以前,要分析这些图片,要么靠人工一张张看,效率低下还容易出错;要么用通用AI工具,但效果总是不尽如人意——毕竟零售场景太特殊了,通用模型很难理解“货架饱满度”、“商品保质期”、“陈列合规性”这些专业概念。 现在,有了Ostrakon-VL-8B,情况完全不一样了。这是首个专门为食品服务和零售商店(FSRS)场景打造的多模态大语言模型,就像一个在零售行业干了十年的专家,看一眼图片就能告诉你: * 这家店的货架陈列符不符合标准 * 哪些商品快过期了需要优先处理 * 促销海报的设计有没有问题 * 店面环境是否整洁安全 更棒的是,这个8B参数的模型在零售专业任务上的表现,甚至超过了那些235B参数的通用大模型——用更

让 Typecho 拥抱 WebAuthn 无密码时代

让 Typecho 拥抱 WebAuthn 无密码时代

Passkey 生物识别登录插件:让 Typecho 拥抱 WebAuthn 无密码时代 摘要:本文深入介绍基于 FIDO2/WebAuthn 标准的 Typecho 生物识别登录插件 Passkey v1.0.2,从技术原理、架构设计、安全机制到实战部署,全方位解析如何为 Typecho 博客系统构建现代化的无密码认证解决方案。 一、项目背景:为什么需要 Passkey? 1.1 传统密码认证的困境 在互联网安全领域,密码认证一直是最常用但也是最脆弱的环节: 传统密码问题 弱密码 密码泄露 钓鱼攻击 暴力破解 撞库攻击 容易被猜测 数据库泄露 用户被欺骗 尝试常见密码 用其他站点密码 统计数据表明: * 📊 81% 的数据泄露事件源于弱密码或被盗密码 * 🔐 普通用户平均拥有 100+ 个账户,

ChatGPT Web Share 效率提升实战:从 API 优化到生产环境部署

ChatGPT Web Share 效率提升实战:从 API 优化到生产环境部署 在将 ChatGPT 能力集成到 Web 应用并开放共享(ChatGPT Web Share)的过程中,我们很快遇到了一个典型的技术挑战:当用户量增长,并发请求涌入时,系统响应延迟显著增加,吞吐量急剧下降,甚至出现服务不稳定和超时的情况。这直接影响了用户体验和服务的可用性。本文将分享我们如何通过一系列技术优化,将系统吞吐量提升了 3 倍,并构建出稳定、高效的生产级集成方案。 1. 背景与痛点分析 在初始的单体架构中,我们的 ChatGPT Web Share 服务直接为每个前端请求调用一次 OpenAI 的 Chat Completions API。这种模式在低并发下表现尚可,但随着用户增长,问题迅速暴露: * 高延迟与低吞吐量:每个请求都需要独立建立 HTTPS 连接、传输数据并等待