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

Java:SpringBoot整合Spring Batch示例

在这里插入图片描述

目录

    • 文档
    • 基础概念
    • Tasklet方式示例
    • Chunk方式示例
    • 参考文章

文档

  • https://docs.spring.io/spring-batch/docs/4.3.9/reference/html/index.html

基础概念

  • JobLauncher:作业启动器,启动作业的入口。对应的实现类为SimpleJobLauncher。
  • Job:作业,用于配置作业的相关配置,一个作业可以配置多个步骤,步骤之间是有序的。
  • Step:步骤,作业具体执行的业务逻辑,一个Job可以配置多个Step。步骤有两种实现方式:
    • Tasklet方式:所有作业逻辑写在一个方法中。
    • Chunk方式:将一个完整的作业逻辑根据作用拆分到三个方法中
      • ItemReader:负责从数据源中读数据(如从文件、数据库等)。
      • ItemProcessor :负责对读出来的数据进行非法校验和对数据进行加工。
      • ItemWriter:将数据写到某个目标中(如文件、数据库等)。
  • JobBuilderFactory:作业构建起工厂,用于构建作业Job对象。
    • get(String name):设置作业名称。
    • start(Step step):设置作业启动的第一个步骤。
    • build():构建Job对象。
  • StepBuilderFactory:作业构建器工厂,用于构造步骤Step对象。
    • get(String name):设置步骤名称。
    • tasklet(Tasklet tasklet):设置Tasklet。
    • build():构建Step对象。
  • JobRepository:作业持久化,在执行作业的过程中用于操作spring batch相关的表,记录作业的相关状态等。

Tasklet方式示例

运行环境

$ java -version
java version "1.8.0_361"
Java(TM) SE Runtime Environment (build 1.8.0_361-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)

spring-batch依赖

<!-- spring-batch -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId>
</dependency><dependency><groupId>org.springframework.batch</groupId><artifactId>spring-batch-test</artifactId><scope>test</scope>
</dependency><!-- h2、mysql... -->
<dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope>
</dependency>

备注:第一次照着网上的文章写代码运行,直接报错,原因是缺少引入的依赖;需要引入数据库驱动,如H2或mysql

项目结构

$ tree 
.
├── pom.xml
└── src└── main├── java│   └── com│       └── example│           └── demo│               ├── HelloWorldApplication.java│               ├── config│               │   └── BatchConfig.java│               └── task│                   └── HelloWorldTasklet.java└── resources

完整依赖pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.7</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version><mybatis-plus.version>3.5.2</mybatis-plus.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- spring-batch --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-batch</artifactId></dependency><dependency><groupId>org.springframework.batch</groupId><artifactId>spring-batch-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><!-- test --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

编写业务逻辑

package com.example.demo.task;import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.stereotype.Component;/*** 任务*/
@Component
public class HelloWorldTasklet implements Tasklet {@Overridepublic RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {System.out.println("Hello, World!");return RepeatStatus.FINISHED;}
}

配置作业步骤

package com.example.demo.config;import com.example.demo.task.HelloWorldTasklet;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Spring Batch配置*/
@Configuration
public class BatchConfig {@Autowiredprivate JobBuilderFactory jobBuilderFactory;@Autowiredprivate StepBuilderFactory stepBuilderFactory;@Autowiredprivate HelloWorldTasklet helloWorldTasklet;@Beanpublic Step step() {return this.stepBuilderFactory.get("step").tasklet(helloWorldTasklet).build();}@Beanpublic Job job(Step step) {return this.jobBuilderFactory.get("job").start(step).build();}
}

启动类增加注解:@EnableBatchProcessing

package com.example.demo;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableBatchProcessing
@SpringBootApplication
public class HelloWorldApplication {public static void main(String[] args) {SpringApplication.run(HelloWorldApplication.class, args);}}

Chunk方式示例

项目结构

$ tree
.
├── pom.xml
└── src└── main├── java│   └── com│       └── example│           └── demo│               ├── Application.java│               ├── config│               │   └── BatchConfig.java│               ├── dto│               │   └── Person.java│               └── processor│                   └── PersonItemProcessor.java└── resources└── persons.csv

依赖pom.xml 同上

