1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.listener;
17
18 import java.util.Arrays;
19 import java.util.Iterator;
20
21 import org.springframework.batch.core.ExitStatus;
22 import org.springframework.batch.core.StepExecution;
23 import org.springframework.batch.core.StepExecutionListener;
24 import org.springframework.core.Ordered;
25
26
27
28
29
30
31 public class CompositeStepExecutionListener implements StepExecutionListener {
32
33 private OrderedComposite<StepExecutionListener> list = new OrderedComposite<StepExecutionListener>();
34
35
36
37
38
39
40 public void setListeners(StepExecutionListener[] listeners) {
41 list.setItems(Arrays.asList(listeners));
42 }
43
44
45
46
47
48
49 public void register(StepExecutionListener stepExecutionListener) {
50 list.add(stepExecutionListener);
51 }
52
53
54
55
56
57
58 @Override
59 public ExitStatus afterStep(StepExecution stepExecution) {
60 for (Iterator<StepExecutionListener> iterator = list.reverse(); iterator.hasNext();) {
61 StepExecutionListener listener = iterator.next();
62 ExitStatus close = listener.afterStep(stepExecution);
63 stepExecution.setExitStatus(stepExecution.getExitStatus().and(close));
64 }
65 return stepExecution.getExitStatus();
66 }
67
68
69
70
71
72
73 @Override
74 public void beforeStep(StepExecution stepExecution) {
75 for (Iterator<StepExecutionListener> iterator = list.iterator(); iterator.hasNext();) {
76 StepExecutionListener listener = iterator.next();
77 listener.beforeStep(stepExecution);
78 }
79 }
80
81 }