diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java index f0e641c90..986015051 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/JobRepository.java @@ -20,6 +20,7 @@ import org.springframework.batch.core.domain.Job; import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobParameters; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.item.ExecutionContext; @@ -95,12 +96,12 @@ public interface JobRepository { /** * @return the last execution of step for the given job instance. */ - public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName); + public StepExecution getLastStepExecution(JobInstance jobInstance, Step step); /** * @return the execution count of the step within the given job instance. */ - public int getStepExecutionCount(JobInstance jobInstance, String stepName); + public int getStepExecutionCount(JobInstance jobInstance, Step step); } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java index 51e7d404d..afce6c606 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/simple/SimpleJob.java @@ -131,7 +131,7 @@ public class SimpleJob extends JobSupport { BatchStatus stepStatus; // if the last execution is null, the step has never been executed. - StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, step.getName()); + StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, step); if (lastStepExecution == null) { stepStatus = BatchStatus.STARTING; } @@ -151,7 +151,7 @@ public class SimpleJob extends JobSupport { return false; } - if (jobRepository.getStepExecutionCount(jobInstance, step.getName()) < step.getStartLimit()) { + if (jobRepository.getStepExecutionCount(jobInstance, step) < step.getStartLimit()) { // step start count is less than start max, return true return true; } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java index 2afe45c08..c1e92950a 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/SimpleJobRepository.java @@ -25,6 +25,7 @@ import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.JobSupport; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.repository.BatchRestartException; import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; @@ -191,7 +192,7 @@ public class SimpleJobRepository implements JobRepository { } else if (jobInstances.size() == 0) { // no job found, create one - jobInstance = createJobInstance(job, jobParameters); + jobInstance = jobInstanceDao.createJobInstance(job, jobParameters); } else { // More than one job found, throw exception @@ -203,19 +204,9 @@ public class SimpleJobRepository implements JobRepository { } -// private List getStepNames(Job job) { -// List stepNames = new ArrayList(job.getSteps().size()); -// for (Iterator iterator = job.getSteps().iterator(); iterator.hasNext();) { -// Step step = (Step) iterator.next(); -// stepNames.add(step.getName()); -// } -// return stepNames; -// } - - - private JobExecution generateJobExecution(JobInstance job) { - JobExecution execution = job.createJobExecution(); + private JobExecution generateJobExecution(JobInstance jobInstance) { + JobExecution execution = jobInstance.createJobExecution(); // Save the JobExecution so that it picks up an ID (useful for clients // monitoring asynchronous executions): saveOrUpdate(execution); @@ -278,23 +269,12 @@ public class SimpleJobRepository implements JobRepository { } } - /** - * Convenience method for creating a new job. A new job is created by - * calling {@link JobInstanceDao#createJobInstance(String, JobParameters)} and then it's - * list of StepInstances is passed to the createStepInstances method. - */ - private JobInstance createJobInstance(Job job, JobParameters jobParameters) { - - JobInstance jobInstance = jobInstanceDao.createJobInstance(job, jobParameters); - return jobInstance; - } - - public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { + public StepExecution getLastStepExecution(JobInstance jobInstance, Step step) { List jobExecutions = jobExecutionDao.findJobExecutions(jobInstance); List stepExecutions = new ArrayList(jobExecutions.size()); for (Iterator iterator = jobExecutions.iterator(); iterator.hasNext();) { JobExecution jobExecution = (JobExecution) iterator.next(); - StepExecution stepExecution = stepExecutionDao.getStepExecution(jobExecution, stepName); + StepExecution stepExecution = stepExecutionDao.getStepExecution(jobExecution, step); if (stepExecution != null) { stepExecutions.add(stepExecution); } @@ -312,12 +292,12 @@ public class SimpleJobRepository implements JobRepository { return latest; } - public int getStepExecutionCount(JobInstance jobInstance, String stepName) { + public int getStepExecutionCount(JobInstance jobInstance, Step step) { int count = 0; List jobExecutions = jobExecutionDao.findJobExecutions(jobInstance); for (Iterator iterator = jobExecutions.iterator(); iterator.hasNext();) { JobExecution jobExecution = (JobExecution) iterator.next(); - if (stepExecutionDao.getStepExecution(jobExecution, stepName) != null) { + if (stepExecutionDao.getStepExecution(jobExecution, step) != null) { count++; } } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java index 632bce611..e22e8b3df 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcStepExecutionDao.java @@ -14,6 +14,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.domain.BatchStatus; import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.item.ExecutionContext; @@ -58,10 +59,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement private static final String FIND_STEP_EXECUTION_CONTEXT = "SELECT TYPE_CD, KEY_NAME, STRING_VAL, DOUBLE_VAL, LONG_VAL, OBJECT_VAL " + "from %PREFIX%STEP_EXECUTION_CONTEXT where STEP_EXECUTION_ID = ?"; - // private static final String GET_STEP_EXECUTION_COUNT = "SELECT - // count(STEP_EXECUTION_ID) from %PREFIX%STEP_EXECUTION where " - // + "STEP_INSTANCE_ID = ?"; - private static final String INSERT_STEP_EXECUTION_CONTEXT = "INSERT into %PREFIX%STEP_EXECUTION_CONTEXT(STEP_EXECUTION_ID, TYPE_CD," + " KEY_NAME, STRING_VAL, DOUBLE_VAL, LONG_VAL, OBJECT_VAL) values(?,?,?,?,?,?,?)"; @@ -438,9 +435,9 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement } } - public StepExecution getStepExecution(JobExecution jobExecution, String stepName) { + public StepExecution getStepExecution(JobExecution jobExecution, Step step) { List executions = getJdbcTemplate().query(getQuery(GET_STEP_EXECUTION), - new Object[] { stepName, jobExecution.getId() }, new StepExecutionRowMapper(jobExecution)); + new Object[] { step.getName(), jobExecution.getId() }, new StepExecutionRowMapper(jobExecution)); Assert.state(executions.size() <= 1, "There can be at most one step execution with given name for single job execution"); diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java index 3b9177a86..4423cd5ad 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapStepDao.java @@ -22,6 +22,7 @@ import java.util.Set; import java.util.Map.Entry; import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.support.transaction.TransactionAwareProxyFactory; @@ -128,7 +129,7 @@ public class MapStepDao implements StepExecutionDao { executions.add(stepExecution); } - public StepExecution getStepExecution(JobExecution jobExecution, String stepName) { + public StepExecution getStepExecution(JobExecution jobExecution, Step step) { // for (Iterator iterator = executionsById.entrySet().iterator(); iterator.hasNext();) { // Entry entry = (Entry) iterator.next(); // StepExecution stepExecution = (StepExecution) entry.getValue(); diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java index 0e359536c..7b88a0346 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/StepExecutionDao.java @@ -1,6 +1,7 @@ package org.springframework.batch.execution.repository.dao; import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.item.ExecutionContext; @@ -48,6 +49,6 @@ public interface StepExecutionDao { */ void updateExecutionContext(StepExecution stepExecution); - StepExecution getStepExecution(JobExecution jobExecution, String stepName); + StepExecution getStepExecution(JobExecution jobExecution, Step step); } diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ChunkedStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ChunkedStep.java index 006a9f709..a75321f6d 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ChunkedStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ChunkedStep.java @@ -318,9 +318,9 @@ public class ChunkedStep extends StepSupport implements InitializingBean { JobInstance jobInstance = stepExecution.getJobExecution().getJobInstance(); String stepName = stepExecution.getStepName(); - StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, stepName); + StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, this); - boolean isRestart = jobRepository.getStepExecutionCount(jobInstance, stepName) > 0 ? true : false; + boolean isRestart = jobRepository.getStepExecutionCount(jobInstance, this) > 0 ? true : false; ExitStatus status = ExitStatus.FAILED; 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 93f217f2f..cc044fbf2 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 @@ -232,10 +232,9 @@ public class ItemOrientedStep extends AbstractStep implements InitializingBean { public void execute(final StepExecution stepExecution) throws BatchCriticalException, JobInterruptedException { JobInstance jobInstance = stepExecution.getJobExecution().getJobInstance(); - String stepName = stepExecution.getStepName(); - StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, stepName); + StepExecution lastStepExecution = jobRepository.getLastStepExecution(jobInstance, this); - boolean isRestart = jobRepository.getStepExecutionCount(jobInstance, stepName) > 0 ? true : false; + boolean isRestart = jobRepository.getStepExecutionCount(jobInstance, this) > 0 ? true : false; ExitStatus status = ExitStatus.FAILED; diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java index 5d5e077e5..05be6ea71 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/MockStepDao.java @@ -20,6 +20,7 @@ import java.util.List; import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.execution.repository.dao.StepExecutionDao; import org.springframework.batch.item.ExecutionContext; @@ -48,11 +49,6 @@ public class MockStepDao implements StepExecutionDao { currentNewStep = 0; } -// public List findStepExecutions(StepInstance step, JobExecution jobExecution) { -// -// return null; -// } - public ExecutionContext findExecutionContext(StepExecution stepExecution) { return null; } @@ -63,8 +59,7 @@ public class MockStepDao implements StepExecutionDao { public void updateExecutionContext(StepExecution stepExecution) { } - public StepExecution getStepExecution(JobExecution jobExecution, String stepName) { - // TODO Auto-generated method stub + public StepExecution getStepExecution(JobExecution jobExecution, Step step) { return null; } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java index fcad20ceb..016abc650 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractStepDaoTests.java @@ -25,7 +25,11 @@ import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.JobSupport; import org.springframework.batch.core.domain.StepExecution; +import org.springframework.batch.core.domain.StepSupport; +import org.springframework.batch.core.runtime.ExitStatusExceptionClassifier; import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.ExitStatus; +import org.springframework.batch.support.PropertiesConverter; import org.springframework.dao.OptimisticLockingFailureException; import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; import org.springframework.util.ClassUtils; @@ -115,45 +119,41 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour public void testUpdateStepWithExecutionContext() { stepExecution.setExecutionContext(executionContext); stepExecutionDao.saveExecutionContext(stepExecution); -// StepInstance tempStep = stepInstanceDao.findStepInstance(jobInstance, step1.getName()); ExecutionContext tempAttributes = stepExecutionDao.findExecutionContext(stepExecution); -// assertEquals(tempStep, step1); assertEquals(executionContext, tempAttributes); } -// TODO update -// public void testSaveStepExecution() { -// -// StepExecution execution = new StepExecution(step2, jobExecution, null); -// execution.setStatus(BatchStatus.STARTED); -// execution.setStartTime(new Date(System.currentTimeMillis())); -// execution.setExecutionContext(new ExecutionContext(PropertiesConverter.stringToProperties("key1=0,key2=5"))); -// execution.setExitStatus(new ExitStatus(false, ExitStatusExceptionClassifier.FATAL_EXCEPTION, -// "java.lang.Exception")); -// stepExecutionDao.saveStepExecution(execution); -// List executions = stepExecutionDao.findStepExecutions(step2, null); -// assertEquals(1, executions.size()); -// StepExecution tempExecution = (StepExecution) executions.get(0); -// assertEquals(execution, tempExecution); -// assertEquals(execution.getExecutionContext().getString("key1"), tempExecution.getExecutionContext().getString("key1")); -// assertEquals(execution.getExitStatus(), tempExecution.getExitStatus()); -// } -// -// public void testUpdateStepExecution() { -// -// stepExecution.setStatus(BatchStatus.COMPLETED); -// stepExecution.setEndTime(new Date(System.currentTimeMillis())); -// stepExecution.setCommitCount(5); -// stepExecution.setTaskCount(5); -// stepExecution.setExecutionContext(new ExecutionContext()); -// stepExecution.setExitStatus(new ExitStatus(false, ExitStatusExceptionClassifier.FATAL_EXCEPTION, -// "java.lang.Exception")); -// stepExecutionDao.updateStepExecution(stepExecution); -// List executions = stepExecutionDao.findStepExecutions(step1, null); -// assertEquals(1, executions.size()); -// StepExecution tempExecution = (StepExecution) executions.get(0); -// assertEquals(stepExecution, tempExecution); -// assertEquals(stepExecution.getExitStatus(), tempExecution.getExitStatus()); -// } + + public void testSaveStepExecution() { + + StepExecution execution = new StepExecution(step2, jobExecution, null); + execution.setStatus(BatchStatus.STARTED); + execution.setStartTime(new Date(System.currentTimeMillis())); + execution.setExecutionContext(new ExecutionContext(PropertiesConverter.stringToProperties("key1=0,key2=5"))); + execution.setExitStatus(new ExitStatus(false, ExitStatusExceptionClassifier.FATAL_EXCEPTION, + "java.lang.Exception")); + stepExecutionDao.saveStepExecution(execution); + StepExecution retrievedExecution = stepExecutionDao.getStepExecution(jobExecution, new StepSupport(step2)); + assertNotNull(retrievedExecution); + assertEquals(execution, retrievedExecution); + assertEquals(execution.getExecutionContext().getString("key1"), retrievedExecution.getExecutionContext().getString("key1")); + assertEquals(execution.getExitStatus(), retrievedExecution.getExitStatus()); + } + + public void testUpdateStepExecution() { + + stepExecution.setStatus(BatchStatus.COMPLETED); + stepExecution.setEndTime(new Date(System.currentTimeMillis())); + stepExecution.setCommitCount(5); + stepExecution.setTaskCount(5); + stepExecution.setExecutionContext(new ExecutionContext()); + stepExecution.setExitStatus(new ExitStatus(false, ExitStatusExceptionClassifier.FATAL_EXCEPTION, + "java.lang.Exception")); + stepExecutionDao.updateStepExecution(stepExecution); + StepExecution retrievedExecution = stepExecutionDao.getStepExecution(jobExecution, new StepSupport(step1)); + assertNotNull(retrievedExecution); + assertEquals(stepExecution, retrievedExecution); + assertEquals(stepExecution.getExitStatus(), retrievedExecution.getExitStatus()); + } public void testUpdateStepExecutionWithNullId() { StepExecution stepExecution = new StepExecution(null, null, null); @@ -166,21 +166,6 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour } } - public void testGetStepExecutionCountForNoExecutions() { - -// int executionCount = stepExecutionDao.getStepExecutionCount(step2); -// assertEquals(executionCount, 0); - } - - public void testIncrementStepExecutionCount() { - -//// assertEquals(1, stepExecutionDao.getStepExecutionCount(step1)); -// StepExecution execution = new StepExecution(step1, new JobExecution(step1.getJobInstance(), new Long(123)), -// null); -// stepExecutionDao.saveStepExecution(execution); -//// assertEquals(2, stepExecutionDao.getStepExecutionCount(step1)); - } - public void testUpdateStepExecutionVersion() throws Exception { int before = stepExecution.getVersion().intValue(); stepExecutionDao.updateStepExecution(stepExecution); @@ -216,7 +201,7 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour } public void testGetStepExecution() { - assertEquals(stepExecution, stepExecutionDao.getStepExecution(jobExecution, step1)); + assertEquals(stepExecution, stepExecutionDao.getStepExecution(jobExecution, new StepSupport(step1))); } } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ChunkedStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ChunkedStepTests.java index 414f2cd5c..5de3c3ec9 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ChunkedStepTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ChunkedStepTests.java @@ -170,9 +170,9 @@ public class ChunkedStepTests extends TestCase { // JobExecution jobExecutionContext = new JobExecution(jobInstance); // StepExecution stepExecution = new StepExecution(step, jobExecutionContext); - repository.getLastStepExecution(jobInstance, "testStep"); + repository.getLastStepExecution(jobInstance, chunkedStep); repoControl.setReturnValue(new StepExecution(null,null)); - repository.getStepExecutionCount(jobInstance, "testStep"); + repository.getStepExecutionCount(jobInstance, chunkedStep); repoControl.setReturnValue(0); repository.saveOrUpdate(stepExecution); repository.saveOrUpdate(stepExecution); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/JobRepositorySupport.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/JobRepositorySupport.java index 87a6b6263..0831242e7 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/JobRepositorySupport.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/support/JobRepositorySupport.java @@ -19,6 +19,7 @@ import org.springframework.batch.core.domain.Job; import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobParameters; +import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.repository.JobRepository; @@ -53,12 +54,12 @@ public class JobRepositorySupport implements JobRepository { public void update(JobInstance job) { } - public StepExecution getLastStepExecution(JobInstance jobInstance, String stepName) { + public StepExecution getLastStepExecution(JobInstance jobInstance, Step step) { // TODO Auto-generated method stub return null; } - public int getStepExecutionCount(JobInstance jobInstance, String stepName) { + public int getStepExecutionCount(JobInstance jobInstance, Step step) { // TODO Auto-generated method stub return 0; }