欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > Spring Batch 专题系列(四):配置与调度 Spring Batch 作业

Spring Batch 专题系列(四):配置与调度 Spring Batch 作业

2025/5/19 13:40:15 来源:https://blog.csdn.net/yinlongfei_love/article/details/147252939  浏览:    关键词:Spring Batch 专题系列(四):配置与调度 Spring Batch 作业

1. 引言

在上一篇文章中,我们详细探讨了 Spring Batch 的核心组件(Job、Step、Chunk、ItemReader、ItemProcessor、ItemWriter),并通过示例展示了它们的协作方式。掌握了这些组件后,接下来需要了解如何灵活配置 Spring Batch 作业,并通过调度机制控制作业的执行时机。本文将聚焦以下内容:

  • Spring Batch 的配置方式:XML 配置和 Java 配置的对比与实现。
  • JobParameters 的定义和使用,用于动态传递运行时参数。
  • 调度 Spring Batch 作业:使用 Spring Scheduler、Quartz 或手动触发。
  • 通过代码示例和 Mermaid 图表展示配置和调度的完整流程。

通过本文,你将学会如何根据项目需求配置 Spring Batch 作业,并实现定时或手动触发,为生产环境部署奠定基础。

2. Spring Batch 配置方式

Spring Batch 支持两种主要配置方式:XML 配置Java 配置。Java 配置因其类型安全和现代化特性在 Spring Boot 项目中更常见,但 XML 配置在遗留系统或特定场景中仍有使用价值。以下分别介绍这两种方式。

2.1 Java 配置

Java 配置使用 Spring 的 @Configuration 注解和流式 API(如 JobBuilderStepBuilder)定义 Job 和 Step。上一篇文章的示例已展示了 Java 配置,这里回顾并扩展一个更复杂的配置。

示例:Java 配置多 Step 作业

package com.example.springbatchdemo.config;import com.example.springbatchdemo.entity.Product;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.PlatformTransactionManager;import javax.sql.DataSource;@Configuration
public class BatchConfiguration {@Beanpublic FlatFileItemReader<Product> reader() {return new FlatFileItemReaderBuilder<Product>().name("productReader").resource(new ClassPathResource("products.csv")).delimited().names("id", "name", "price").targetType(Product.class).build();}@Beanpublic ProductItemProcessor processor() {return new ProductItemProcessor();}@Beanpublic JdbcBatchItemWriter<Product> writer(DataSource dataSource) {return new JdbcBatchItemWriterBuilder<Product>().sql("INSERT INTO product (id, name, price) VALUES (:id, :name, :price)").dataSource(dataSource).beanMapped().build();}@Beanpublic Step importStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {return new StepBuilder("importStep", jobRepository).<Product, Product>chunk(10).reader(reader()).processor(processor()).writer(writer(dataSource)).transactionManager(transactionManager).build();}@Beanpublic Step logStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {return new StepBuilder("logStep", jobRepository).tasklet((contribution, chunkContext) -> {System.out.println("Job completed successfully!");return RepeatStatus.FINISHED;}).transactionManager(transactionManager).build();}@Beanpublic Job importProductsJob(JobRepository jobRepository, Step importStep, Step logStep) {return new JobBuilder("importProductsJob", jobRepository).start(importStep).next(logStep).build();}
}

Processor 实现(为完整性重复):

package com.example.springbatchdemo.config;import com.example.springbatchdemo.entity.Product;
import org.springframework.batch.item.ItemProcessor;public class ProductItemProcessor implements ItemProcessor<Product, Product> {private static final double EXCHANGE_RATE = 0.14;@Overridepublic Product process(Product item) {if (item.getPrice() <= 0) {return null;}item.setPrice(item.getPrice() * EXCHANGE_RATE);return item;}
}

说明

  • 使用 @Bean 定义 Reader、Processor、Writer、Step 和 Job。
  • JobBuilderStepBuilder 提供流式 API,清晰定义作业结构。
  • 支持条件流(如 .on("COMPLETED").to(nextStep)),后续文章会深入。

优点

  • 类型安全,编译期检查错误。
  • 与 Spring Boot 集成紧密,易于调试。
  • 代码清晰,适合现代开发。

2.2 XML 配置

XML 配置使用 Spring 的 XML 配置文件定义 Job 和 Step,常见于早期 Spring 项目。以下是将上述 Java 配置转换为 XML 的等效实现。

示例:XML 配置

创建 batch-config.xml(放置在 src/main/resources):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:batch

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词