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 org.springframework.batch.core.ExitStatus;
19 import org.springframework.batch.core.Job;
20 import org.springframework.batch.core.Step;
21 import org.springframework.batch.core.StepExecution;
22 import org.springframework.batch.item.ExecutionContext;
23 import org.springframework.batch.support.PatternMatcher;
24 import org.springframework.beans.factory.InitializingBean;
25 import org.springframework.util.Assert;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class ExecutionContextPromotionListener extends StepExecutionListenerSupport implements InitializingBean {
42
43 private String[] keys = null;
44
45 private String[] statuses = new String[] { ExitStatus.COMPLETED.getExitCode() };
46
47 private boolean strict = false;
48
49 @Override
50 public ExitStatus afterStep(StepExecution stepExecution) {
51 ExecutionContext stepContext = stepExecution.getExecutionContext();
52 ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext();
53 String exitCode = stepExecution.getExitStatus().getExitCode();
54 for (String statusPattern : statuses) {
55 if (PatternMatcher.match(statusPattern, exitCode)) {
56 for (String key : keys) {
57 if (stepContext.containsKey(key)) {
58 jobContext.put(key, stepContext.get(key));
59 } else {
60 if (strict) {
61 throw new IllegalArgumentException("The key [" + key
62 + "] was not found in the Step's ExecutionContext.");
63 }
64 }
65 }
66 break;
67 }
68 }
69
70 return null;
71 }
72
73 @Override
74 public void afterPropertiesSet() throws Exception {
75 Assert.notNull(this.keys, "The 'keys' property must be provided");
76 Assert.notEmpty(this.keys, "The 'keys' property must not be empty");
77 Assert.notNull(this.statuses, "The 'statuses' property must be provided");
78 Assert.notEmpty(this.statuses, "The 'statuses' property must not be empty");
79 }
80
81
82
83
84
85 public void setKeys(String[] keys) {
86 this.keys = keys;
87 }
88
89
90
91
92
93
94 public void setStatuses(String[] statuses) {
95 this.statuses = statuses;
96 }
97
98
99
100
101
102
103
104 public void setStrict(boolean strict) {
105 this.strict = strict;
106 }
107
108 }