当前位置: 首页 > news >正文

Spring Boot + Spring Batch + Quartz 整合定时批量任务

 ​

 博客主页:     南来_北往

系列专栏:Spring Boot实战


前言

最近一周,被借调到其他部门,赶一个紧急需求,需求内容如下:

PC网页触发一条设备升级记录(下图),后台要定时批量设备更新。这里定时要用到Quartz,批量数据处理要用到SpringBatch,二者结合,可以完成该需求。

由于之前,没有用过SpringBatch,于是上网查了下资料,发现可参考的不是很多,于是只能去慢慢的翻看官方文档。

Spring Batch - Reference Documentation

具体实现

在你的pom.xml文件中添加以下依赖: 

<dependencies><!-- Spring Boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><!-- Quartz --><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency>
</dependencies>

 在application.properties文件中添加以下配置:

spring.quartz.job-store-type=memory
spring.quartz.properties.org.quartz.scheduler.instanceName=MyScheduler
spring.quartz.properties.org.quartz.threadPool.threadCount=5

 创建一个实现Job接口的类,例如MyBatchJob

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;public class MyBatchJob extends QuartzJobBean {@Autowiredprivate JobLauncher jobLauncher;@Overrideprotected void executeInternal(JobExecutionContext context) throws JobExecutionException {try {jobLauncher.run(myBatchJob(), new JobParametersBuilder().addString("JobID", String.valueOf(System.currentTimeMillis())).toJobParameters());} catch (Exception e) {throw new JobExecutionException(e);}}private Job myBatchJob() {// 返回你的Spring Batch Job实例}
}

 在你的配置类中(例如ApplicationConfig),添加一个SchedulerFactoryBean的Bean,用于配置定时任务的触发器:

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;@Configuration
public class ApplicationConfig {@Beanpublic JobDetail myBatchJobDetail() {return JobBuilder.newJob(MyBatchJob.class).withIdentity("myBatchJob").storeDurably().build();}@Beanpublic Trigger myBatchJobTrigger() {SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(60) // 设置任务执行间隔,例如每60秒执行一次.repeatForever(); // 设置任务重复执行return TriggerBuilder.newTrigger().forJob(myBatchJobDetail()).withIdentity("myBatchJobTrigger").withSchedule(scheduleBuilder).build();}@Beanpublic SchedulerFactoryBean schedulerFactoryBean() {SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();schedulerFactoryBean.setJobDetails(myBatchJobDetail());schedulerFactoryBean.setTriggers(myBatchJobTrigger());return schedulerFactoryBean;}
}

现在,你已经成功地整合了Spring Boot、Spring Batch和Quartz,实现了定时批量任务。每隔指定的时间间隔(例如60秒),MyBatchJob将会被执行一次。

http://www.lryc.cn/news/408245.html

相关文章:

  • C++STL简介(二)
  • 嵌入式高频面试题100道及参考答案(3万字长文)
  • python爬虫-事件触发机制
  • LeetCode-day27-3106. 满足距离约束且字典序最小的字符串
  • C++中的static_cast函数
  • 从零开始学习网络安全渗透测试之基础入门篇——(二)Web架构前后端分离站Docker容器站OSS存储负载均衡CDN加速反向代理WAF防护
  • 2679. 矩阵中的和
  • Unity Playables:下一代动画与音频序列
  • matlab仿真 模拟调制(下)
  • RabbitMQ是什么?
  • 追问试面试系列:分布式id
  • 护网紧急情况应对指南:Linux 应急响应手册
  • WEB攻防-通用漏洞-SQL 读写注入-MYSQLMSSQLPostgreSQL
  • 【前端学习笔记】CSS基础一
  • Github遇到的问题解决方法总结(持续更新...)
  • 数字信封+数字签名工具类测试样例(Java实现)
  • The Schematic workflow failed. See above.
  • 操作系统面试知识点总结4
  • Lua实现面向对象以及类的继承
  • 机器学习课程学习周报五
  • vue3.0学习笔记(二)——生命周期与响应式数据(ref,reactive,toRef,toRefs函数)
  • C++——QT:保姆级教程,从下载到安装到用QT写出第一个程序
  • 掌握互联网路由选择协议:从基础入门到实战
  • [笔记]ONVIF服务端实现[进行中...]
  • 深度强化学习 ②(DRL)
  • 线性代数重要知识点和理论(下)
  • 独立开发者系列(35)——python环境的理解
  • 中小企业常见的网络安全问题及防范措施
  • Android 线程并发:线程通信:Handler机制
  • 搭建自己的金融数据源和量化分析平台(三):读取深交所股票列表