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.listener;
017
018import java.util.Iterator;
019import java.util.List;
020
021import org.springframework.batch.core.JobExecution;
022import org.springframework.batch.core.JobExecutionListener;
023import org.springframework.core.Ordered;
024
025/**
026 * @author Dave Syer
027 *
028 */
029public class CompositeJobExecutionListener implements JobExecutionListener {
030
031        private OrderedComposite<JobExecutionListener> listeners = new OrderedComposite<JobExecutionListener>();
032
033        /**
034         * Public setter for the listeners.
035         *
036         * @param listeners list of {@link JobExecutionListener}s to be called when job execution events occur.
037         */
038        public void setListeners(List<? extends JobExecutionListener> listeners) {
039                this.listeners.setItems(listeners);
040        }
041
042        /**
043         * Register additional listener.
044         *
045         * @param jobExecutionListener instance {@link JobExecutionListener} to be registered.
046         */
047        public void register(JobExecutionListener jobExecutionListener) {
048                listeners.add(jobExecutionListener);
049        }
050
051        /**
052         * Call the registered listeners in reverse order, respecting and
053         * prioritising those that implement {@link Ordered}.
054         * @see org.springframework.batch.core.JobExecutionListener#afterJob(org.springframework.batch.core.JobExecution)
055         */
056        @Override
057        public void afterJob(JobExecution jobExecution) {
058                for (Iterator<JobExecutionListener> iterator = listeners.reverse(); iterator.hasNext();) {
059                        JobExecutionListener listener = iterator.next();
060                        listener.afterJob(jobExecution);
061                }
062        }
063
064        /**
065         * Call the registered listeners in order, respecting and prioritising those
066         * that implement {@link Ordered}.
067         * @see org.springframework.batch.core.JobExecutionListener#beforeJob(org.springframework.batch.core.JobExecution)
068         */
069        @Override
070        public void beforeJob(JobExecution jobExecution) {
071                for (Iterator<JobExecutionListener> iterator = listeners.iterator(); iterator.hasNext();) {
072                        JobExecutionListener listener = iterator.next();
073                        listener.beforeJob(jobExecution);
074                }
075        }
076
077}