With the introduction of the previous two articles on spring batch, you should have a preliminary idea of spring batch. This article, will pass a "Hello world! "Example, and discuss some of the basic configuration and implementation of spring batch with you. So we have a real experience of spring batch from the point of view of development.
Description: 1, this example uses the Spring-batch 2.1.8
2, this example does not configure Itemreader, Itemprocessor, and Itemwriter as previously mentioned, but instead calls Tasklet in step and completes "Hello Tasklet" by world! The output.
Engineering structures such as:
The Joblaunch.java class is used to start Bath,writetasklet.java to complete the output work. Application.xml is used to configure some spring information, batch.xml configure job information.
The Application.xml file is configured as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p"
xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:context= "Http://www.springframework.org/schema/context"
xsi:schemalocation= "Http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd "
default-autowire= "ByName" >
<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 ">
</bean>
<bean id= "TransactionManager"
class= "Org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
</beans>
Joblauncher is responsible for batch start-up work, Jobrepository is responsible for the entire operation of the job CRUD operations, TransactionManager responsible for transaction management operations.
The Batch.xml file is configured as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<bean:beans xmlns= "Http://www.springframework.org/schema/batch"
xmlns:bean= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "
xmlns:p= "http://www.springframework.org/schema/p" xmlns:tx= "Http://www.springframework.org/schema/tx "
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:context= "http://www.springframework.org/schema/ Context "
xsi:schemalocation= "Http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd ">
<bean:import resource= "Applicationcontext.xml"/>
<job id= "Helloworldjob" >
<step id= "Step_hello" next= "Step_world" >
<tasklet ref= "Hello" transaction-manager= "TransactionManager" ></tasklet>
</step>
<step id= "Step_world" >
<tasklet ref= "World" transaction-manager= "TransactionManager" ></tasklet>
</step>
</job>
<bean:bean id= "Hello" class= "Com.wanggc.springbatch.sample.helloworld.writeTasklet" >
<bean:property name= "message" value= "Hello" ></bean:property>
</bean:bean>
<bean:bean id= "World" class= "Com.wanggc.springbatch.sample.helloworld.writeTasklet" >
<bean:property name= "message" value= "world!" ></bean:property>
</bean:bean>
</bean:beans>
A job with ID Helloworldjob is configured, the job has two Step:step_hello and Step_world, which is responsible for outputting "Hello", which is responsible for outputting "world!", when the first step is completed, Executes the second step.
The code for the Writetasklet class is as follows:
Public class Writetasklet implements Tasklet {
/** Message * *
private String message;
/**
* @param message
* The message to set
*/
Public void Setmessage (String message) {
this.message = message;
}
@Override
Public Repeatstatus Execute (stepcontribution arg0, Chunkcontext arg1)
throws Exception {
System.out.println (message);
return repeatstatus.finished;
}
}
A message property is defined in this class that injects values through Batch.xml's "Hello" and "World" beans. The Execute method, which is inherited by the Tasklet interface, is where Tasklet implements the business logic. This instance simply outputs the message message and returns directly.
The code for the startup class Joblaunch class is as follows:
Public class Joblaunch {
/**
* @param args
*/
Public static void Main (string[] args) {
ApplicationContext context = new Classpathxmlapplicationcontext (
"Batch.xml");
joblauncher launcher = (joblauncher) context.getbean ("Joblauncher");
Job Job = (Job) Context.getbean ("Helloworldjob");
try {
/ * Run job * /
jobexecution result = Launcher.run (Job, New Jobparameters ());
/ * processing ends, console print processing results * /
System.out.println (result.tostring ());
} catch (Exception e) {
e.printstacktrace ();
}
}
}
Spring Batch Simple Application (i) (Hello world)