Skip to content

Commit eaabd24

Browse files
committed
Move enhance method to AbstractStep
1 parent 4dda48d commit eaabd24

File tree

7 files changed

+173
-145
lines changed

7 files changed

+173
-145
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
* @author Michael Minella
6464
* @author Chris Schaefer
6565
* @author Mahmoud Ben Hassine
66+
* @author Taeik Lim
6667
*/
6768
public abstract class AbstractStep implements Step, InitializingBean, BeanNameAware {
6869

@@ -190,6 +191,32 @@ protected void open(ExecutionContext ctx) throws Exception {
190191
protected void close(ExecutionContext ctx) throws Exception {
191192
}
192193

194+
public void enhance(CommonStepProperties properties) {
195+
setJobRepository(properties.getJobRepository());
196+
197+
ObservationRegistry observationRegistry = properties.getObservationRegistry();
198+
if (observationRegistry != null) {
199+
setObservationRegistry(observationRegistry);
200+
}
201+
202+
MeterRegistry meterRegistry = properties.getMeterRegistry();
203+
if (meterRegistry != null) {
204+
setMeterRegistry(meterRegistry);
205+
}
206+
207+
Boolean allowStartIfComplete = properties.getAllowStartIfComplete();
208+
if (allowStartIfComplete != null) {
209+
setAllowStartIfComplete(allowStartIfComplete);
210+
}
211+
212+
setStartLimit(properties.getStartLimit());
213+
214+
List<StepExecutionListener> listeners = properties.getStepExecutionListeners();
215+
if (!listeners.isEmpty()) {
216+
setStepExecutionListeners(listeners.toArray(new StepExecutionListener[0]));
217+
}
218+
}
219+
193220
/**
194221
* Template method for step execution logic - calls abstract methods for resource
195222
* initialization ( {@link #open(ExecutionContext)}), execution logic
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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 io.micrometer.core.instrument.MeterRegistry;
22+
import io.micrometer.core.instrument.Metrics;
23+
import io.micrometer.observation.ObservationRegistry;
24+
25+
import org.springframework.batch.core.Step;
26+
import org.springframework.batch.core.StepExecutionListener;
27+
import org.springframework.batch.core.repository.JobRepository;
28+
29+
/**
30+
* A common properties for {@link Step}.
31+
*
32+
* @author Taeik Lim
33+
*/
34+
public class CommonStepProperties {
35+
36+
private String name;
37+
38+
private int startLimit = Integer.MAX_VALUE;
39+
40+
private Boolean allowStartIfComplete;
41+
42+
private JobRepository jobRepository;
43+
44+
private ObservationRegistry observationRegistry = ObservationRegistry.NOOP;
45+
46+
private MeterRegistry meterRegistry = Metrics.globalRegistry;
47+
48+
private List<StepExecutionListener> stepExecutionListeners = new ArrayList<>();
49+
50+
public CommonStepProperties() {
51+
}
52+
53+
public CommonStepProperties(CommonStepProperties properties) {
54+
this.name = properties.name;
55+
this.startLimit = properties.startLimit;
56+
this.allowStartIfComplete = properties.allowStartIfComplete;
57+
this.jobRepository = properties.jobRepository;
58+
this.observationRegistry = properties.observationRegistry;
59+
this.meterRegistry = properties.meterRegistry;
60+
this.stepExecutionListeners = new ArrayList<>(properties.stepExecutionListeners);
61+
}
62+
63+
public String getName() {
64+
return name;
65+
}
66+
67+
public void setName(String name) {
68+
this.name = name;
69+
}
70+
71+
public Integer getStartLimit() {
72+
return startLimit;
73+
}
74+
75+
public void setStartLimit(Integer startLimit) {
76+
this.startLimit = startLimit;
77+
}
78+
79+
public Boolean getAllowStartIfComplete() {
80+
return allowStartIfComplete;
81+
}
82+
83+
public void setAllowStartIfComplete(Boolean allowStartIfComplete) {
84+
this.allowStartIfComplete = allowStartIfComplete;
85+
}
86+
87+
public JobRepository getJobRepository() {
88+
return jobRepository;
89+
}
90+
91+
public void setJobRepository(JobRepository jobRepository) {
92+
this.jobRepository = jobRepository;
93+
}
94+
95+
public ObservationRegistry getObservationRegistry() {
96+
return observationRegistry;
97+
}
98+
99+
public void setObservationRegistry(ObservationRegistry observationRegistry) {
100+
this.observationRegistry = observationRegistry;
101+
}
102+
103+
public MeterRegistry getMeterRegistry() {
104+
return meterRegistry;
105+
}
106+
107+
public void setMeterRegistry(MeterRegistry meterRegistry) {
108+
this.meterRegistry = meterRegistry;
109+
}
110+
111+
public List<StepExecutionListener> getStepExecutionListeners() {
112+
return stepExecutionListeners;
113+
}
114+
115+
public void addStepExecutionListeners(List<StepExecutionListener> stepExecutionListeners) {
116+
this.stepExecutionListeners.addAll(stepExecutionListeners);
117+
}
118+
119+
public void addStepExecutionListener(StepExecutionListener stepExecutionListener) {
120+
this.stepExecutionListeners.add(stepExecutionListener);
121+
}
122+
123+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import java.util.Set;
2222

2323
import org.springframework.batch.core.ChunkListener;
24-
import org.springframework.batch.core.Step;
2524
import org.springframework.batch.core.StepExecutionListener;
2625
import org.springframework.batch.core.annotation.AfterChunk;
2726
import org.springframework.batch.core.annotation.AfterChunkError;
2827
import org.springframework.batch.core.annotation.BeforeChunk;
2928
import org.springframework.batch.core.listener.StepListenerFactoryBean;
29+
import org.springframework.batch.core.step.AbstractStep;
30+
import org.springframework.batch.core.step.CommonStepProperties;
3031
import org.springframework.batch.core.step.tasklet.Tasklet;
3132
import org.springframework.batch.core.step.tasklet.TaskletStep;
3233
import org.springframework.batch.item.ItemStream;
@@ -49,6 +50,7 @@
4950
* @author Dave Syer
5051
* @author Michael Minella
5152
* @author Mahmoud Ben Hassine
53+
* @author Taeik Lim
5254
* @since 2.2
5355
* @param <B> the type of builder represented
5456
*/
@@ -78,8 +80,8 @@ public AbstractTaskletStepBuilder(StepBuilderHelper<?> parent) {
7880

7981
/**
8082
* 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.
83+
* to {@link AbstractStep#enhance(CommonStepProperties)} and then to
84+
* {@link #createTasklet()} in subclasses to create the actual tasklet.
8385
* @return a tasklet step fully configured and ready to execute
8486
*/
8587
public TaskletStep build() {
@@ -88,7 +90,7 @@ public TaskletStep build() {
8890

8991
TaskletStep step = new TaskletStep(getName());
9092

91-
super.enhance(step);
93+
step.enhance(properties);
9294

9395
step.setChunkListeners(chunkListeners.toArray(new ChunkListener[0]));
9496

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);

0 commit comments

Comments
 (0)