Here we focus on the use of various listeners inside the Springbatch, as well as the delivery of job parameters. The pursuit of the day that it terminates, the process of seeking is the lost process.
Springbatch listener One, job LISTENERS: Listening to operations on the job level
Public Interface Jobexecutionlistener { void beforejob (jobexecution jobexecution); void afterjob (Jobexecution jobexecution);}
- Methods of the class method
<JobID= "Helloworldjob"> <Listeners> <Listenerref= "Jobexeclistener"/> </Listeners> <Step....> </Step></Job>
The definition of a bean
<id= "Jobexeclistener" class= " Spring.batch.helloworld.JobExecListener "/>
The implementation of the bean
PackageSpring.batch.helloworld;Importorg.springframework.batch.core.JobExecution;ImportOrg.springframework.batch.core.JobExecutionListener;/*** @Author: HUHX * @Date: 2017-11-02 a.m. 8:50*/ Public classJobexeclistenerImplementsJobexecutionlistener {@Override Public voidbeforejob (jobexecution jobexecution) {System.out.println ("Before start time:" +jobexecution.getstarttime ()); } @Override Public voidafterjob (jobexecution jobexecution) {System.out.println ("After end time:" +jobexecution.getendtime ()); }}
- Using annotations, the difference is in the writing of the Bean class.
Public class Jobexeclistener { @BeforeJob publicvoid beforejob (jobexecution jobexecution) { System.out.println ("Before start time:" + jobexecution.getstarttime ()); } @AfterJob publicvoid afterjob (jobexecution jobexecution) { System.out.println ("After end time:" + jobexecution.getendtime ());} }
This example is based on the HelloWorld example, you can refer to the blog: springbatch----The use of >springbatch (a). The following is the result of printing
Geneva xx:Hello world! Geneva -xx:
Step LISTENERS: Monitoring the process of step
Springbatch provides us with the following listener for step.
1, Chunklistener called before and after chunk execution2, Itemprocesslistener called before and after an itemprocessor gets a item and when that processor throws an EXCEP tion3, Itemreadlistener called before and after an item isRead and when anexception occurs reading an item4, Itemwritelistener called before and after an item isWritten and when a exception occurs writing an item5, SkipListener Called when a skip occurs whilereading, processing, or writing an item6, Stepexecutionlistener called before and after a step
The interface of the step listener is basically the same as the job listener above. It is also the way to implement the method and how the annotations are implemented.
Third, the use of the parent and abstract attributes
1. Abstract When true to theabstract for configuration. Abstract configuration enti-ties aren ' t Instantiated.2, child elements inherit all the attributes of the parent element, and of course the child element can replicate the properties of the parent element Override them.
Add a step and a job to the Batch.xml file.
<!--Parent and abstract -<StepID= "Parentstep"Abstract= "true"> <TaskletTransaction-manager= "TransactionManager"> <Listeners> <Listenerref= "Parentsteplistener"/> </Listeners> </Tasklet></Step><JobID= "Childjob"> <StepID= "Childstep"Parent= "Parentstep"> <Taskletref= "Childtasklet"Transaction-manager= "TransactionManager"/> </Step></Job><Bean:beanID= "Childtasklet"class= "Spring.batch.parentAbstract.ParentTasklet"Scope= "Step"> <Bean:propertyname= "username"value= "#{jobparameters[' password '}"/></Bean:bean>
Parenttasklet Implementation Class Code:
Packagespring.batch.parentAbstract;Importorg.springframework.batch.core.StepContribution;ImportOrg.springframework.batch.core.scope.context.ChunkContext;ImportOrg.springframework.batch.core.step.tasklet.Tasklet;ImportOrg.springframework.batch.repeat.RepeatStatus;/*** @Author: HUHX * @Date: 2017-11-02 a.m. 9:18*/ Public classParenttaskletImplementsTasklet {PrivateString username; Public voidSetusername (String username) { This. Username =username; } @Override PublicRepeatstatus Execute (stepcontribution contribution, Chunkcontext Chunkcontext)throwsException {System.out.println ("Parent Tasklet" +username); returnrepeatstatus.finished; }
}
The result of the printing is as follows, you can see that childjob already has the parentstep inside the listener.
Before Step.parent tasklet123456after step.
Another Joblaunch.java code is as follows
Public classJoblaunch { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Config/batch/batch.xml"); Joblauncher Launcher= (Joblauncher) context.getbean ("Joblauncher"); Job Job= (Job) context.getbean ("Childjob"); Try { //Run JobJobparametersbuilder Parametersbuilder =NewJobparametersbuilder (); Jobparameters jobparameters=Parametersbuilder. AddString ("username", "Linux"). AddString ("Password", "123456"). Tojobparameters (); Launcher.run (Job, jobparameters); } Catch(Exception e) {e.printstacktrace (); } }}
Friendship Link
Use of Springbatch---->springbatch (iv)