Spring Batch 中的Listener

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值