欢迎使用Spring Batch示例。Spring Batch是一个用于执行批处理作业的弹簧框架模块。我们可以使用spring批处理来处理一系列作业。
目录[ 隐藏 ]
在进行spring批处理示例程序之前,让我们先了解一下spring批处理术语。
让我们考虑一个实施弹簧批的实例。我们将考虑以下方案用于实现目的。
包含数据的CSV文件需要转换为XML以及数据和标记将在列名后面命名。
以下是用于弹簧批处理示例的重要工具和库。
下图说明了Spring Batch示例项目中的所有组件。
下面是pom.xml文件的内容,其中包含spring batch示例项目的所有必需依赖项。
<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>com.journaldev.spring</groupId><artifactId>SpringBatchExample</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>SpringBatchDemo</name><url>http://maven.apache.org</url><properties><jdk.version>1.8</jdk.version><spring.version>4.3.12.RELEASE</spring.version><spring.batch.version>3.0.8.RELEASE</spring.batch.version><mysql.driver.version>5.1.25</mysql.driver.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 jdbc, for database --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><!-- Spring XML to/back object --><dependency><groupId>org.springframework</groupId><artifactId>spring-oxm</artifactId><version>${spring.version}</version></dependency><!-- MySQL database driver --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.driver.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><!-- Spring Batch unit test --><dependency><groupId>org.springframework.batch</groupId><artifactId>spring-batch-test</artifactId><version>${spring.batch.version}</version></dependency><!-- Junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version><scope>test</scope></dependency><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.10</version></dependency></dependencies><build><finalName>spring-batch</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-eclipse-plugin</artifactId><version>2.9</version><configuration><downloadSources>true</downloadSources><downloadJavadocs>false</downloadJavadocs></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.3.2</version><configuration><source>${jdk.version}</source><target>${jdk.version}</target></configuration></plugin></plugins></build>
</project>
以下是用于弹簧批处理的示例CSV文件的内容。
1001,Tom,Moody, 29/7/2013
1002,John,Parker, 30/7/2013
1003,Henry,Williams, 31/7/2013
我们必须在配置文件中定义spring bean和spring批处理作业。下面是job-batch-demo.xml
文件的内容,它是spring批处理项目中最重要的部分。
<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/batchhttp://www.springframework.org/schema/batch/spring-batch-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"><import resource="../config/context.xml" /><import resource="../config/database.xml" /><bean id="report" class="com.journaldev.spring.model.Report"scope="prototype" /><bean id="itemProcessor" class="com.journaldev.spring.CustomItemProcessor" /><batch:job id="DemoJobXMLWriter"><batch:step id="step1"><batch:tasklet><batch:chunk reader="csvFileItemReader" writer="xmlItemWriter"processor="itemProcessor" commit-interval="10"></batch:chunk></batch:tasklet></batch:step></batch:job><bean id="csvFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"><property name="resource" value="classpath:csv/input/report.csv" /><property name="lineMapper"><bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper"><property name="lineTokenizer"><beanclass="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"><property name="names" value="id,firstname,lastname,dob" /></bean></property><property name="fieldSetMapper"><bean class="com.journaldev.spring.ReportFieldSetMapper" /><!-- if no data type conversion, use BeanWrapperFieldSetMapper to map by name <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper"> <property name="prototypeBeanName" value="report" /> </bean> --></property></bean></property></bean><bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter"><property name="resource" value="file:xml/outputs/report.xml" /><property name="marshaller" ref="reportMarshaller" /><property name="rootTagName" value="report" /></bean><bean id="reportMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"><property name="classesToBeBound"><list><value>com.journaldev.spring.model.Report</value></list></property></bean></beans>
FlatFileItemReader
读取CSV文件,CustomItemProcessor
处理数据并使用写入XML文件StaxEventItemWriter
。batch:job
- 此标记定义我们要创建的作业。Id属性指定作业的ID。我们可以在一个xml文件中定义多个作业。csvFileItemReader
在这个例子中使用了bean作为实例FlatFileItemReader
。CustomItemProcessor
在这个例子中使用过。ItemReader
,ItemProcessor
并ItemWriter
从org.springframework.batch.item
包。首先,我们将CSV文件读入java对象,然后使用JAXB将其写入xml文件。下面是我们的模型类,带有所需的JAXB 注释。
package com.journaldev.spring.model;import java.util.Date;import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "record")
public class Report {private int id;private String firstName;private String lastName;private Date dob;@XmlAttribute(name = "id")public int getId() {return id;}public void setId(int id) {this.id = id;}@XmlElement(name = "firstname")public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}@XmlElement(name = "lastname")public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}@XmlElement(name = "dob")public Date getDob() {return dob;}public void setDob(Date dob) {this.dob = dob;}@Overridepublic String toString() {return "Report [id=" + id + ", firstname=" + firstName + ", lastName=" + lastName + ", DateOfBirth=" + dob+ "]";}}
请注意,模型类字段应与spring批处理映射器配置中定义的相同,即property name="names" value="id,firstname,lastname,dob"
在我们的示例中。
FieldSetMapper
转换日期需要自定义。如果不需要数据类型转换,则只BeanWrapperFieldSetMapper
应使用自动按名称映射值。
扩展的java类FieldSetMapper
是ReportFieldSetMapper
。
package com.journaldev.spring;import java.text.ParseException;
import java.text.SimpleDateFormat;import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;import com.journaldev.spring.model.Report;public class ReportFieldSetMapper implements FieldSetMapper<Report> {private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");public Report mapFieldSet(FieldSet fieldSet) throws BindException {Report report = new Report();report.setId(fieldSet.readInt(0));report.setFirstName(fieldSet.readString(1));report.setLastName(fieldSet.readString(2));// default format yyyy-MM-dd// fieldSet.readDate(4);String date = fieldSet.readString(3);try {report.setDob(dateFormat.parse(date));} catch (ParseException e) {e.printStackTrace();}return report;}}
现在,在作业配置中定义,itemProcessor将在itemWriter之前触发。我们CustomItemProcessor.java
为此创建了一个类。
package com.journaldev.spring;import org.springframework.batch.item.ItemProcessor;import com.journaldev.spring.model.Report;public class CustomItemProcessor implements ItemProcessor<Report, Report> {public Report process(Report item) throws Exception {System.out.println("Processing..." + item);String fname = item.getFirstName();String lname = item.getLastName();item.setFirstName(fname.toUpperCase());item.setLastName(lname.toUpperCase());return item;}}
我们可以在ItemProcessor实现中操作数据,因为您可以看到我将名字和姓氏值转换为大写。
在我们的spring批量配置文件中,我们已经导入了两个额外的配置文件 - context.xml
和database.xml
。
<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-4.3.xsd"><!-- stored job-meta in memory --><!-- <bean id="jobRepository"class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"><property name="transactionManager" ref="transactionManager" /></bean>--><!-- stored job-meta in database --><bean id="jobRepository"class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"><property name="dataSource" ref="dataSource" /><property name="transactionManager" ref="transactionManager" /><property name="databaseType" value="mysql" /></bean><bean id="transactionManager"class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /><bean id="jobLauncher"class="org.springframework.batch.core.launch.support.SimpleJobLauncher"><property name="jobRepository" ref="jobRepository" /></bean></beans>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:jdbc="http://www.springframework.org/schema/jdbc" 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-4.3.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd"><!-- connect to database --><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/Test" /><property name="username" value="test" /><property name="password" value="test123" /></bean><bean id="transactionManager"class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" /><!-- create job-meta tables automatically --><!-- <jdbc:initialize-database data-source="dataSource"> <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" /> <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" /> </jdbc:initialize-database> -->
</beans>
Spring Batch使用一些元数据表来存储批处理作业信息。我们可以从Spring批处理配置中创建它们,但建议通过执行SQL文件手动完成它们,如上面的注释代码所示。从安全的角度来看,最好不要给Spring批处理数据库用户提供DDL执行权限。
Spring Batch表与Java中表示它们的Domain对象非常匹配。例如 - JobInstance,JobExecution,JobParameters和StepExecution地图BATCH_JOB_INSTANCE,BATCH_JOB_EXECUTION,分别BATCH_JOB_EXECUTION_PARAMS和BATCH_STEP_EXECUTION。
ExecutionContext映射到BATCH_JOB_EXECUTION_CONTEXT和BATCH_STEP_EXECUTION_CONTEXT。
JobRepository负责将每个java对象保存并存储到正确的表中。
以下是每个元数据表的详细信息。
我们的Spring Batch示例项目已准备就绪,最后一步是编写一个测试类来将其作为java程序执行。
package com.journaldev.spring;import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main(String[] args) {String[] springConfig = { "spring/batch/jobs/job-batch-demo.xml" };ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(springConfig);JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");Job job = (Job) context.getBean("DemoJobXMLWriter");JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();try {JobExecution execution = jobLauncher.run(job, jobParameters);System.out.println("Exit Status : " + execution.getStatus());} catch (Exception e) {e.printStackTrace();}System.out.println("Done");context.close();}
}
只需运行上面的程序,您将获得如下所示的输出xml。
Copy
<?xml version="1.0" encoding="UTF-8"?><report><record id="1001"><dob>2013-07-29T00:00:00+05:30</dob><firstname>TOM</firstname><lastname>MOODY</lastname></record><record id="1002"><dob>2013-07-30T00:00:00+05:30</dob><firstname>JOHN</firstname><lastname>PARKER</lastname></record><record id="1003"><dob>2013-07-31T00:00:00+05:30</dob><firstname>HENRY</firstname><lastname>WILLIAMS</lastname></record></report>
这就是Spring Batch示例,您可以从下面的链接下载最终项目。
下载Spring Batch示例项目
参考:官方指南
转载来源:https://www.journaldev.com/17157/spring-batch-example
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态