Spring batch_intercepting Step execution_ configuration Skiplistener
About configuring skip:http://my.oschina.net/xinxingegeya/blog/346244
Let's take a look at the inheritance diagram of the Steplistener.java interface:
Stepexecutionlistener
Stepexecutionlistener represents the most generic listener for Step execution. It allows for notification before a Step was started and after it had ends, whether it ended normally or failed
SkipListener
Itemreadlistener, Itemprocesslistener, and itemwritelistner all provide mechanisms for being notified of errors, but none Would inform you, a record has actually been skipped. Onwriteerror, for example, would be called even if a item is retried and successful. For this reason, there are a separate interface for tracking skipped items:
Public interface skiplistener<t,s> extends Steplistener {void Onskipinread (Throwable T); void Onskipinprocess (t item, Throwable t); void Onskipinwrite (S item, Throwable t);}
Onskipinread'll be called whenever a item is a skipped while reading. It should is noted that rollbacks could cause the same item to being registered as skipped more than once. Onskipinwrite would be called if an item was skipped while writing. Because the item has been read successfully (and not skipped), it's also provided the item itself as an argument.
Skiplisteners and transactions
One of the most common use cases for a SkipListener are to log out a skipped item, so that another batch process or Eve n human process can be used to evaluate and fix the issue leading to the skip. Because There is many cases in which the original transaction is rolled back, Spring Batch makes both guarantees:
The appropriate skip method (depending on when the error happened) is only being called once per item.
The SkipListener would always be called just before the transaction is committed. This was to ensure the transactional resources call by the listener was not rolled back by a failure within the ITEMWR Iter.
Chunklistener
A chunk is defined as the items processed within the scope of a transaction. Committing a transaction, at each commit interval, commits a ' chunk '. A chunklistener can useful to perform logic before a chunk begins processing or after a chunk have completed successfull Y:
Public interface Chunklistener extends Steplistener {void Beforechunk (); void Afterchunk ();}
The Beforechunk method is called after the transaction was started, but before read was called on the Itemreader. Conversely, Afterchunk is called after the chunk have been committed (and not at all if there is a rollback).
Above the example of a few listener, then these listener how to use: Take Skiplistener for example, first to understand the Skiplistener interface:
We choose a skiplistenersupport to implement the SkipListener of our custom logic by inheriting it . as follows:
Myskiplistener.java
Package com.lyx.batch3;import org.springframework.batch.core.listener.skiplistenersupport;import com.lyx.batch.People;import com.lyx.batch.PeopleDESC;public class MySkipListener extends skiplistenersupport<people, peopledesc> {@Overridepublic void Onskipinread (throwable t) {// todo auto-generated method stubsuper.onskipinread ( T); System.out.println (">>>>>>>>>>>>>>skip in read>>> >>>>>>>>>>> ");} @Overridepublic void onskipinwrite (peopledesc item, throwable t) {// todo auto-generated method stubsuper.onskipinwrite (item, t); System.out.println (">>>>>>>>>>>>>>skip in write>> >>>>>>>>>>>> "); System.out.println (">>>>=" + item.tostring ());} /** * */@Overridepublic void onskipinprocess when processor throws a skip include containing exception ( PEOPLE&NBSP;ITEM,&NBSP;THROWABLE&NBSP;T) {// TODO Auto-generated method Stubsuper.onskipinprocess (item, t); System.out.println (">>>>>>>>>>>>>>skip in process>> >>>>>>>>>>>> "); System.out.println (">>>>=" + item.tostring ());}}
The following are the configuration files:
<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" ><batch:step id= "Adddescstep" parent= "Abstractstep" ><batch:tasklet><batch:chunk reader= " Peopleadddescreader " processor=" Allowskipprocessor "writer=" Adddescpeoplewriter " commit-interval=" 2 " skip-limit= ><batch:skippable-exception-classes><!--batch:include Configuration allows exceptions to occur --> <batch:include class= "Com.lyx.batch.InvalidDataException" /></batch: skippable-exception-classes><batch:listeners><!-- Here you can configure multiple Listener --><batch: listener ref= "Sampleskiplistener" /></batch:listeners></batch:chunk></batch: tasklet></batch:step><!-- During job operation, you can monitor JOB&NBsp;--><batch:listeners><batch:listener ref= "Samplelistener" /></batch:listeners ></batch:job><!-- add people desc job end --><bean id= " Samplelistener " class=" Com.lyx.batch3.SampleJobExecutionListener " /><bean id=" Sampleskiplistener " class=" Com.lyx.batch3.MySkipListener " /><bean id=" Peopleadddescreader " parent=" Abstractcursorreader "scope=" Step "><property name=" SQL "><value><! [cdata[select first_name ,last_name from people where first_name like ? or last_name like ]] ></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=" Allowskipprocessor " class=" Com.lyx.batch.AllowSkipProcessor " /><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>
The main configuration is:
<!-- add people desc job begin --><batch:job id= "AddPeopleDescJob" ><batch:step id= "Adddescstep" parent= "Abstractstep" ><batch:tasklet><batch:chunk reader= "Peopleadddescreader" processor= "Allowskipprocessor" writer= "Adddescpeoplewriter" Commit-interval= "2" skip-limit= "><batch:skippable-exception-classes><!--Batch: The include configuration allows exceptions to occur --><batch:include class= "Com.lyx.batch.InvalidDataException" /></ batch:skippable-exception-classes><batch:listeners><!-- Here you can configure multiple Listener --><batch : listener ref= "Sampleskiplistener" /></batch:listeners></batch:chunk></batch: tasklet></batch:step><!-- During job runs, you can monitor Job --><batch:listeners><batch: listener ref= "Samplelistener" /></batch:listeners></batch:job><!-- add people desc job end --≫<bean id= "Samplelistener" class= "Com.lyx.batch3.SampleJobExecutionListener" /><bean id= "Sampleskiplistener" class= "Com.lyx.batch3.MySkipListener" />
Run:
Appmain12.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 step listener skip listener * * @author lenovo * */public class appmain12 {public static void main (String[] args) THROWS&NBsp jobexecutionalreadyrunningexception, jobrestartexception,jobinstancealreadycompleteexception, Jobparametersinvalidexception {long starttime = system.currenttimemillis (); // Get Start time @suppresswarnings ("resource") applicationcontext context = new Classpathxmlapplicationcontext (new string[] { "Classpath:spring-batch-exception-listener.xml" }); 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 (); // Gets the end time System.out.println ("program run time: " + (endtime - starttime) + " MS ");}}
Operation Result:
.........................................................................
Process People desc
Skip Invalid Data!!!!!!!!!!!!!!!!!!!!!!!!
Process People desc
Skip Invalid Data!!!!!!!!!!!!!!!!!!!!!!!!
>>>>>>>>>>>>>>skip in Process>>>>>>>>>> >>>>
>>>>=firstname:lyx, Lastname:lyx
>>>>>>>>>>>>>>skip in Process>>>>>>>>>> >>>>
>>>>=firstname:lyx, Lastname:lyx
Process People desc
Skip Invalid Data!!!!!!!!!!!!!!!!!!!!!!!!
>>>>>>>>>>>>>>skip in Process>>>>>>>>>> >>>>
>>>>=firstname:lyx, Lastname:lyx
Job success .....
November 19, 2014 3:00:06 pm Org.springframework.batch.core.launch.support.SimpleJobLauncher Run
Info: Job: [Flowjob: [Name=addpeopledescjob]] completed with the following parameters: [{}] and the following status: [Compl Eted]
Task completed properly
Program Run time: 8779ms
Summary: Through the skip listener can be seen when the skip occurs, can be captured by listener to the occurrence of the event, so that the data of the skip to record, in order to do further processing.
====================end====================
Spring batch_intercepting Step Execution