Skip to content

Commit 57505ba

Browse files
committed
Move enhance method to AbstractStep
1 parent 724cc97 commit 57505ba

File tree

7 files changed

+138
-108
lines changed

7 files changed

+138
-108
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
* @author Michael Minella
6161
* @author Chris Schaefer
6262
* @author Mahmoud Ben Hassine
63+
* @author Taeik Lim
6364
*/
6465
public abstract class AbstractStep implements Step, InitializingBean, BeanNameAware {
6566

@@ -183,6 +184,22 @@ protected void open(ExecutionContext ctx) throws Exception {
183184
protected void close(ExecutionContext ctx) throws Exception {
184185
}
185186

187+
public void enhance(StepProperties properties) {
188+
setJobRepository(properties.getJobRepository());
189+
190+
Boolean allowStartIfComplete = properties.getAllowStartIfComplete();
191+
if (allowStartIfComplete != null) {
192+
setAllowStartIfComplete(allowStartIfComplete);
193+
}
194+
195+
setStartLimit(properties.getStartLimit());
196+
197+
List<StepExecutionListener> listeners = properties.getStepExecutionListeners();
198+
if (!listeners.isEmpty()) {
199+
setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0]));
200+
}
201+
}
202+
186203
/**
187204
* Template method for step execution logic - calls abstract methods for resource
188205
* initialization ( {@link #open(ExecutionContext)}), execution logic
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.step;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import org.springframework.batch.core.Step;
22+
import org.springframework.batch.core.StepExecutionListener;
23+
import org.springframework.batch.core.repository.JobRepository;
24+
25+
/**
26+
* A common properties for {@link Step}.
27+
*
28+
* @author Taeik Lim
29+
*/
30+
public class StepProperties {
31+
32+
private List<StepExecutionListener> stepExecutionListeners = new ArrayList<>();
33+
34+
private String name;
35+
36+
private int startLimit = Integer.MAX_VALUE;
37+
38+
private Boolean allowStartIfComplete;
39+
40+
private JobRepository jobRepository;
41+
42+
public StepProperties() {
43+
}
44+
45+
public StepProperties(StepProperties properties) {
46+
this.name = properties.name;
47+
this.startLimit = properties.startLimit;
48+
this.allowStartIfComplete = properties.allowStartIfComplete;
49+
this.jobRepository = properties.jobRepository;
50+
this.stepExecutionListeners = new ArrayList<>(properties.stepExecutionListeners);
51+
}
52+
53+
public JobRepository getJobRepository() {
54+
return jobRepository;
55+
}
56+
57+
public void setJobRepository(JobRepository jobRepository) {
58+
this.jobRepository = jobRepository;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public void setName(String name) {
66+
this.name = name;
67+
}
68+
69+
public List<StepExecutionListener> getStepExecutionListeners() {
70+
return stepExecutionListeners;
71+
}
72+
73+
public void addStepExecutionListeners(List<StepExecutionListener> stepExecutionListeners) {
74+
this.stepExecutionListeners.addAll(stepExecutionListeners);
75+
}
76+
77+
public void addStepExecutionListener(StepExecutionListener stepExecutionListener) {
78+
this.stepExecutionListeners.add(stepExecutionListener);
79+
}
80+
81+
public Integer getStartLimit() {
82+
return startLimit;
83+
}
84+
85+
public void setStartLimit(Integer startLimit) {
86+
this.startLimit = startLimit;
87+
}
88+
89+
public Boolean getAllowStartIfComplete() {
90+
return allowStartIfComplete;
91+
}
92+
93+
public void setAllowStartIfComplete(Boolean allowStartIfComplete) {
94+
this.allowStartIfComplete = allowStartIfComplete;
95+
}
96+
97+
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/AbstractTaskletStepBuilder.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.springframework.batch.core.annotation.AfterChunkError;
2828
import org.springframework.batch.core.annotation.BeforeChunk;
2929
import org.springframework.batch.core.listener.StepListenerFactoryBean;
30+
import org.springframework.batch.core.step.AbstractStep;
31+
import org.springframework.batch.core.step.StepProperties;
3032
import org.springframework.batch.core.step.tasklet.Tasklet;
3133
import org.springframework.batch.core.step.tasklet.TaskletStep;
3234
import org.springframework.batch.item.ItemStream;
@@ -49,6 +51,7 @@
4951
* @author Dave Syer
5052
* @author Michael Minella
5153
* @author Mahmoud Ben Hassine
54+
* @author Taeik Lim
5255
* @since 2.2
5356
* @param <B> the type of builder represented
5457
*/
@@ -78,8 +81,8 @@ public AbstractTaskletStepBuilder(StepBuilderHelper<?> parent) {
7881

7982
/**
8083
* Build the step from the components collected by the fluent setters. Delegates first
81-
* to {@link #enhance(Step)} and then to {@link #createTasklet()} in subclasses to
82-
* create the actual tasklet.
84+
* to {@link AbstractStep#enhance(StepProperties)} and then to
85+
* {@link #createTasklet()} in subclasses to create the actual tasklet.
8386
* @return a tasklet step fully configured and ready to execute
8487
*/
8588
public TaskletStep build() {
@@ -88,7 +91,7 @@ public TaskletStep build() {
8891

8992
TaskletStep step = new TaskletStep(getName());
9093

91-
super.enhance(step);
94+
step.enhance(properties);
9295

9396
step.setChunkListeners(chunkListeners.toArray(new ChunkListener[0]));
9497

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FlowStepBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* nested flow composed of other steps.
2525
*
2626
* @author Dave Syer
27+
* @author Taeik Lim
2728
* @since 2.2
2829
*/
2930
public class FlowStepBuilder extends StepBuilderHelper<FlowStepBuilder> {
@@ -59,7 +60,7 @@ public Step build() {
5960
FlowStep step = new FlowStep();
6061
step.setName(getName());
6162
step.setFlow(flow);
62-
super.enhance(step);
63+
step.enhance(properties);
6364
try {
6465
step.afterPropertiesSet();
6566
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/JobStepBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* with parameters taken from the parent job or from the step execution.
2828
*
2929
* @author Dave Syer
30+
* @author Taeik Lim
3031
* @since 2.2
3132
*/
3233
public class JobStepBuilder extends StepBuilderHelper<JobStepBuilder> {
@@ -85,7 +86,7 @@ public Step build() {
8586

8687
JobStep step = new JobStep();
8788
step.setName(getName());
88-
super.enhance(step);
89+
step.enhance(properties);
8990
if (job != null) {
9091
step.setJob(job);
9192
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/PartitionStepBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Dave Syer
3535
* @author Mahmoud Ben Hassine
3636
* @author Dimitrios Liapis
37+
* @author Taeik Lim
3738
* @since 2.2
3839
*/
3940
public class PartitionStepBuilder extends StepBuilderHelper<PartitionStepBuilder> {
@@ -161,7 +162,7 @@ public PartitionStepBuilder aggregator(StepExecutionAggregator aggregator) {
161162
public Step build() {
162163
PartitionStep step = new PartitionStep();
163164
step.setName(getName());
164-
super.enhance(step);
165+
step.enhance(properties);
165166

166167
if (partitionHandler != null) {
167168
step.setPartitionHandler(partitionHandler);

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilderHelper.java

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.batch.core.listener.StepListenerFactoryBean;
2525
import org.springframework.batch.core.repository.JobRepository;
2626
import org.springframework.batch.core.step.AbstractStep;
27+
import org.springframework.batch.core.step.StepProperties;
2728
import org.springframework.batch.core.step.tasklet.TaskletStep;
2829
import org.springframework.batch.support.ReflectionUtils;
2930
import org.springframework.transaction.PlatformTransactionManager;
@@ -41,17 +42,18 @@
4142
* @author Dave Syer
4243
* @author Michael Minella
4344
* @author Mahmoud Ben Hassine
45+
* @author Taeik Lim
4446
* @since 2.2
4547
*/
4648
public abstract class StepBuilderHelper<B extends StepBuilderHelper<B>> {
4749

4850
protected final Log logger = LogFactory.getLog(getClass());
4951

50-
protected final CommonStepProperties properties;
52+
protected final StepProperties properties;
5153

5254
public StepBuilderHelper(String name) {
53-
this.properties = new CommonStepProperties();
54-
properties.name = name;
55+
this.properties = new StepProperties();
56+
properties.setName(name);
5557
}
5658

5759
/**
@@ -60,16 +62,16 @@ public StepBuilderHelper(String name) {
6062
* @param parent a parent helper containing common step properties
6163
*/
6264
protected StepBuilderHelper(StepBuilderHelper<?> parent) {
63-
this.properties = new CommonStepProperties(parent.properties);
65+
this.properties = new StepProperties(parent.properties);
6466
}
6567

6668
public B repository(JobRepository jobRepository) {
67-
properties.jobRepository = jobRepository;
69+
properties.setJobRepository(jobRepository);
6870
return self();
6971
}
7072

7173
public B startLimit(int startLimit) {
72-
properties.startLimit = startLimit;
74+
properties.setStartLimit(startLimit);
7375
return self();
7476
}
7577

@@ -98,114 +100,22 @@ public B listener(StepExecutionListener listener) {
98100
}
99101

100102
public B allowStartIfComplete(boolean allowStartIfComplete) {
101-
properties.allowStartIfComplete = allowStartIfComplete;
103+
properties.setAllowStartIfComplete(allowStartIfComplete);
102104
return self();
103105
}
104106

105107
protected abstract B self();
106108

107109
protected String getName() {
108-
return properties.name;
110+
return properties.getName();
109111
}
110112

111113
protected JobRepository getJobRepository() {
112-
return properties.jobRepository;
114+
return properties.getJobRepository();
113115
}
114116

115117
protected boolean isAllowStartIfComplete() {
116-
return properties.allowStartIfComplete != null ? properties.allowStartIfComplete : false;
117-
}
118-
119-
protected void enhance(Step target) {
120-
121-
if (target instanceof AbstractStep) {
122-
123-
AbstractStep step = (AbstractStep) target;
124-
step.setJobRepository(properties.getJobRepository());
125-
126-
Boolean allowStartIfComplete = properties.allowStartIfComplete;
127-
if (allowStartIfComplete != null) {
128-
step.setAllowStartIfComplete(allowStartIfComplete);
129-
}
130-
131-
step.setStartLimit(properties.startLimit);
132-
133-
List<StepExecutionListener> listeners = properties.stepExecutionListeners;
134-
if (!listeners.isEmpty()) {
135-
step.setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0]));
136-
}
137-
138-
}
139-
140-
}
141-
142-
public static class CommonStepProperties {
143-
144-
private List<StepExecutionListener> stepExecutionListeners = new ArrayList<>();
145-
146-
private int startLimit = Integer.MAX_VALUE;
147-
148-
private Boolean allowStartIfComplete;
149-
150-
private JobRepository jobRepository;
151-
152-
public CommonStepProperties() {
153-
}
154-
155-
public CommonStepProperties(CommonStepProperties properties) {
156-
this.name = properties.name;
157-
this.startLimit = properties.startLimit;
158-
this.allowStartIfComplete = properties.allowStartIfComplete;
159-
this.jobRepository = properties.jobRepository;
160-
this.stepExecutionListeners = new ArrayList<>(properties.stepExecutionListeners);
161-
}
162-
163-
public JobRepository getJobRepository() {
164-
return jobRepository;
165-
}
166-
167-
public void setJobRepository(JobRepository jobRepository) {
168-
this.jobRepository = jobRepository;
169-
}
170-
171-
public String getName() {
172-
return name;
173-
}
174-
175-
public void setName(String name) {
176-
this.name = name;
177-
}
178-
179-
public List<StepExecutionListener> getStepExecutionListeners() {
180-
return stepExecutionListeners;
181-
}
182-
183-
public void addStepExecutionListeners(List<StepExecutionListener> stepExecutionListeners) {
184-
this.stepExecutionListeners.addAll(stepExecutionListeners);
185-
}
186-
187-
public void addStepExecutionListener(StepExecutionListener stepExecutionListener) {
188-
this.stepExecutionListeners.add(stepExecutionListener);
189-
}
190-
191-
public Integer getStartLimit() {
192-
return startLimit;
193-
}
194-
195-
public void setStartLimit(Integer startLimit) {
196-
this.startLimit = startLimit;
197-
}
198-
199-
public Boolean getAllowStartIfComplete() {
200-
return allowStartIfComplete;
201-
}
202-
203-
public void setAllowStartIfComplete(Boolean allowStartIfComplete) {
204-
this.allowStartIfComplete = allowStartIfComplete;
205-
}
206-
207-
private String name;
208-
118+
return properties.getAllowStartIfComplete() != null ? properties.getAllowStartIfComplete() : false;
209119
}
210120

211121
}

0 commit comments

Comments
 (0)