package com.example.demo.dto;import lombok.Data;@Data
public class Person {private String name;private Integer age;
}
package com.example.demo.config;import com.example.demo.dto.Person;
import com.example.demo.processor.PersonItemProcessor;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.json.JacksonJsonObjectMarshaller;
import org.springframework.batch.item.json.JsonFileItemWriter;
import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;/*** Spring Batch配置*/
@Configuration
public class BatchConfig {@Autowiredprivate JobBuilderFactory jobBuilderFactory;@Autowiredprivate StepBuilderFactory stepBuilderFactory;@Beanpublic Step step() {return this.stepBuilderFactory.get("step").<Person, Person>chunk(10).reader(reader()).processor(processor()).writer(writer()).build();}@Beanpublic Job job(Step step) {return this.jobBuilderFactory.get("job").start(step).build();}/*** 读取csv文件* @return*/@Beanpublic FlatFileItemReader<Person> reader() {return new FlatFileItemReaderBuilder<Person>().name("personItemReader").resource(new ClassPathResource("persons.csv")).delimited().names(new String[] {"name", "age"}).targetType(Person.class).build();}/*** 处理器* @return*/@Beanpublic PersonItemProcessor processor() {return new PersonItemProcessor();}/*** 写到json文件* @return*/@Beanpublic JsonFileItemWriter<Person> writer() {return new JsonFileItemWriterBuilder<Person>().jsonObjectMarshaller(new JacksonJsonObjectMarshaller<>()).resource(new FileSystemResource("persons.json")).name("personItemWriter").build();}}
package com.example.demo.processor;import com.example.demo.dto.Person;
import org.springframework.batch.item.ItemProcessor;public class PersonItemProcessor implements ItemProcessor<Person, Person> {@Overridepublic Person process(Person person) throws Exception {// 为每个对象年龄+1person.setAge(person.getAge() + 1);return person;}
}

启动入口

package com.example.demo;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableBatchProcessing
@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

启动SpringBoot即可运行任务

读取的persons.csv文件

Tom,18
Jack,20

生成的persons.json 文件

[{"name":"Tom","age":19},{"name":"Jack","age":21}
]

参考文章

  1. SpringBatch从入门到实战(二):HelloWorld
  2. Spring Batch 之 Hello World
  3. Spring Batch 入门级示例教程
  4. Spring Batch 批处理框架优化实践,效率嘎嘎高!

相关文章:

Java:SpringBoot整合Spring Batch示例

目录 文档基础概念Tasklet方式示例Chunk方式示例参考文章 文档 https://docs.spring.io/spring-batch/docs/4.3.9/reference/html/index.html 基础概念 JobLauncher&#xff1a;作业启动器&#xff0c;启动作业的入口。对应的实现类为SimpleJobLauncher。Job&#xff1a;作业…...

Windows + Msys 下编译 TensorFlow 2.14

安装基本工具 pacman -S --needed zip unzip patch diffutils git 下载安装 Windows 版本 bazel 6.1.2&#xff0c;复制到 C:/Windows/system32 目录下&#xff0c;改名为 bazel.exe wget https://github.com/bazelbuild/bazel/releases/download/6.1.2/bazel-6.1.2-window…...

百度发布全新 AI 互动式搜索:百度简单搜索

本心、输入输出、结果 文章目录 百度发布全新 AI 互动式搜索&#xff1a;百度简单搜索前言主要能力 相关资料能力介绍 百度搜索升级发文告用户如何获取百度简单搜索百度简单搜索的定位百度简单搜索在 APP 上面的体验讨论和点评我们关注的几个问题 弘扬爱国精神 百度发布全新 AI…...

VUE开发记录

记录vue开发中遇到的问题 - 2023/10/16 问题&#xff1a;项目element-ui表单中&#xff0c;input点击需要打开弹框&#xff0c;弹框选择值后回填到input&#xff0c;但是此时elementUI的校验出错&#xff08;值存在却校验为空&#xff09; 解决方法&#xff1a; this.employee…...

2023年中国乳胶制品产量、需求量及市场规模分析[图]

乳胶泛指聚合物微粒分散于水中形成的胶体乳液&#xff0c;又称胶乳。习惯上将橡胶微粒的水分散体称为胶乳&#xff0c;而将树脂微粒的水分散体称为乳液。以乳胶为原料制成的制品称乳胶制品&#xff0c;常见的如海绵、手套、玩具、胶管等。 我国乳胶制品细分主要分为避孕套、乳胶…...

手撕Vue-数据驱动界面改变上

经过上一篇的介绍&#xff0c;已经实现了监听数据的变化&#xff0c;接下来就是要实现数据变化后&#xff0c;界面也跟着变化&#xff0c;这就是数据驱动界面改变。 想要实现数据变化之后更新UI界面&#xff0c;我们可以使用发布订阅模式来实现&#xff0c;先定义一个观察者类,…...

for循环中循环一次提交一次 insert update 关闭事务 spring springboot mybatis

省流&#xff1a; 在方法上直接加如下注解&#xff1a; Transactional(propagation Propagation.NOT_SUPPORTED) public void t1(){//业务代码 } 正文&#xff1a; 在测试的时候&#xff0c;有时候会希望在for循环中&#xff0c;代码循环一次就提交一次事务。 方法一&#…...

VS2010 C语言内嵌汇编语言程序

VS2010 C语言内嵌汇编语言程序 2021年7月28日席锦 在visual studio 2010中C语言使用内联汇编写代码 &#xff0c;它的格式有两种&#xff0c; 一种是__asm 直接接汇编指令语句&#xff0c;比如:__asm int 3 // 软件中断 另一种是加上花括号&#xff0c;类似于一个函数&…...

【TES720D】青翼科技基于复旦微的FMQL20S400全国产化ARM核心模块

板卡概述 TES720D是一款基于上海复旦微电子FMQL20S400的全国产化核心模块。该核心模块将复旦微的FMQL20S400&#xff08;兼容FMQL10S400&#xff09;的最小系统集成在了一个50*70mm的核心板上&#xff0c;可以作为一个核心模块&#xff0c;进行功能性扩展&#xff0c;特别是用…...

css 左右滚轮无缝衔接

最近的项目有做到一个功能 类似跑马灯或者公告栏那种 有文字 也有列表的 所以 写了两种 第一种公告栏文字是用的js 第二种图文类型是用的css 两种方法 记录一下 第一种 纯文字滚动 其实也是根据js去计算dom的宽度 通过js去给css赋值 <div class"div1"><div …...

Hadoop分布式文件系统-HDFS

1.介绍 HDFS (Hadoop Distributed File System)是 Hadoop 下的分布式文件系统,具有高容错、高吞吐量等特性,可以部署在低成本的硬件上。 2.HDFS 设计原理 2.1 HDFS 架构 HDFS 遵循主/从架构,由单个 NameNode(NN) 和多个 DataNode(DN) 组成:...

专业图表绘制软件 OmniGraffle Pro mac v7.22.1中文版软件介绍

OmniGraffle Pro mac是一款Mac平台上的专业绘图软件&#xff0c;主要用于创建各种图形&#xff0c;包括流程图、组织结构图、网络拓扑图、UI原型等。该软件提供了强大的绘图工具和丰富的样式库&#xff0c;可以让用户快速创建出高质量的图形&#xff0c;并支持导入和导出各种常…...

Git 本地文件合并和恢复

前记&#xff1a; git svn sourcetree gitee github gitlab gitblit gitbucket gitolite gogs 版本控制 | 仓库管理 ---- 系列工程笔记. Platform&#xff1a;Windows 10 Git version&#xff1a;git version 2.32.0.windows.1 Function&#xff1a; Git 本地文件合并和恢复…...

记录git仓库pr没有显示贡献者的问题,以及如何提交一个pr(简)

git config --global --list # 查看全局配置&#xff08;适用于所有仓库&#xff09;的信息&#xff0c;可以添加 --global 标志git config --list # 查看你的Git配置git config user.name # 显示您的Git用户名。同样&#xff0c;可以替换 user.name 为其他配置项名称来查看特定…...

入侵检测代码

在人工智能中有个入侵检测&#xff1a;当检测到的目标位于指定区域内才算是入侵&#xff0c;思路很简单&#xff0c;判断相关坐标即可&#xff1a; from matplotlib import pyplot as plt, patches from shapely.geometry import Polygon, Pointdef is_intrusion(target_box, …...

数字孪生技术如何提高化工生产安全性?

随着科技的不断进步&#xff0c;数字孪生技术已经渗透到了各个领域&#xff0c;为化工行业带来了翻天覆地的变革。这一技术的应用不仅在生产效率方面发挥了积极作用&#xff0c;还在安全性、创新、环保和可持续性等多个方面作出了巨大的贡献。 化工行业常常涉及危险品和复杂的生…...

PHP 如何查看php函数源码

一、在git找到php对应的版本 找到对应的分支版本可以下载也可以在线直接查看 通过这个地址 https://github.com/php/php-src 二、下面已shuffle函数举例&#xff0c;版本为7.4 找到对应的版本进入 点击ext&#xff0c;这个文件夹里面是存放函数的目录 在文件夹里搜不到stu…...

前端web自动化测试:selenium怎么实现关键字驱动

要做 ui 自动化测试&#xff0c;使用关键字驱动可以说是必须会的一种测试方式&#xff0c;它既可以在纯代码的自动化程序中运行&#xff0c;也可以在测试平台中使用。 使用纯代码方式时&#xff0c;自动化工程师先写好一个通用的程序&#xff0c;其他手工测试人员只需要把执行…...

C++标准模板(STL)- 类型支持 (数值极限,min,lowest,max)

数值极限 提供查询所有基础数值类型的性质的接口 定义于头文件 <limits> template< class T > class numeric_limits; numeric_limits 类模板提供查询各种算术类型属性的标准化方式&#xff08;例如 int 类型的最大可能值是 std::numeric_limits<int>::m…...

国际SPEC CPU创榜以来整机最高纪录!浪潮信息八路服务器TS860G7刷新权威算力基准评测性能

近日&#xff0c;国际标准性能评估组织SPEC发布新一轮SPEC CPU2017通用算力性能测试榜单&#xff0c;浪潮信息八路服务器TS860G7以3940分获得SPEC CPU创榜以来整机性能最佳成绩&#xff0c;打破了单系统服务器性能世界纪录&#xff0c;较之前的测试最高分提升10%。 SPEC CPU201…...

G-Helper终极指南:5分钟精通华硕笔记本性能调校

G-Helper终极指南&#xff1a;5分钟精通华硕笔记本性能调校 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, TUF, Strix, Scar, an…...

GLM-OCR硬件优化指南:为GPU部署调整显存与算力配置

GLM-OCR硬件优化指南&#xff1a;为GPU部署调整显存与算力配置 如果你正在尝试部署GLM-OCR模型&#xff0c;是不是也遇到过这样的困惑&#xff1a;明明选了看起来不错的GPU&#xff0c;但推理时要么爆显存&#xff0c;要么速度慢得让人着急&#xff0c;钱花了效果却没达到预期…...

Settingator:嵌入式参数管理库的轻量级设计与实践

1. Settingator 库概述&#xff1a;嵌入式设备与移动端配置协同的工程实践Settingator 是一个面向嵌入式系统的轻量级 Arduino 兼容库&#xff0c;其核心目标并非提供通用通信协议栈&#xff0c;而是构建一套可验证、可回滚、低侵入的运行时参数管理机制&#xff0c;专为配合同…...

PyTorch 3.0静态图分布式训练落地实录:从模型编译失败到千卡吞吐提升3.8倍,我踩过的11个致命坑

第一章&#xff1a;PyTorch 3.0静态图分布式训练落地实录&#xff1a;从模型编译失败到千卡吞吐提升3.8倍在 PyTorch 3.0 正式引入 torch.compile() 与 torch.distributed._composable 协同优化的静态图分布式训练范式后&#xff0c;我们于千卡规模集群&#xff08;A100-80GB …...

SEO数据分析资源网

SEO数据分析资源网&#xff1a;揭秘成功的关键 在当前数字化竞争日益激烈的环境中&#xff0c;SEO&#xff08;搜索引擎优化&#xff09;已经成为企业提升在线可见度和吸引客户的重要手段。SEO并不是一蹴而就的事情&#xff0c;而是需要不断的数据分析和调整。今天&#xff0c…...

Docker镜像推送到私有仓库完整指南:从命名规范到AWS ECR实战

镜像构建好了&#xff0c;放在本地只有自己能看见。团队其他人怎么用&#xff1f;部署服务器怎么拉&#xff1f;你需要一个私有镜像仓库。今天这篇文章&#xff0c;我们用AWS ECR&#xff08;Elastic Container Registry&#xff09;做例子&#xff0c;从创建仓库到推送镜像&am…...

基于DSP28335逆变器程序,单相全桥逆变器程序,采用双极性调制 程序逻辑清晰,注释详细,详...

基于DSP28335逆变器程序&#xff0c;单相全桥逆变器程序&#xff0c;采用双极性调制 程序逻辑清晰&#xff0c;注释详细&#xff0c;详细到几乎每一句都有注释&#xff0c;对于小白异常友好&#xff0c;有些地方甚至基本原理都补充写明了&#xff0c;百分之99的程序注释不会有我…...

SAE J1850 CRC-8算法详解:如何在嵌入式系统中高效实现

SAE J1850 CRC-8算法在嵌入式系统中的极致优化实践 在汽车电子和工业控制领域&#xff0c;数据通信的可靠性直接关系到系统安全。SAE J1850标准中定义的CRC-8校验算法因其高效性和可靠性&#xff0c;成为CAN总线等嵌入式通信系统的首选校验方案。不同于通用教程&#xff0c;本文…...

Whisper JAX时间戳功能:为语音内容添加精准时间标记的终极指南

Whisper JAX时间戳功能&#xff1a;为语音内容添加精准时间标记的终极指南 【免费下载链接】whisper-jax JAX implementation of OpenAIs Whisper model for up to 70x speed-up on TPU. 项目地址: https://gitcode.com/gh_mirrors/wh/whisper-jax Whisper JAX是OpenAI …...

rustaceanvim 代码操作与宏扩展:提升 Rust 开发效率的实用方法

rustaceanvim 代码操作与宏扩展&#xff1a;提升 Rust 开发效率的实用方法 【免费下载链接】rustaceanvim &#x1f980; Supercharge your Rust experience in Neovim! A heavily modified fork of rust-tools.nvim 项目地址: https://gitcode.com/gh_mirrors/ru/rustaceanv…...