1. Intro to Spring Batch
1) Spring Batch is a framework for Batch Processing - Execution of a series of Jobs.
2) Each "Job" contains many "Steps". And one "Step" can be a single "tasklet" or a "READ-PROCESS-WRITE" task.
2. An overview of Spring Batch
3. Example for Tasklet
A sample tasklet delete all files under a specific folder.
context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <import resource="../jobs/tasklet.xml" /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository" /> </bean> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> <property name="transactionManager" ref="transactionManager" /> </bean> <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /> </beans>
tasklet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <batch:job id="taskletExampleJob"> <batch:step id="cleanFolder"> <batch:tasklet ref="cleanResourceFolderTasklet" /> </batch:step> </batch:job> <bean id="cleanResourceFolderTasklet" class="edu.xmu.spring.batch.tasklet.CleanResourceFolderTasklet"> <property name="directory" value="file:csv/bak" /> </bean> </beans>
App.java
package edu.xmu.spring.batch; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "spring/batch/config/context.xml" }); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("taskletExampleJob"); JobExecution execution; try { execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + execution.getStatus()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); context.close(); } }
CleanResourceFolderTasklet.java
package edu.xmu.spring.batch.tasklet; import java.io.File; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.UnexpectedJobExecutionException; 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.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.util.Assert; public class CleanResourceFolderTasklet implements Tasklet, InitializingBean { private Resource directory; @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { File dir = directory.getFile(); Assert.state(dir.isDirectory()); File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { boolean deleted = files[i].delete(); if (!deleted) { throw new UnexpectedJobExecutionException( "Could not delete file " + files[i].getPath()); } else { System.out.println(files[i].getPath() + " is deleted!"); } } return RepeatStatus.FINISHED; } @Override public void afterPropertiesSet() throws Exception { Assert.notNull(directory, "directory must be set"); } public Resource getDirectory() { return directory; } public void setDirectory(Resource directory) { this.directory = directory; } }
4. Example for Job(Read-Process-Write)
Read from csv and write to csv.
context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <import resource="../jobs/job.xml" /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository" /> </bean> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"> <property name="transactionManager" ref="transactionManager" /> </bean> <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /> </beans>
job.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:batch="http://www.springframework.org/schema/batch" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <batch:job id="readProcessWriteExampleJob"> <batch:step id="readAndWriteStep"> <batch:tasklet> <batch:chunk reader="cvsFileItemReader" processor="csvItemProcessor" writer="csvItemWriter" commit-interval="10" /> </batch:tasklet> </batch:step> </batch:job> <bean id="cvsFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name="resource" value="file:csv/input/user.csv" /> <property name="lineMapper"> <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"> <property name="lineTokenizer"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="id, name, password" /> </bean> </property> <property name="fieldSetMapper"> <bean class="edu.xmu.spring.batch.UserFieldSetMapper" /> </property> </bean> </property> </bean> <bean id="csvItemProcessor" class="edu.xmu.spring.batch.processor.UserCsvItemProcessor" /> <bean id="csvItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"> <property name="resource" value="file:csv/output/user.csv" /> <property name="lineAggregator"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"> <property name="fieldExtractor"> <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"> <property name="names" value="id, username, password" /> </bean> </property> </bean> </property> </bean> </beans>
User.java
package edu.xmu.spring.batch.model; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
UserFieldSetMapper.java
package edu.xmu.spring.batch; import org.springframework.batch.item.file.mapping.FieldSetMapper; import org.springframework.batch.item.file.transform.FieldSet; import org.springframework.validation.BindException; import edu.xmu.spring.batch.model.User; public class UserFieldSetMapper implements FieldSetMapper<User> { @Override public User mapFieldSet(FieldSet fieldSet) throws BindException { User user = new User(); user.setId(fieldSet.readInt("id")); user.setUsername(fieldSet.readString("name")); user.setPassword(fieldSet.readString("password")); return user; } }
UserCsvItemProcessor.java
package edu.xmu.spring.batch.processor; import org.springframework.batch.item.ItemProcessor; import edu.xmu.spring.batch.model.User; public class UserCsvItemProcessor implements ItemProcessor<User, User> { @Override public User process(User item) throws Exception { item.setUsername(item.getUsername().replace(',', '-')); item.setPassword(item.getPassword().replace(',', '-')); return item; } }
App.java
package edu.xmu.spring.batch; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] { "spring/batch/config/context.xml" }); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("readProcessWriteExampleJob"); JobExecution execution; try { execution = jobLauncher.run(job, new JobParameters()); System.out.println("Exit Status : " + execution.getStatus()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); context.close(); } }
pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>edu.xmu.spring.batch</groupId> <artifactId>spring-batch</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <spring.version>3.2.2.RELEASE</spring.version> <spring.batch.version>2.2.0.RELEASE</spring.batch.version> <junit.version>4.11</junit.version> </properties> <dependencies> <!-- Spring Core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring Batch dependencies --> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-core</artifactId> <version>${spring.batch.version}</version> </dependency> <dependency> <groupId>org.springframework.batch</groupId> <artifactId>spring-batch-infrastructure</artifactId> <version>${spring.batch.version}</version> </dependency> </dependencies> </project>
Reference Links:
1) http://www.mkyong.com/spring-batch/
相关推荐
* Spring enterprise: Spring Java EE integration, Spring Integration, Spring Batch, jBPM with Spring, Spring Remoting, messaging, transactions, scaling using Terracotta and GridGrain, and more. ...
Spring Batch 是一个强大的、全面的批处理框架,用于处理大量数据。它被广泛应用于企业级应用,特别是那些需要高效、可扩展性和可靠性的系统。在3.0.5.RELEASE这个版本中,Spring Batch 提供了一系列增强的功能和...
《Spring Batch Admin 1.3.0.RELEASE:全面解析与应用指南》 Spring Batch Admin是基于Spring Batch的管理界面,它为批处理作业的监控、管理和调度提供了一个直观的Web界面。在本文中,我们将深入探讨Spring Batch ...
You'll also get solutions to common problems with persistence, integrating Spring Boot with batch processing, algorithmic programming via Spring Batch, and much more. Other recipes cover topics such ...
Work with all aspects of batch processing in a modern Java environment using a selection of Spring frameworks. This book provides up-to-date examples using the latest configuration techniques based on...
初始化数据库:找到目录:/ spring-batch-admin-backend / src / main / db,里面有两个文件,一个是数据库创建脚本,一个是表结构+数据的脚本,先执行创建库的,如果想在已经存在的库里面运行程序,可以省略这一步...
使用Java 8+构建,Spring Boot(2.0.3)Redis(3.0.4) 经过JUnit(4.12)和JMeter的测试 通过带有三个Spring配置文件(dev,qa,dk)的Spring Boot执行-假定mvn clean install dev Profile异步处理批处理并注销...
Spring Batch 是一个强大的、全面的批处理框架,用于处理大量数据。它被广泛应用于企业级应用,特别是那些需要处理大量输入/输出数据的系统。在深入理解 Spring Batch 的源码之前,我们需要先了解一下 Spring Batch ...
这些JAR包涵盖了Spring的核心模块,如core-container、web、data、amqp、batch等,还包括了对其他开源框架如Hibernate、MyBatis的支持库。开发者在实际项目中会把这些库导入到类路径中,以使用Spring提供的各种服务...
《Spring Batch 框架详解:2.0.0.RELEASE 版本解析》 Spring Batch 是一个由 Spring 社区开发的开源框架,专为处理大量数据的批处理作业而设计。它提供了完整的功能,包括事务管理、错误处理、跳过策略、重启能力...
Spring Batch 是一个强大的Java批处理框架,用于处理大量数据处理任务。这个名为“poc_spring_batch”的项目是一个概念证明,展示了如何利用Spring Batch来构建一个批量应用程序,并进行升级测试。下面将详细介绍...
|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|:-------:| | 25 | 28.57 | 28.83 | 28.68 | 28.96 | 28.74 | 28.92 | **29.23** | **29.16** | **29.17** | - Set...
Jointly developed by SpringSource and Accenture, Spring Batch fills this critical gap by providing a robust and convenient framework for writing batch applications that process large volumes of ...
11. **Spring Batch**:对于批量处理和工作流任务,Spring Batch 提供了一个全面的解决方案,包括事务管理、错误处理和重试机制。 12. **Spring Integration**:Spring Integration 提供了连接不同系统和协议的能力...
《Spring Batch 2.2.0.RELEASE:批量处理与数据转换的核心技术解析》 Spring Batch 是一个由 Spring 社区开发的开源框架,专为处理大批量数据而设计。在2.2.0.RELEASE版本中,它提供了一套强大、可扩展且企业级的...
**Spring Batch 深度解析** Spring Batch 是一个强大的、全面的批处理框架,由 Spring 社区开发,旨在简化企业级应用中的批量数据处理任务。这个框架提供了一种标准的方式来处理大量的数据输入和输出,使得开发者...
最后,本书还涵盖了Spring的其他扩展,如Spring Batch(批处理)、Spring Integration(企业级集成)和Spring Boot,这些都是Spring生态系统的一部分,有助于构建复杂的企业级应用。 通过《Spring Recipes: A ...