从头认识SpringBatch批处理框架---SkipListener

本文详细介绍了Spring Batch中SkipListener接口的功能与使用方法,包括其三个核心方法onSkipInRead、onSkipInWrite和onSkipInProcess的作用及应用场景,并提供了一个自定义SkipListener的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SkipListener在chunk处理阶段抛出跳过定义的异常时触发,在chunk的读、处理、写阶段发生的异常都会触发该拦截器


SkipListener接口声明:

/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.batch.core;

/**
 * Interface for listener to skipped items. Callbacks will be called by
 * {@link Step} implementations at the appropriate time in the step lifecycle.
 * Implementers of this interface should not assume that any method will be 
 * called immediately after an error has been encountered.  Because there 
 * may be errors later on in processing the chunk, this listener will not be
 * called until just before committing.
 * 
 * @author Dave Syer
 * @author Robert Kasanicky
 * 
 */
public interface SkipListener<T,S> extends StepListener {

	/**
	 * Callback for a failure on read that is legal, so is not going to be
	 * re-thrown. In case transaction is rolled back and items are re-read, this
	 * callback will occur repeatedly for the same cause.  This will only happen
	 * if read items are not buffered.
	 * 
	 * @param t cause of the failure
	 */
	void onSkipInRead(Throwable t);

	/**
	 * This item failed on write with the given exception, and a skip was called
	 * for. 
	 * 
	 * @param item the failed item
	 * @param t the cause of the failure
	 */
	void onSkipInWrite(S item, Throwable t);

	/**
	 * This item failed on processing with the given exception, and a skip was called
	 * for. 
	 * 
	 * @param item the failed item
	 * @param t the cause of the failure
	 */
	void onSkipInProcess(T item, Throwable t);

}
SkipListener方法说明及注解定义

方法方法说明注解定义
onSkipInRead(Throwable t)在读阶段发生异常并且配置了异常可以跳过时触发该操作@OnSkipInRead
onSkipInWrite(String item, Throwable t)在写阶段发生异常并且配置了异常可以跳过时触发该操作@OnSkipInWrite
onSkipInProcess(String item, Throwable t)在处理阶段发生异常并且配置了异常可以跳过时触发该操作@OnSkipInProcess

SkipListener的默认实现接口

CompositeSkipListener ---组合跳过拦截器

MulticasterBatchListener--组合策略跳过拦截器

SkipListenerSupport----跳过拦截器默认实现


自定义SkipListener实现示例:

/**
 * 
 */
package com.juxtapose.example.ch05.step.listener;

import org.springframework.batch.core.SkipListener;
import org.springframework.batch.core.annotation.OnSkipInProcess;
import org.springframework.batch.item.file.FlatFileParseException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 自定义SkipListener
 * @author wbw
 *
 */
public class DBSkipListener implements SkipListener<String, String> {
	private JdbcTemplate jdbcTemplate;

	@OnSkipInProcess
	public void onSkipInRead(Throwable t) {
		if (t instanceof FlatFileParseException) {
			jdbcTemplate.update("insert into skipbills "
					+ "(line,content) values (?,?)",
					((FlatFileParseException) t).getLineNumber(),
					((FlatFileParseException) t).getInput());
		}
	}

	public void onSkipInWrite(String item, Throwable t) {
	}

	public void onSkipInProcess(String item, Throwable t) {
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值