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.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user