首页 > 代码库 > Spring Batch_官网DEMO实现

Spring Batch_官网DEMO实现

Spring Batch_官网DEMO实现

http://spring.io/guides/gs/batch-processing/

使用spring xml方式实现了spring batch官网的demo,现在把具体的代码贴出来,具体的细节配置还要参考官网的说明。

首先建立maven项目,pom文件如下:

<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>lyx</groupId>
	<artifactId>SpringBatch2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<!--spring context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<!--spring core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<!--spring bean -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<!--spring aop -->

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<!--spring jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<!-- spring tx -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.batch</groupId>
			<artifactId>spring-batch-core</artifactId>
			<version>3.0.2.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>3.1</version>
		</dependency>

		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jdbc</artifactId>
			<version>8.0.14</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
		</dependency>

	</dependencies>
</project>


最主要的就是配置下面的这个spring-batch2.xml文件,也就是配置reader,writer,processor,以及各种依赖类。该spring-batch2.xml文件时依据BatchConfiguration.java配置的,具体配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd">

	<bean id="lineTokenizer"
		class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
		<property name="delimiter" value="http://www.mamicode.com/," />
		<property name="names">
			<list>
				<value>firstName</value>
				<value>lastName</value>
			</list>
		</property>
	</bean>
	<bean id="fieldSetMapper"
		class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
		<property name="prototypeBeanName" value="http://www.mamicode.com/person" />
	</bean>
	<bean id="person" class="com.lyx.batch.Person" scope="prototype" />
	<bean id="lineMapper"
		class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
		<property name="lineTokenizer" ref="lineTokenizer" />
		<property name="fieldSetMapper" ref="fieldSetMapper" />
	</bean>
	<bean id="resource" class="org.springframework.core.io.ClassPathResource">
		<constructor-arg index="0" type="java.lang.String"
			value="http://www.mamicode.com/sample-data.csv" />
	</bean>
	<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
		<property name="resource" ref="resource" />
		<property name="encoding" value="http://www.mamicode.com/utf-8" />
		<property name="lineMapper" ref="lineMapper" />
	</bean>

	<bean id="itemProcessor" class="com.lyx.batch.PersonItemProcessor" />
	<bean id="jdbcBatchItemWriter"
		class="org.springframework.batch.item.database.JdbcBatchItemWriter">
		<property name="itemSqlParameterSourceProvider" ref="itemSqlParameterSourceProvider" />
		<property name="sql"
			value="http://www.mamicode.com/INSERT INTO people (first_name, last_name) VALUES (:firstName, :lastName)" />
		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="itemSqlParameterSourceProvider"
		class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
	<!--tomcat jdbc pool数据源配置 -->
	<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
		destroy-method="close">
		<property name="poolProperties">
			<bean class="org.apache.tomcat.jdbc.pool.PoolProperties">
				<property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver" />
				<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/test" />
				<property name="username" value="http://www.mamicode.com/root" />
				<property name="password" value="http://www.mamicode.com/034039" />
				<property name="maxActive" value="http://www.mamicode.com/100" />
				<property name="initialSize" value="http://www.mamicode.com/10" />
				<property name="maxWait" value="http://www.mamicode.com/10000" />
				<property name="minIdle" value="http://www.mamicode.com/10" />
			</bean>
		</property>
	</bean>

	<batch:job id="sampleJob" job-repository="jobRepository">
		<batch:step id="step">
			<tasklet transaction-manager="transactionManager">
				<chunk reader="flatFileItemReader" processor="itemProcessor"
					writer="jdbcBatchItemWriter" commit-interval="1" />
			</tasklet>
		</batch:step>
	</batch:job>

	<!-- spring batch 配置jobRepository -->
	<batch:job-repository id="jobRepository"
		data-source="dataSource" transaction-manager="transactionManager"
		isolation-level-for-create="REPEATABLE_READ" table-prefix="BATCH_"
		max-varchar-length="1000" />
	<!-- spring的事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- batch luncher -->
	<bean id="jobLauncher"
		class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
		<property name="jobRepository" ref="jobRepository" />
	</bean>
</beans>


主要的类有以下两个类:

Person.java

package com.lyx.batch;

public class Person {
	private String lastName;
	private String firstName;

	public Person() {
	}

	public Person(String firstName, String lastName) {
		this.firstName = firstName;
		this.lastName = lastName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "firstName: " + firstName + ", lastName: " + lastName;
	}

}

PersonItemProcessor.java

package com.lyx.batch;

import org.springframework.batch.item.ItemProcessor;

public class PersonItemProcessor implements ItemProcessor<Person, Person> {

	public Person process(final Person person) throws Exception {
		final String firstName = person.getFirstName().toUpperCase();
		final String lastName = person.getLastName().toUpperCase();
		final Person transformedPerson = new Person(firstName, lastName);
		System.out.println("Converting (" + person + ") into ("
				+ transformedPerson + ")");
		return transformedPerson;
	}

}


最后,运行这个demo的前提是你要把spring batch的数据库配置好,具体的sql文件在spring batch core jar包中,还有数据源文件,要放在resources目录中。。

AppMain.java启动该demo

package com.lyx.batch;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppMain {

	public static void main(String[] args)
			throws JobExecutionAlreadyRunningException, JobRestartException,
			JobInstanceAlreadyCompleteException, JobParametersInvalidException {
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "classpath:spring-batch2.xml" });
		JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
		Job job = (Job) context.getBean("sampleJob");
		JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
		JobExecution result = launcher.run(job,
				jobParametersBuilder.toJobParameters());
		ExitStatus es = result.getExitStatus();
		if (es.getExitCode().equals(ExitStatus.COMPLETED.getExitCode())) {
			System.out.println("任务正常完成");
		} else {
			System.out.println("任务失败,exitCode=" + es.getExitCode());
		}
	}
}

如果没有意外的话,会在数据库中看到相应的结果。

在该demo的基础上可以 试验spring batch的各种特性,可以进一步探索spring batch。

==========END==========


Spring Batch_官网DEMO实现