From 09545ba3675a25b8d6ae273bc00a737fe09105a8 Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 26 Feb 2008 17:08:22 +0000 Subject: [PATCH] OPEN - issue BATCH-151: add support for some sort of 'final' method to existing or new ItemProcessor implementation and related tasklets http://jira.springframework.org/browse/BATCH-151 Encapsulate step context identifier to prevent step from forgetting to set it up. --- .../batch/execution/scope/SimpleStepContext.java | 7 +++++++ .../batch/execution/scope/StepContext.java | 8 ++++++++ .../batch/execution/scope/StepScope.java | 8 +------- .../batch/execution/step/ItemOrientedStep.java | 4 ---- .../batch/execution/scope/StepScopeTests.java | 10 +++++++--- .../batch/execution/step/ItemOrientedStepTests.java | 2 +- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/SimpleStepContext.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/SimpleStepContext.java index e99819ef0..b63ea8705 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/SimpleStepContext.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/SimpleStepContext.java @@ -162,5 +162,12 @@ public class SimpleStepContext extends SynchronizedAttributeAccessor implements public StepExecution getStepExecution() { return stepExecution; } + + /* (non-Javadoc) + * @see org.springframework.batch.execution.scope.StepContext#getIdentifier() + */ + public String getIdentifier() { + return "JOB_EXECUTION_ID:"+stepExecution.getJobExecutionId(); + } } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepContext.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepContext.java index 20f23ddbc..de193ecdc 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepContext.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepContext.java @@ -51,4 +51,12 @@ public interface StepContext extends AttributeAccessor { */ void close(); + /** + * Identify this context so that concurrently running jobs in the same VM + * can be distinguished. + * + * @return a sufficiently unique identifier for this context + */ + String getIdentifier(); + } \ No newline at end of file diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepScope.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepScope.java index 797e9d471..30b85add4 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepScope.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/scope/StepScope.java @@ -51,11 +51,6 @@ public class StepScope implements Scope, BeanFactoryPostProcessor, Ordered { return order; } - /** - * Context key for clients to use for conversation identifier. - */ - public static final String ID_KEY = "JOB_IDENTIFIER"; - private String name = "step"; /* @@ -89,8 +84,7 @@ public class StepScope implements Scope, BeanFactoryPostProcessor, Ordered { */ public String getConversationId() { StepContext context = getContext(); - Object id = context.getAttribute(ID_KEY); - return "" + id; + return context.getIdentifier(); } /* diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java index d5593b7af..c2239cc24 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java @@ -29,7 +29,6 @@ import org.springframework.batch.core.runtime.ExitStatusExceptionClassifier; import org.springframework.batch.core.tasklet.Tasklet; import org.springframework.batch.execution.scope.SimpleStepContext; import org.springframework.batch.execution.scope.StepContext; -import org.springframework.batch.execution.scope.StepScope; import org.springframework.batch.execution.scope.StepSynchronizationManager; import org.springframework.batch.execution.step.support.SimpleExitStatusExceptionClassifier; import org.springframework.batch.execution.step.support.StepInterruptionPolicy; @@ -237,9 +236,6 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean { final StepContext stepContext = new SimpleStepContext(stepExecution, parentStepContext); StepSynchronizationManager.register(stepContext); possiblyRegisterStreams(); - // Add the job identifier so that it can be used to identify - // the conversation in StepScope - stepContext.setAttribute(StepScope.ID_KEY, stepExecution.getJobExecution().getId()); if (isSaveExecutionContext() && isRestart && lastStepExecution != null) { stepExecution.setExecutionContext(lastStepExecution.getExecutionContext()); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/scope/StepScopeTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/scope/StepScopeTests.java index 812cb4c4b..1f94c94ca 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/scope/StepScopeTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/scope/StepScopeTests.java @@ -20,6 +20,10 @@ import java.util.List; import junit.framework.TestCase; +import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.JobInstance; +import org.springframework.batch.core.domain.JobSupport; +import org.springframework.batch.core.domain.StepSupport; import org.springframework.batch.repeat.synch.RepeatSynchronizationManager; import org.springframework.beans.BeansException; import org.springframework.beans.factory.ObjectFactory; @@ -41,7 +45,8 @@ public class StepScopeTests extends TestCase { */ protected void setUp() throws Exception { super.setUp(); - context = new SimpleStepContext(null); + JobExecution jobExecution = new JobExecution(new JobInstance(new Long(1L), null, new JobSupport()), new Long(11L)); + context = new SimpleStepContext(jobExecution.createStepExecution(new StepSupport())); StepSynchronizationManager.register(context); } @@ -131,9 +136,8 @@ public class StepScopeTests extends TestCase { * {@link org.springframework.batch.execution.scope.StepScope#getConversationId()}. */ public void testGetConversationIdFromAttribute() { - context.setAttribute(StepScope.ID_KEY, "foo"); String id = scope.getConversationId(); - assertEquals("foo", id); + assertEquals("JOB_EXECUTION_ID:11", id); } /** diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java index ca8518aed..e6bd0bcb2 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java @@ -186,7 +186,7 @@ public class ItemOrientedStepTests extends TestCase { assertNotNull(StepSynchronizationManager.getContext().getStepExecution()); assertEquals(stepExecution, StepSynchronizationManager.getContext().getStepExecution()); // StepScope can obtain id information.... - assertNotNull(StepSynchronizationManager.getContext().getAttribute(StepScope.ID_KEY)); + assertNotNull(StepSynchronizationManager.getContext().getIdentifier()); } });