From efc6247e90a1817588adf3910380b91645e96604 Mon Sep 17 00:00:00 2001 From: dsyer Date: Fri, 5 Oct 2007 16:04:08 +0000 Subject: [PATCH] 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. --- .../step/simple/SimpleStepExecutor.java | 2 +- .../step/simple/DefaultStepExecutorTests.java | 40 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java b/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java index fa36d52cc..3c86473eb 100644 --- a/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java +++ b/execution/src/main/java/org/springframework/batch/execution/step/simple/SimpleStepExecutor.java @@ -161,6 +161,7 @@ public class SimpleStepExecutor implements StepExecutor { ExitStatus status = ExitStatus.FAILED; final SimpleStepContext stepScopeContext = StepSynchronizationManager.open(); + stepScopeContext.setStepExecution(stepExecution); try { stepExecution.setStartTime(new Timestamp(System.currentTimeMillis())); @@ -182,7 +183,6 @@ public class SimpleStepExecutor implements StepExecutor { stepExecution.getJobExecution().unregisterStepContext(context); } }); - stepScopeContext.setStepExecution(stepExecution); context.setAttribute(StepScope.ID_KEY, stepExecution.getJobExecution() .getJobIdentifier()); // Mark the context as a step context as a hint to scope diff --git a/execution/src/test/java/org/springframework/batch/execution/step/simple/DefaultStepExecutorTests.java b/execution/src/test/java/org/springframework/batch/execution/step/simple/DefaultStepExecutorTests.java index b3b57d0b8..5df6ea2de 100644 --- a/execution/src/test/java/org/springframework/batch/execution/step/simple/DefaultStepExecutorTests.java +++ b/execution/src/test/java/org/springframework/batch/execution/step/simple/DefaultStepExecutorTests.java @@ -39,6 +39,8 @@ import org.springframework.batch.item.ItemProcessor; import org.springframework.batch.item.ItemProvider; import org.springframework.batch.item.provider.ListItemProvider; import org.springframework.batch.repeat.ExitStatus; +import org.springframework.batch.repeat.RepeatContext; +import org.springframework.batch.repeat.interceptor.RepeatInterceptorAdapter; import org.springframework.batch.repeat.policy.SimpleCompletionPolicy; import org.springframework.batch.repeat.support.RepeatTemplate; import org.springframework.batch.restart.RestartData; @@ -58,6 +60,8 @@ public class DefaultStepExecutorTests extends TestCase { private StepConfigurationSupport stepConfiguration; + private RepeatTemplate template; + private ItemProvider getProvider(String[] args) { return new ListItemProvider(Arrays.asList(args)); } @@ -85,8 +89,7 @@ public class DefaultStepExecutorTests extends TestCase { stepConfiguration = new SimpleStepConfiguration(); stepConfiguration.setTasklet(getTasklet(new String[] { "foo", "bar", "spam" })); - // Only process one chunk: - RepeatTemplate template = new RepeatTemplate(); + template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(1)); stepExecutor.setStepOperations(template); // Only process one item: @@ -110,7 +113,7 @@ public class DefaultStepExecutorTests extends TestCase { public void testChunkExecutor() throws Exception { - RepeatTemplate template = new RepeatTemplate(); + template = new RepeatTemplate(); // Only process one item: template.setCompletionPolicy(new SimpleCompletionPolicy(1)); @@ -129,7 +132,7 @@ public class DefaultStepExecutorTests extends TestCase { public void testStepContextInitialized() throws Exception { - RepeatTemplate template = new RepeatTemplate(); + template = new RepeatTemplate(); // Only process one item: template.setCompletionPolicy(new SimpleCompletionPolicy(1)); @@ -159,6 +162,35 @@ public class DefaultStepExecutorTests extends TestCase { } + public void testStepContextInitializedBeforeTasklet() throws Exception { + + template = new RepeatTemplate(); + + // Only process one chunk: + template.setCompletionPolicy(new SimpleCompletionPolicy(1)); + stepExecutor.setStepOperations(template); + + final StepInstance step = new StepInstance(new Long(1)); + SimpleJobIdentifier jobIdentifier = new SimpleJobIdentifier("FOO"); + final JobExecution jobExecution = new JobExecution(new JobInstance( + jobIdentifier, new Long(3))); + final StepExecution stepExecution = new StepExecution(step, + jobExecution); + + template.setInterceptor(new RepeatInterceptorAdapter() { + public void open(RepeatContext context) { + assertNotNull(StepSynchronizationManager.getContext() + .getStepExecution()); + assertEquals(stepExecution, StepSynchronizationManager.getContext() + .getStepExecution()); + } + }); + + stepExecutor.process(stepConfiguration, stepExecution); + assertEquals(1, processed.size()); + + } + public void testRepository() throws Exception { SimpleJobRepository repository = new SimpleJobRepository(