001/*
002 * Copyright 2006-2018 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 org.springframework.batch.core.scope.context.ChunkContext;
019
020/**
021 * Listener interface for the lifecycle of a chunk.  A chunk
022 * can be thought of as a collection of items that will be
023 * committed together.
024 *
025 * @author Lucas Ward
026 * @author Michael Minella
027 * @author Mahmoud Ben Hassine
028 *
029 */
030public interface ChunkListener extends StepListener {
031
032        static final String ROLLBACK_EXCEPTION_KEY = "sb_rollback_exception";
033
034        /**
035         * Callback before the chunk is executed, but inside the transaction.
036         *
037         * @param context The current {@link ChunkContext}
038         */
039        void beforeChunk(ChunkContext context);
040
041        /**
042         * Callback after the chunk is executed, outside the transaction.
043         *
044         * @param context The current {@link ChunkContext}
045         */
046        void afterChunk(ChunkContext context);
047
048        /**
049         * Callback after a chunk has been marked for rollback.  It is invoked
050         * after transaction rollback.  While the rollback will have occurred,
051         * transactional resources might still be active and accessible.  Due to
052         * this, data access code within this callback will still "participate" in
053         * the original transaction unless it declares that it runs in its own
054         * transaction.  Hence: <em> Use PROPAGATION_REQUIRES_NEW for any
055         * transactional operation that is called from here.</em>
056         *
057         * @param context the chunk context containing the exception that caused
058         * the underlying rollback.
059         */
060        void afterChunkError(ChunkContext context);
061}