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="," />
		<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="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="sample-data.csv" />
	</bean>
	<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
		<property name="resource" ref="resource" />
		<property name="encoding" value="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="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="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="034039" />
				<property name="maxActive" value="100" />
				<property name="initialSize" value="10" />
				<property name="maxWait" value="10000" />
				<property name="minIdle" value="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==========

时间: 2024-10-10 08:13:00

Spring Batch_官网DEMO实现的相关文章

webpack官网demo起步中遇到的问题

在webpack官网demo一开始搭建中 

jqgrid--api,官网demo,编辑

api参考: http://blog.csdn.net/hurryjiang/article/details/7551477 官网demo: http://www.trirand.com/blog/jqgrid/jqgrid.html 对单元格进入编辑状态: 1 在colModel中加入三个参数: editable : true, edittype : "checkbox", editoptions : { value : "运行:停止" } 2 监听select事

SpringBoot使用JSP(官网Demo)

最开始接触java的时候,前端页面基本都是用jsp来写,最近公司项目要使用SpringBoot重构,查看SpringBoot文档,发现SpringBoot不建议使用JSP,因为jsp在使用内嵌servlet容器时会有一些限制 虽然以后项目中也会将jsp替换成Template Engines,而且这都是前端的事情,其实与后端并无比较大的联系,但比较好奇Springboot中对jsp的使用,故根据官方文档进行了简单测试验证 如果使用JSP,则需要将项目打包成war包,jar包不支持JSP. 打开JS

spring改版官网下载jar包, 源代码和文档

从网上找了一些方法,现在都整理了一下,有简单粗暴的,也有百转回肠的(详细,直接从官网一步一步的进入下载页),希望大家根据自己的喜好可以找到的真爱. 方法一:(简单粗暴直接) http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-framework-3.2.4.RELEASE-dist.zip 直接粘到地址栏或者下载工具里,每次有更新只要改版本号就可以: 方法二

Remoting and web services using Spring[摘自官网]

spring document url: http://docs.spring.io/spring/docs/ Using Hessian First we’ll have to create a new servlet in your application (this is an excerpt from 'web.xml'): <servlet> <servlet-name>remoting</servlet-name> <servlet-class>

Spring官网及jar包下载

1.Spring官网地址:http://spring.io/ 2.Spring jar包下载 进入Spring的官网: Spring jar 包下载: 点击链接,进入下载页面

如何运行Struts2官网最新Demo?

本篇将讲述下如何运行官网当前最新Struts2.5.10.1 版本的Demo. Struts2 官网:http://struts.apache.org/ 0x00 Demo下载 Struts2 官网2.5.10.1 版Demo: https://github.com/apache/struts-examples 0x01 修改pom.xml配置 由于官网Demo采用Maven管理方式,刚开始尝试没有修改走了点坑,现在把坑填一下. Downloads\struts-examples-master\

Android学习 多读官网,故意健康---手势

官网地址 ttp://developer.android.com/training/gestures/detector.html: 一.能够直接覆盖Activity的onTouch方法 public class MainActivity extends Activity { ... // This example shows an Activity, but you would use the same approach if // you were subclassing a View. @O

Android学习 多读官网,有益健康---手势

官网地址 ttp://developer.android.com/training/gestures/detector.html: 一.可以直接覆盖Activity的onTouch方法 public class MainActivity extends Activity { ... // This example shows an Activity, but you would use the same approach if // you were subclassing a View. @O