Spring Batch_ Website Demo implementation
http://spring.io/guides/gs/batch-processing/
Using spring XML to implement the spring batch website demo, now put the specific code, specific details of the configuration should also refer to the official website instructions.
To build the MAVEN project first, the Pom file is as follows:
<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>
The main thing is to configure the following Spring-batch2.xml file, which is the configuration reader,writer,processor, as well as the various dependency classes. The Spring-batch2.xml file is configured According to Batchconfiguration.java and is configured as follows:
<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.xsdhttp ://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 Data Source Configuration --><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= " /><property name=" InitialSize " value=" / ><property name= "maxwait" value= "10000" /><property name= "MinIdle" value= "Ten" /></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 configuration jobrepository --><batch:job-repository id= "Jobrepository "Data-source=" DataSource " transaction-manager=" TransactionManager "isolation-level-for-create=" REPEATABLE_ READ " table-prefix=" Batch_ "Max-varcHar-length= /><!-- spring transaction manager --><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>
The main classes have the following two classes:
Person.java
Package Com.lyx.batch;public class Person {private string lastname;private string firstname;public person () {}public Pers On (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;} @Overridepublic 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;}}
Finally, the premise of running this demo is that you have to configure the spring batch database, the specific SQL files in the spring batch core jar package, and the data source files, to be placed in the resources directory.
Appmain.java Start the 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" &NBSP;}); 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 ("task completed");} else {system.out.println ("Task failed, exitcode=" + es.getexitcode ());}}}
If there is no accident, you will see the corresponding results in the database.
Based on this demo, you can experiment with the various features of spring batch to further explore spring batch.
==========end==========
Spring Batch_ Website Demo implementation