Skip to content

Commit efc6247

Browse files
author
dsyer
committed
IN PROGRESS - issue BATCH-121: BatchResourceFactoryBean - is it adding any value?
http://opensource.atlassian.com/projects/spring/browse/BATCH-121 Expose the StepExecution to the StepContext early enough for a stepOperations interceptor to find it in the open() method. See also BATCH-125.
1 parent 3b7b956 commit efc6247

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public ExitStatus process(final StepConfiguration configuration, final StepExecu
161161
ExitStatus status = ExitStatus.FAILED;
162162

163163
final SimpleStepContext stepScopeContext = StepSynchronizationManager.open();
164+
stepScopeContext.setStepExecution(stepExecution);
164165

165166
try {
166167
stepExecution.setStartTime(new Timestamp(System.currentTimeMillis()));
@@ -182,7 +183,6 @@ public void run() {
182183
stepExecution.getJobExecution().unregisterStepContext(context);
183184
}
184185
});
185-
stepScopeContext.setStepExecution(stepExecution);
186186
context.setAttribute(StepScope.ID_KEY, stepExecution.getJobExecution()
187187
.getJobIdentifier());
188188
// Mark the context as a step context as a hint to scope

execution/src/test/java/org/springframework/batch/execution/step/simple/DefaultStepExecutorTests.java

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.springframework.batch.item.ItemProvider;
4040
import org.springframework.batch.item.provider.ListItemProvider;
4141
import org.springframework.batch.repeat.ExitStatus;
42+
import org.springframework.batch.repeat.RepeatContext;
43+
import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter;
4244
import org.springframework.batch.repeat.policy.SimpleCompletionPolicy;
4345
import org.springframework.batch.repeat.support.RepeatTemplate;
4446
import org.springframework.batch.restart.RestartData;
@@ -58,6 +60,8 @@ public void process(Object data) throws Exception {
5860

5961
private StepConfigurationSupport stepConfiguration;
6062

63+
private RepeatTemplate template;
64+
6165
private ItemProvider getProvider(String[] args) {
6266
return new ListItemProvider(Arrays.asList(args));
6367
}
@@ -85,8 +89,7 @@ protected void setUp() throws Exception {
8589
stepConfiguration = new SimpleStepConfiguration();
8690
stepConfiguration.setTasklet(getTasklet(new String[] { "foo", "bar",
8791
"spam" }));
88-
// Only process one chunk:
89-
RepeatTemplate template = new RepeatTemplate();
92+
template = new RepeatTemplate();
9093
template.setCompletionPolicy(new SimpleCompletionPolicy(1));
9194
stepExecutor.setStepOperations(template);
9295
// Only process one item:
@@ -110,7 +113,7 @@ public void testStepExecutor() throws Exception {
110113

111114
public void testChunkExecutor() throws Exception {
112115

113-
RepeatTemplate template = new RepeatTemplate();
116+
template = new RepeatTemplate();
114117

115118
// Only process one item:
116119
template.setCompletionPolicy(new SimpleCompletionPolicy(1));
@@ -129,7 +132,7 @@ public void testChunkExecutor() throws Exception {
129132

130133
public void testStepContextInitialized() throws Exception {
131134

132-
RepeatTemplate template = new RepeatTemplate();
135+
template = new RepeatTemplate();
133136

134137
// Only process one item:
135138
template.setCompletionPolicy(new SimpleCompletionPolicy(1));
@@ -159,6 +162,35 @@ public ExitStatus execute() throws Exception {
159162

160163
}
161164

165+
public void testStepContextInitializedBeforeTasklet() throws Exception {
166+
167+
template = new RepeatTemplate();
168+
169+
// Only process one chunk:
170+
template.setCompletionPolicy(new SimpleCompletionPolicy(1));
171+
stepExecutor.setStepOperations(template);
172+
173+
final StepInstance step = new StepInstance(new Long(1));
174+
SimpleJobIdentifier jobIdentifier = new SimpleJobIdentifier("FOO");
175+
final JobExecution jobExecution = new JobExecution(new JobInstance(
176+
jobIdentifier, new Long(3)));
177+
final StepExecution stepExecution = new StepExecution(step,
178+
jobExecution);
179+
180+
template.setInterceptor(new RepeatInterceptorAdapter() {
181+
public void open(RepeatContext context) {
182+
assertNotNull(StepSynchronizationManager.getContext()
183+
.getStepExecution());
184+
assertEquals(stepExecution, StepSynchronizationManager.getContext()
185+
.getStepExecution());
186+
}
187+
});
188+
189+
stepExecutor.process(stepConfiguration, stepExecution);
190+
assertEquals(1, processed.size());
191+
192+
}
193+
162194
public void testRepository() throws Exception {
163195

164196
SimpleJobRepository repository = new SimpleJobRepository(

0 commit comments

Comments
 (0)