001/* 002 * Copyright 2006-2008 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.core.step.item; 017 018import java.util.List; 019 020import org.apache.commons.logging.Log; 021import org.apache.commons.logging.LogFactory; 022import org.springframework.batch.core.listener.ItemListenerSupport; 023 024/** 025 * Default implementation of the {@link ItemListenerSupport} class that 026 * writes all exceptions via commons logging. Since generics can't be used to 027 * ensure the list contains exceptions, any non exceptions will be logged out by 028 * calling toString on the object. 029 * 030 * @author Lucas Ward 031 * 032 */ 033public class DefaultItemFailureHandler extends ItemListenerSupport<Object,Object> { 034 035 protected static final Log logger = LogFactory 036 .getLog(DefaultItemFailureHandler.class); 037 038 @Override 039 public void onReadError(Exception ex) { 040 try { 041 logger.error("Error encountered while reading", ex); 042 } catch (Exception exception) { 043 logger.error("Invalid type for logging: [" + exception.toString() 044 + "]"); 045 } 046 } 047 048 @Override 049 public void onWriteError(Exception ex, List<? extends Object> item) { 050 try { 051 logger.error("Error encountered while writing item: [ " + item + "]", ex); 052 } catch (Exception exception) { 053 logger.error("Invalid type for logging: [" + exception.toString() 054 + "]"); 055 } 056 } 057 058}