1、JobExecutionListener
用于监听批处理作业执行情况,从 JobExecution 中获取运行结果后,根据结果来进行不同的处理。JobExecution 保存着当前批处理Job实例、开始时间,结束时间、状态等
package org.springframework.batch.core;
/**
* Provide callbacks at specific points in the lifecycle of a {@link Job}.
* Implementations can be stateful if they are careful to either ensure thread
* safety, or to use one instance of a listener per job, assuming that job
* instances themselves are not used by more than one thread.
*
* @author Dave Syer
*
*/
public interface JobExecutionListener {
/**
* Callback before a job executes.
*
* @param jobExecution the current {@link JobExecution}
*/
void beforeJob(JobExecution jobExecution);
/**
* Callback after completion of a job. Called after both both successful and
* failed executions. To perform logic on a particular status, use
* "if (jobExecution.getStatus() == BatchStatus.X)".
*
* @param jobExecution the current {@link JobExecution}
*/
void afterJob(JobExecution jobExecution);
}
通过定义类实现JobExecutionListener接口, 为批处理Job建立Listener
<bean id="productJobListener" class="com.geekcap.javaworld.springbatchexample.simple.listener.ProductJobExecutionListener"></bean>
<batch:job id="simpleFileImportJob">
<batch:step id="importFileStep">
<batch:tasklet>
<batch:chunk reader="productReader" processor="productProcessor" writer="productWriter" commit-interval="5" skip-limit="10">
<batch:skippable-exception-classes>
<batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="productJobListener"></batch:listener>
</batch:listeners>
</batch:job>
2、StepExecutionListener
用于监听Step生命周期
<bean id="productStepExecutionListener" class="com.geekcap.javaworld.springbatchexample.simple.listener.ProductStepExecutionListener"></bean>
<batch:job id="simpleFileImportJob">
<batch:step id="importFileStep">
<batch:tasklet>
<batch:chunk reader="productReader" processor="productProcessor" writer="productWriter" commit-interval="5" skip-limit="10">
<batch:skippable-exception-classes>
<batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
</batch:skippable-exception-classes>
</batch:chunk>
</batch:tasklet>
<batch:listeners>
<batch:listener ref="productStepExecutionListener"></batch:listener>
</batch:listeners>
</batch:step>
<batch:listeners>
<batch:listener ref="productJobListener"></batch:listener>
</batch:listeners>
</batch:job>
3、ChunkListener、ItemReadListener、ItemProcessListener、ItemWriteListener