|
1 |
| -/* |
2 |
| - * Copyright 2006-2019 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 |
| - * https://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.job.flow; |
17 |
| - |
18 |
| -import java.util.Collection; |
19 |
| -import java.util.Map; |
20 |
| -import java.util.concurrent.ConcurrentHashMap; |
21 |
| - |
22 |
| -import org.springframework.batch.core.Job; |
23 |
| -import org.springframework.batch.core.JobExecution; |
24 |
| -import org.springframework.batch.core.JobExecutionException; |
25 |
| -import org.springframework.batch.core.Step; |
26 |
| -import org.springframework.batch.core.job.AbstractJob; |
27 |
| -import org.springframework.batch.core.job.SimpleStepHandler; |
28 |
| -import org.springframework.batch.core.step.StepHolder; |
29 |
| -import org.springframework.batch.core.step.StepLocator; |
30 |
| - |
31 |
| -/** |
32 |
| - * Implementation of the {@link Job} interface that allows for complex flows of |
33 |
| - * steps, rather than requiring sequential execution. In general, this job |
34 |
| - * implementation was designed to be used behind a parser, allowing for a |
35 |
| - * namespace to abstract away details. |
36 |
| - * |
37 |
| - * @author Dave Syer |
38 |
| - * @author Mahmoud Ben Hassine |
39 |
| - * @since 2.0 |
40 |
| - */ |
41 |
| -public class FlowJob extends AbstractJob { |
42 |
| - |
43 |
| - protected Flow flow; |
44 |
| - |
45 |
| - private Map<String, Step> stepMap = new ConcurrentHashMap<>(); |
46 |
| - |
47 |
| - private volatile boolean initialized = false; |
48 |
| - |
49 |
| - /** |
50 |
| - * Create a {@link FlowJob} with null name and no flow (invalid state). |
51 |
| - */ |
52 |
| - public FlowJob() { |
53 |
| - super(); |
54 |
| - } |
55 |
| - |
56 |
| - /** |
57 |
| - * Create a {@link FlowJob} with provided name and no flow (invalid state). |
58 |
| - * |
59 |
| - * @param name the name to be associated with the FlowJob. |
60 |
| - */ |
61 |
| - public FlowJob(String name) { |
62 |
| - super(name); |
63 |
| - } |
64 |
| - |
65 |
| - /** |
66 |
| - * Public setter for the flow. |
67 |
| - * |
68 |
| - * @param flow the flow to set |
69 |
| - */ |
70 |
| - public void setFlow(Flow flow) { |
71 |
| - this.flow = flow; |
72 |
| - } |
73 |
| - |
74 |
| - /** |
75 |
| - * {@inheritDoc} |
76 |
| - */ |
77 |
| - @Override |
78 |
| - public Step getStep(String stepName) { |
79 |
| - if (!initialized) { |
80 |
| - init(); |
81 |
| - } |
82 |
| - return stepMap.get(stepName); |
83 |
| - } |
84 |
| - |
85 |
| - /** |
86 |
| - * Initialize the step names |
87 |
| - */ |
88 |
| - private void init() { |
89 |
| - findSteps(flow, stepMap); |
90 |
| - } |
91 |
| - |
92 |
| - /** |
93 |
| - * @param flow |
94 |
| - * @param map |
95 |
| - */ |
96 |
| - private void findSteps(Flow flow, Map<String, Step> map) { |
97 |
| - |
98 |
| - for (State state : flow.getStates()) { |
99 |
| - if (state instanceof StepLocator) { |
100 |
| - StepLocator locator = (StepLocator) state; |
101 |
| - for (String name : locator.getStepNames()) { |
102 |
| - map.put(name, locator.getStep(name)); |
103 |
| - } |
104 |
| - } else if (state instanceof StepHolder) { |
105 |
| - Step step = ((StepHolder) state).getStep(); |
106 |
| - String name = step.getName(); |
107 |
| - stepMap.put(name, step); |
108 |
| - } |
109 |
| - else if (state instanceof FlowHolder) { |
110 |
| - for (Flow subflow : ((FlowHolder) state).getFlows()) { |
111 |
| - findSteps(subflow, map); |
112 |
| - } |
113 |
| - } |
114 |
| - } |
115 |
| - |
116 |
| - } |
117 |
| - |
118 |
| - /** |
119 |
| - * {@inheritDoc} |
120 |
| - */ |
121 |
| - @Override |
122 |
| - public Collection<String> getStepNames() { |
123 |
| - if (!initialized) { |
124 |
| - init(); |
125 |
| - } |
126 |
| - return stepMap.keySet(); |
127 |
| - } |
128 |
| - |
129 |
| - /** |
130 |
| - * @see AbstractJob#doExecute(JobExecution) |
131 |
| - */ |
132 |
| - @Override |
133 |
| - protected void doExecute(final JobExecution execution) throws JobExecutionException { |
134 |
| - try { |
135 |
| - JobFlowExecutor executor = new JobFlowExecutor(getJobRepository(), |
136 |
| - new SimpleStepHandler(getJobRepository()), execution); |
137 |
| - executor.updateJobExecutionStatus(flow.start(executor).getStatus()); |
138 |
| - } |
139 |
| - catch (FlowExecutionException e) { |
140 |
| - if (e.getCause() instanceof JobExecutionException) { |
141 |
| - throw (JobExecutionException) e.getCause(); |
142 |
| - } |
143 |
| - throw new JobExecutionException("Flow execution ended unexpectedly", e); |
144 |
| - } |
145 |
| - } |
146 |
| - |
147 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2006-2022 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 | + * https://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.job.flow; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.concurrent.ConcurrentHashMap; |
| 21 | + |
| 22 | +import org.springframework.batch.core.Job; |
| 23 | +import org.springframework.batch.core.JobExecution; |
| 24 | +import org.springframework.batch.core.JobExecutionException; |
| 25 | +import org.springframework.batch.core.Step; |
| 26 | +import org.springframework.batch.core.job.AbstractJob; |
| 27 | +import org.springframework.batch.core.job.SimpleStepHandler; |
| 28 | +import org.springframework.batch.core.step.StepHolder; |
| 29 | +import org.springframework.batch.core.step.StepLocator; |
| 30 | + |
| 31 | +/** |
| 32 | + * Implementation of the {@link Job} interface that allows for complex flows of |
| 33 | + * steps, rather than requiring sequential execution. In general, this job |
| 34 | + * implementation was designed to be used behind a parser, allowing for a |
| 35 | + * namespace to abstract away details. |
| 36 | + * |
| 37 | + * @author Dave Syer |
| 38 | + * @author Mahmoud Ben Hassine |
| 39 | + * @author Taeik Lim |
| 40 | + * @since 2.0 |
| 41 | + */ |
| 42 | +public class FlowJob extends AbstractJob { |
| 43 | + |
| 44 | + protected Flow flow; |
| 45 | + |
| 46 | + private Map<String, Step> stepMap = new ConcurrentHashMap<>(); |
| 47 | + |
| 48 | + private volatile boolean initialized = false; |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a {@link FlowJob} with null name and no flow (invalid state). |
| 52 | + */ |
| 53 | + public FlowJob() { |
| 54 | + super(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Create a {@link FlowJob} with provided name and no flow (invalid state). |
| 59 | + * |
| 60 | + * @param name the name to be associated with the FlowJob. |
| 61 | + */ |
| 62 | + public FlowJob(String name) { |
| 63 | + super(name); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Public setter for the flow. |
| 68 | + * |
| 69 | + * @param flow the flow to set |
| 70 | + */ |
| 71 | + public void setFlow(Flow flow) { |
| 72 | + this.flow = flow; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritDoc} |
| 77 | + */ |
| 78 | + @Override |
| 79 | + public Step getStep(String stepName) { |
| 80 | + if (!initialized) { |
| 81 | + init(); |
| 82 | + } |
| 83 | + return stepMap.get(stepName); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Initialize the step names |
| 88 | + */ |
| 89 | + private void init() { |
| 90 | + findSteps(flow, stepMap); |
| 91 | + initialized = true; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @param flow |
| 96 | + * @param map |
| 97 | + */ |
| 98 | + private void findSteps(Flow flow, Map<String, Step> map) { |
| 99 | + |
| 100 | + for (State state : flow.getStates()) { |
| 101 | + if (state instanceof StepLocator) { |
| 102 | + StepLocator locator = (StepLocator) state; |
| 103 | + for (String name : locator.getStepNames()) { |
| 104 | + map.put(name, locator.getStep(name)); |
| 105 | + } |
| 106 | + } else if (state instanceof StepHolder) { |
| 107 | + Step step = ((StepHolder) state).getStep(); |
| 108 | + String name = step.getName(); |
| 109 | + stepMap.put(name, step); |
| 110 | + } |
| 111 | + else if (state instanceof FlowHolder) { |
| 112 | + for (Flow subflow : ((FlowHolder) state).getFlows()) { |
| 113 | + findSteps(subflow, map); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * {@inheritDoc} |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public Collection<String> getStepNames() { |
| 125 | + if (!initialized) { |
| 126 | + init(); |
| 127 | + } |
| 128 | + return stepMap.keySet(); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @see AbstractJob#doExecute(JobExecution) |
| 133 | + */ |
| 134 | + @Override |
| 135 | + protected void doExecute(final JobExecution execution) throws JobExecutionException { |
| 136 | + try { |
| 137 | + JobFlowExecutor executor = new JobFlowExecutor(getJobRepository(), |
| 138 | + new SimpleStepHandler(getJobRepository()), execution); |
| 139 | + executor.updateJobExecutionStatus(flow.start(executor).getStatus()); |
| 140 | + } |
| 141 | + catch (FlowExecutionException e) { |
| 142 | + if (e.getCause() instanceof JobExecutionException) { |
| 143 | + throw (JobExecutionException) e.getCause(); |
| 144 | + } |
| 145 | + throw new JobExecutionException("Flow execution ended unexpectedly", e); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
0 commit comments