Spring batch_configuring a Step for Restart
Spring Official Document: Http://docs.spring.io/spring-batch/trunk/reference/html/configureStep.html#stepRestart
When an ordinary job is in the state of complete, it cannot be restart. Let's see how to make a job restart.
Restartable= "true"
<job id= "Footballjob" restartable= "false" > ...</job>
The job should be configured to restartable= "true" first.
Then see how the step is configured:
Allow-start-if-complete= "true"
This is set when the step is complete, it can also be restarted. This default value is False.
start-limit= "3"
This is the number of times the setting can be restarted, and the default value is Integer.max_value.
Here is a demo from the official Spring document:
<job id= "Footballjob" restartable= "true" ><step id= "Playerload" next= "Gameload" ><tasklet>< Chunk reader= "Playerfileitemreader" writer= "Playerwriter" commit-interval= "/></tasklet></step>" <step id= "Gameload" next= "playersummarization" ><tasklet allow-start-if-complete= "true" ><chunk Reader= "Gamefileitemreader" writer= "Gamewriter" commit-interval= "ten"/></tasklet></step><step ID = "Playersummarization" ><tasklet start-limit= "3" ><chunk reader= "Playersummarizationsource" writer= " Summarywriter "Commit-interval="/></tasklet></step></job>
Here is my configuration for restart:
Spring-batch-restart.xml
<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 "xmlns:context="/HTTP/ Www.springframework.org/schema/context "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.xsdhttp://www.springframework.org/schema/ Context http://www.springframework.org/schema/context/spring-context.xsd "><!-- Pack Scan -- ><context:component-scan base-package= "Com.lyx.batch" /><bean id= "ExceptionHandler" class= "Com.lyx.batch.ExceptionListener" /><batch:step id= "Abstractstep" abstract= " True "><batch:listeners><batch:listener ref=" Exceptionhandler " /></batch:listeners ></batch:step><bean id= "ABstractcursorreader " abstract=" true "class=" Org.springframework.batch.item.database.JdbcCursorItemReader " ><property name= "DataSource" ref= "DataSource" /></bean><!-- add People desc job begin --><batch:job id= "AddPeopleDescJob" Restartable= "true" ><batch:step id= "Adddescstep" parent= "Abstractstep" ><batch:tasklet allow-start-if-complete= "true" start-limit= "2" ><batch:chunk reader= "Peopleadddescreader" Processor= "Adddescprocessor" writer= "Adddescpeoplewriter" commit-interval= "2" /></batch:tasklet ></batch:step></batch:job><!-- add people desc job end - -><bean id= "Peopleadddescreader" parent= "Abstractcursorreader" scope= "Step" ><property name= "SQL" ><value><! [Cdata[select first_name ,last_name from people where first_name like&NBSP;?&NBSP;OR&NBSP;LAST_NAME&NBSP;LIKE&NBSP;]] ></value></property><property name= "RowMapper" ref= "PeopleRowMapper" /> <property name= "PreparedStatementSetter" ref= "PreparedStatementSetter" /><property name= "Fetchsize" value= " /></bean><bean id=" Peoplerowmapper " class=" Com.lyx.batch.PeopleRowMapper " /><bean id=" PreparedStatementSetter " class=" Com.lyx.batch.PeoplePreparedStatementSetter " /><bean id=" Adddescprocessor " class=" Com.lyx.batch.AddPeopleDescProcessor " /><bean id=" Adddescpeoplewriter " class=" Com.lyx.batch.AddDescPeopleWriter " /><!--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" /></bean></property></bean><!-- 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>
Appmain8.java
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;/** * Test restart * * @author Lenovo * */public class AppMain8 {public static Void main (String[] args) throws jobexecutionalreadyrunningexception, jobrestartexception,jobinstancealreadycompleteexception, jobparametersinvalidexception { Long starttime = system.currenttimemillis (); // Get start time @suppresswarnings ("resource") Applicationcontext context = new classpathxmlapplicationcontext (new String[] { "Classpath:spring-batch-restart.xml" &NBSP;}); Jobparametersbuilder jobparametersbuilder = new jobparametersbuilder (); job job = (Job) context.getbean ("Addpeopledescjob"); 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 ());} Long endtime = system. Currenttimemillis (); // Get end Time System.out.println ("program run Time: " + (endtime - StartTime) + "MS");}}
When I first run successfully:
SELECT * from Batch_job_instance;
job_instance_id VERSION job_name job_key-------------------------------------- --------------------------------1 0 addpeopledescjob d41d8cd98f00b204e9800998ecf8427e
When I run the second time successfully, query the related table:
SELECT * from Batch_job_execution; you will find two job_execution, all of which belong to a job instance.
job_instance_id VERSION job_name job_key-------------------------------------- --------------------------------1 0 addpeopledescjob d41d8cd98f00b204e9800998ecf8427e
job_execution_id job_instance_id create_time--------------------------------------------------1 1 2014-11-14 12:33:12 2 1 2014-11-14 12:33:48
To view step run information:
Select step_execution_id, Step_name, job_execution_id from Batch_step_execution;
step_execution_id step_name job_execution_id--------------------------------------------1 Add Descstep 1 2 adddescstep 2
The corresponding database will also be one more times the data.
When the job is run for the third time, the following exception information is reported:
caused by:org.springframework.batch.core.StartLimitExceededException:Maximum start limit exceeded for step: Adddescstepstartmax:2
It is obvious that the number of reboots is exhausted ...
=============================end=============================
Spring batch_configuring a Step for Restart