001/*
002 * Copyright 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.listener;
017
018import java.util.Iterator;
019import java.util.List;
020import javax.batch.api.chunk.listener.RetryReadListener;
021
022/**
023 * <p>
024 * Composite class holding {@link RetryReadListener}'s.
025 * </p>
026 *
027 * @author Chris Schaefer
028 * @since 3.0
029 */
030public class CompositeRetryReadListener implements RetryReadListener {
031        private OrderedComposite<RetryReadListener> listeners = new OrderedComposite<RetryReadListener>();
032
033        /**
034         * <p>
035         * Public setter for the {@link RetryReadListener}'s.
036         * </p>
037         *
038         * @param listeners the {@link RetryReadListener}'s to set
039         */
040        public void setListeners(List<? extends RetryReadListener> listeners) {
041                this.listeners.setItems(listeners);
042        }
043
044        /**
045         * <p>
046         * Register an additional {@link RetryReadListener}.
047         * </p>
048         *
049         * @param listener the {@link RetryReadListener} to register
050         */
051        public void register(RetryReadListener listener) {
052                listeners.add(listener);
053        }
054
055        @Override
056        public void onRetryReadException(Exception ex) throws Exception {
057                for (Iterator<RetryReadListener> iterator = listeners.reverse(); iterator.hasNext();) {
058                        RetryReadListener listener = iterator.next();
059                        listener.onRetryReadException(ex);
060                }
061        }
062}