View Javadoc

1   /*
2    * Copyright 2006-2013 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.batch.core.listener;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.springframework.batch.core.JobExecution;
22  import org.springframework.batch.core.JobExecutionListener;
23  import org.springframework.core.Ordered;
24  
25  /**
26   * @author Dave Syer
27   *
28   */
29  public class CompositeJobExecutionListener implements JobExecutionListener {
30  
31  	private OrderedComposite<JobExecutionListener> listeners = new OrderedComposite<JobExecutionListener>();
32  
33  	/**
34  	 * Public setter for the listeners.
35  	 *
36  	 * @param listeners
37  	 */
38  	public void setListeners(List<? extends JobExecutionListener> listeners) {
39  		this.listeners.setItems(listeners);
40  	}
41  
42  	/**
43  	 * Register additional listener.
44  	 *
45  	 * @param jobExecutionListener
46  	 */
47  	public void register(JobExecutionListener jobExecutionListener) {
48  		listeners.add(jobExecutionListener);
49  	}
50  
51  	/**
52  	 * Call the registered listeners in reverse order, respecting and
53  	 * prioritising those that implement {@link Ordered}.
54  	 * @see org.springframework.batch.core.JobExecutionListener#afterJob(org.springframework.batch.core.JobExecution)
55  	 */
56  	@Override
57  	public void afterJob(JobExecution jobExecution) {
58  		for (Iterator<JobExecutionListener> iterator = listeners.reverse(); iterator.hasNext();) {
59  			JobExecutionListener listener = iterator.next();
60  			listener.afterJob(jobExecution);
61  		}
62  	}
63  
64  	/**
65  	 * Call the registered listeners in order, respecting and prioritising those
66  	 * that implement {@link Ordered}.
67  	 * @see org.springframework.batch.core.JobExecutionListener#beforeJob(org.springframework.batch.core.JobExecution)
68  	 */
69  	@Override
70  	public void beforeJob(JobExecution jobExecution) {
71  		for (Iterator<JobExecutionListener> iterator = listeners.iterator(); iterator.hasNext();) {
72  			JobExecutionListener listener = iterator.next();
73  			listener.beforeJob(jobExecution);
74  		}
75  	}
76  
77  }