001/*
002 * Copyright 2006-2013 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;
017
018import java.util.List;
019
020import org.springframework.batch.core.scope.context.ChunkContext;
021import org.springframework.batch.item.ItemWriter;
022
023/**
024 * <p>
025 * Listener interface for the writing of items.  Implementations
026 * of this interface will be notified before, after, and in case
027 * of any exception thrown while writing a list of items.
028 * </p>
029 *
030 * <p>
031 * <em>Note: </em> This listener is designed to work around the
032 * lifecycle of an item.  This means that each method should be
033 * called once within the lifecycle of an item and in fault
034 * tolerant scenarios, any transactional work that is done in
035 * one of these methods would be rolled back and not re-applied.
036 * Because of this, it is recommended to not perform any logic
037 * using this listener that participates in a transaction.
038 *</p>
039 *
040 * @author Lucas Ward
041 *
042 */
043public interface ItemWriteListener<S> extends StepListener {
044
045        /**
046         * Called before {@link ItemWriter#write(java.util.List)}
047         *
048         * @param items to be written
049         */
050        void beforeWrite(List<? extends S> items);
051
052        /**
053         * Called after {@link ItemWriter#write(java.util.List)} This will be
054         * called before any transaction is committed, and before
055         * {@link ChunkListener#afterChunk(ChunkContext)}
056         *
057         * @param items written items
058         */
059        void afterWrite(List<? extends S> items);
060
061        /**
062         * Called if an error occurs while trying to write. Will be called inside a
063         * transaction, but the transaction will normally be rolled back. There is
064         * no way to identify from this callback which of the items (if any) caused
065         * the error.
066         *
067         * @param exception thrown from {@link ItemWriter}
068         * @param items attempted to be written.
069         */
070        void onWriteError(Exception exception, List<? extends S> items);
071}