diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java index e19536ee2..ac52f79d5 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/AbstractStep.java @@ -33,6 +33,7 @@ import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.NoSuchJobException; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.repeat.ExitStatus; +import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.Assert; @@ -44,7 +45,7 @@ import org.springframework.util.Assert; * @author Ben Hale * @author Robert Kasanicky */ -public abstract class AbstractStep implements Step, InitializingBean { +public abstract class AbstractStep implements Step, InitializingBean, BeanNameAware { /** * Exit code for interrupted status. @@ -53,11 +54,11 @@ public abstract class AbstractStep implements Step, InitializingBean { private static final Log logger = LogFactory.getLog(AbstractStep.class); - protected String name; + private String name; - protected int startLimit = Integer.MAX_VALUE; + private int startLimit = Integer.MAX_VALUE; - protected boolean allowStartIfComplete; + private boolean allowStartIfComplete; private CompositeStepExecutionListener listener = new CompositeStepExecutionListener(); @@ -88,6 +89,21 @@ public abstract class AbstractStep implements Step, InitializingBean { this.name = name; } + /** + * Set the name property if it is not already set. Because of the order of + * the callbacks in a Spring container the name property will be set first + * if it is present. Care is needed with bean definition inheritance - if a + * parent bean has a name, then its children need an explicit name as well, + * otherwise they will not be unique. + * + * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) + */ + public void setBeanName(String name) { + if (this.name == null) { + this.name = name; + } + } + public int getStartLimit() { return this.startLimit; } @@ -134,7 +150,7 @@ public abstract class AbstractStep implements Step, InitializingBean { * resource initialization ({@link #open(ExecutionContext)}), execution * logic ({@link #doExecute(StepExecution)}) and resource closing ({@link #close(ExecutionContext)}). */ - public void execute(StepExecution stepExecution) throws JobInterruptedException, UnexpectedJobExecutionException { + public final void execute(StepExecution stepExecution) throws JobInterruptedException, UnexpectedJobExecutionException { stepExecution.setStartTime(new Date()); stepExecution.setStatus(BatchStatus.STARTED); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ItemOrientedStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ItemOrientedStep.java index 0eefe3f36..e03f7d41e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ItemOrientedStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/item/ItemOrientedStep.java @@ -207,7 +207,7 @@ public class ItemOrientedStep extends AbstractStep { * execution * */ - public ExitStatus doExecute(final StepExecution stepExecution) throws Exception { + protected ExitStatus doExecute(final StepExecution stepExecution) throws Exception { stream.update(stepExecution.getExecutionContext()); getJobRepository().saveOrUpdateExecutionContext(stepExecution); itemHandler.mark(); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java index 90744f836..be247839d 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java @@ -22,7 +22,6 @@ import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.step.AbstractStep; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.repeat.ExitStatus; -import org.springframework.beans.factory.BeanNameAware; import org.springframework.util.Assert; /** @@ -38,25 +37,10 @@ import org.springframework.util.Assert; * @author Ben Hale * @author Robert Kasanicky */ -public class TaskletStep extends AbstractStep implements BeanNameAware { +public class TaskletStep extends AbstractStep { private Tasklet tasklet; - /** - * Set the name property if it is not already set. Because of the order of - * the callbacks in a Spring container the name property will be set first - * if it is present. Care is needed with bean definition inheritance - if a - * parent bean has a name, then its children need an explicit name as well, - * otherwise they will not be unique. - * - * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String) - */ - public void setBeanName(String name) { - if (this.name == null) { - this.name = name; - } - } - /** * Register each of the objects as listeners. * diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java index 0d01946cd..3e580794d 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java @@ -41,17 +41,8 @@ import org.springframework.batch.core.repository.dao.MapJobInstanceDao; import org.springframework.batch.core.repository.dao.MapStepExecutionDao; import org.springframework.batch.core.repository.dao.StepExecutionDao; import org.springframework.batch.core.repository.support.SimpleJobRepository; -import org.springframework.batch.core.step.AbstractStep; -import org.springframework.batch.core.step.skip.ItemSkipPolicy; -import org.springframework.batch.core.step.skip.NeverSkipItemSkipPolicy; -import org.springframework.batch.item.AbstractItemReader; -import org.springframework.batch.item.ExecutionContext; -import org.springframework.batch.item.ItemReader; -import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.core.step.StepSupport; import org.springframework.batch.repeat.ExitStatus; -import org.springframework.batch.repeat.exception.ExceptionHandler; -import org.springframework.batch.retry.RetryPolicy; -import org.springframework.transaction.PlatformTransactionManager; /** * Tests for DefaultJobLifecycle. MapJobDao and MapStepExecutionDao are used @@ -192,19 +183,7 @@ public class SimpleJobTests extends TestCase { job.setJobRepository(jobRepository); // do not set StepExecutorFactory... step1.setStartLimit(5); - step1.setItemReader(new AbstractItemReader() { - public Object read() throws Exception { - list.add("1"); - return null; - } - }); step2.setStartLimit(5); - step2.setItemReader(new AbstractItemReader() { - public Object read() throws Exception { - list.add("2"); - return null; - } - }); job.execute(jobExecution); assertEquals(2, list.size()); checkRepository(BatchStatus.COMPLETED, ExitStatus.FINISHED); @@ -353,7 +332,7 @@ public class SimpleJobTests extends TestCase { catch (RuntimeException e) { assertSame(exception, e); } - + try { job.execute(jobExecution); fail(); @@ -384,27 +363,15 @@ public class SimpleJobTests extends TestCase { checkRepository(status, null); } - private static class StubStep extends AbstractStep { + private static class StubStep extends StepSupport { private Runnable runnable; private Throwable exception; - protected ExceptionHandler exceptionHandler; + private JobRepository jobRepository; - protected RetryPolicy retryPolicy; - - protected JobRepository jobRepository; - - protected PlatformTransactionManager transactionManager; - - protected ItemReader itemReader; - - protected ItemWriter itemWriter; - - protected ItemSkipPolicy itemSkipPolicy = new NeverSkipItemSkipPolicy(); - - Properties passedInContext; + private Properties passedInContext; /** * @param string @@ -427,13 +394,17 @@ public class SimpleJobTests extends TestCase { this.runnable = runnable; } + /* + * (non-Javadoc) + * @see org.springframework.batch.core.step.StepSupport#execute(org.springframework.batch.core.StepExecution) + */ public void execute(StepExecution stepExecution) throws JobInterruptedException, UnexpectedJobExecutionException { - + passedInContext = stepExecution.getExecutionContext().getProperties(); stepExecution.getExecutionContext().putString("key", "value"); jobRepository.saveOrUpdateExecutionContext(stepExecution); - + if (exception instanceof RuntimeException) { stepExecution.setExitStatus(ExitStatus.FAILED); throw (RuntimeException) exception; @@ -450,29 +421,7 @@ public class SimpleJobTests extends TestCase { runnable.run(); } stepExecution.setExitStatus(ExitStatus.FINISHED); - } - /** - * Set the name property. Always overrides the default value if this - * object is a Spring bean. - * - * @see #setBeanName(java.lang.String) - */ - public void setName(String name) { - this.name = name; - } - - /** - * Public setter for the {@link RetryPolicy}. - * - * @param retryPolicy the {@link RetryPolicy} to set - */ - public void setRetryPolicy(RetryPolicy retryPolicy) { - this.retryPolicy = retryPolicy; - } - - public void setExceptionHandler(ExceptionHandler exceptionHandler) { - this.exceptionHandler = exceptionHandler; } /** @@ -484,47 +433,5 @@ public class SimpleJobTests extends TestCase { this.jobRepository = jobRepository; } - /** - * Public setter for the {@link PlatformTransactionManager}. - * - * @param transactionManager the transaction manager to set - */ - public void setTransactionManager(PlatformTransactionManager transactionManager) { - this.transactionManager = transactionManager; - } - - /** - * @param itemReader the itemReader to set - */ - public void setItemReader(ItemReader itemReader) { - this.itemReader = itemReader; - } - - /** - * @param itemWriter the itemWriter to set - */ - public void setItemWriter(ItemWriter itemWriter) { - this.itemWriter = itemWriter; - } - - public void setItemSkipPolicy(ItemSkipPolicy itemSkipPolicy) { - this.itemSkipPolicy = itemSkipPolicy; - } - - protected ExitStatus doExecute(StepExecution stepExecution) throws Exception { - // TODO Auto-generated method stub - return null; - } - - protected void close(ExecutionContext ctx) throws Exception { - // TODO Auto-generated method stub - - } - - protected void open(ExecutionContext ctx) throws Exception { - // TODO Auto-generated method stub - - } - } }