diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobInstance.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobInstance.java index 8782de4b1..7668420ae 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobInstance.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/JobInstance.java @@ -26,8 +26,8 @@ import org.springframework.util.Assert; * * Trying to execute an existing JobIntance that has already completed * successfully will result in error. Error will be raised also for an attempt - * to restart a failed JobInstance if the Job ({@link JobInstance#getJob()}) is not - * restartable. + * to restart a failed JobInstance if the Job ({@link JobInstance#getJob()}) + * is not restartable. * * @see Job * @see JobParameters @@ -44,24 +44,13 @@ public class JobInstance extends Entity { private Job job; - private JobExecution lastExecution; - public JobInstance(Long id, JobParameters jobParameters, Job job) { super(id); Assert.notNull(job); - this.jobParameters = jobParameters == null ? new JobParameters() - : jobParameters; + this.jobParameters = jobParameters == null ? new JobParameters() : jobParameters; this.job = job; } - public void setLastExecution(JobExecution lastExecution) { - this.lastExecution = lastExecution; - } - - public JobExecution getLastExecution() { - return lastExecution; - } - /** * @return {@link JobParameters} */ @@ -76,15 +65,8 @@ public class JobInstance extends Entity { return job == null ? null : job.getName(); } - public JobExecution createJobExecution() { - JobExecution newExecution = new JobExecution(this); - this.setLastExecution(newExecution); - return newExecution; - } - public String toString() { - return super.toString() + ", JobParameters=[" + jobParameters + "]" - + ", Job=[" + job + "]"; + return super.toString() + ", JobParameters=[" + jobParameters + "]" + ", Job=[" + job + "]"; } public Job getJob() { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java index d21887164..c39cc42ee 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/domain/JobInstanceTests.java @@ -24,13 +24,6 @@ import junit.framework.TestCase; public class JobInstanceTests extends TestCase { private JobInstance instance = new JobInstance(new Long(11), new JobParameters(), new JobSupport("job")); - - public void testLastExecution(){ - JobExecution lastExecution = new JobExecution(); - assertNull(instance.getLastExecution()); - instance.setLastExecution(lastExecution); - assertEquals(lastExecution, instance.getLastExecution()); - } /** * Test method for {@link org.springframework.batch.core.domain.JobInstance#getIdentifier()}. @@ -44,12 +37,6 @@ public class JobInstanceTests extends TestCase { assertEquals("job", instance.getJob().getName()); } - public void testCreateJobExecution(){ - JobExecution execution = instance.createJobExecution(); - assertNotNull(execution); - assertEquals(execution, instance.getLastExecution()); - } - public void testCreateWithNulls(){ try { new JobInstance(null, null, null); diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java index 3dc53d0cc..b96bd160f 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java @@ -61,7 +61,6 @@ public class SimpleJob extends AbstractJob { public void execute(JobExecution execution) throws InfrastructureException { JobInstance jobInstance = execution.getJobInstance(); - jobInstance.setLastExecution(execution); StepExecution currentStepExecution = null; int startedCount = 0; 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 a25bc7ccf..064b24669 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 @@ -139,8 +139,6 @@ public class SimpleJobRepository implements JobRepository { Assert.notNull(job, "Job must not be null."); Assert.notNull(jobParameters, "JobParameters must not be null."); - JobInstance jobInstance; - /* * Find all jobs matching the runtime information. * @@ -150,7 +148,7 @@ public class SimpleJobRepository implements JobRepository { * has finished. */ - jobInstance = jobInstanceDao.getJobInstance(job, jobParameters); + JobInstance jobInstance = jobInstanceDao.getJobInstance(job, jobParameters); // existing job instance found if (jobInstance != null) { @@ -159,40 +157,29 @@ public class SimpleJobRepository implements JobRepository { } List executions = jobExecutionDao.findJobExecutions(jobInstance); - JobExecution lastExecution = null; + // check for running executions and find the last started for (Iterator iterator = executions.iterator(); iterator.hasNext();) { JobExecution execution = (JobExecution) iterator.next(); - if (lastExecution == null) { - lastExecution = execution; - } - if (execution.getStartTime() != null && lastExecution.getStartTime() != null - && lastExecution.getStartTime().getTime() < execution.getStartTime().getTime()) { - lastExecution = execution; - } - if (execution.isRunning()) { throw new JobExecutionAlreadyRunningException("A job execution for this job is already running: " + jobInstance); } } - jobInstance.setLastExecution(lastExecution); } else { // no job found, create one jobInstance = jobInstanceDao.createJobInstance(job, jobParameters); } - - return generateJobExecution(jobInstance); - - } - - private JobExecution generateJobExecution(JobInstance jobInstance) { - JobExecution execution = jobInstance.createJobExecution(); + + JobExecution jobExecution = new JobExecution(jobInstance); + // Save the JobExecution so that it picks up an ID (useful for clients // monitoring asynchronous executions): - saveOrUpdate(execution); - return execution; + saveOrUpdate(jobExecution); + + return jobExecution; + } /** diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java index 365de5e4b..18351e0db 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/SimpleJobRepositoryTests.java @@ -105,12 +105,7 @@ public class SimpleJobRepositoryTests extends TestCase { jobConfiguration.setSteps(stepConfigurations); - databaseJob = new JobInstance(new Long(1), jobParameters, jobConfiguration) { - public JobExecution createJobExecution() { - jobExecution = super.createJobExecution(); - return jobExecution; - } - }; + databaseJob = new JobInstance(new Long(1), jobParameters, jobConfiguration); databaseStep1 = "dbStep1"; databaseStep2 = "dbStep2"; diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java index 914f71df0..caa97afba 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/AbstractJobDaoTests.java @@ -81,7 +81,6 @@ public abstract class AbstractJobDaoTests extends AbstractTransactionalDataSourc jobExecution.setStartTime(jobExecutionStartTime); jobExecution.setStatus(BatchStatus.STARTED); jobExecutionDao.saveJobExecution(jobExecution); - jobInstance.setLastExecution(jobExecution); } public void testVersionIsNotNullForJob() throws Exception { diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcJobDaoQueryTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcJobDaoQueryTests.java index 4ba572fe1..304a636a8 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcJobDaoQueryTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/JdbcJobDaoQueryTests.java @@ -20,6 +20,7 @@ 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.JobParameters; import org.springframework.batch.execution.job.JobSupport; @@ -32,7 +33,7 @@ import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer * */ public class JdbcJobDaoQueryTests extends TestCase { - + JdbcJobExecutionDao jobExecutionDao; List list = new ArrayList(); @@ -42,7 +43,7 @@ public class JdbcJobDaoQueryTests extends TestCase { * @see junit.framework.TestCase#setUp() */ protected void setUp() throws Exception { - + jobExecutionDao = new JdbcJobExecutionDao(); jobExecutionDao.setJobExecutionIncrementer(new DataFieldMaxValueIncrementer() { @@ -69,7 +70,10 @@ public class JdbcJobDaoQueryTests extends TestCase { return 1; } }); - jobExecutionDao.saveJobExecution(new JobInstance(new Long(11), new JobParameters(), new JobSupport("testJob")).createJobExecution()); + JobExecution jobExecution = new JobExecution(new JobInstance(new Long(11), new JobParameters(), new JobSupport( + "testJob"))); + + jobExecutionDao.saveJobExecution(jobExecution); assertEquals(1, list.size()); String query = (String) list.get(0); assertTrue("Query did not contain FOO_:" + query, query.indexOf("FOO_") >= 0); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/resource/StepExecutionProxyResourceTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/resource/StepExecutionProxyResourceTests.java index 24e5e3776..3699ff52a 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/resource/StepExecutionProxyResourceTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/resource/StepExecutionProxyResourceTests.java @@ -62,7 +62,7 @@ public class StepExecutionProxyResourceTests extends TestCase { protected void setUp() throws Exception { jobInstance = new JobInstance(new Long(0), new JobParameters(), new JobSupport("testJob")); - JobExecution jobExecution = jobInstance.createJobExecution(); + JobExecution jobExecution = new JobExecution(jobInstance); Step step = new StepSupport("bar"); stepExecution = jobExecution.createStepExecution(step); resource.beforeStep(stepExecution); @@ -97,7 +97,7 @@ public class StepExecutionProxyResourceTests extends TestCase { resource.setFilePattern("foo/data/%JOB_NAME%/%job.key%-foo"); jobInstance = new JobInstance(new Long(0), new JobParametersBuilder().addString("job.key", "spam") .toJobParameters(), new JobSupport("testJob")); - JobExecution jobExecution = jobInstance.createJobExecution(); + JobExecution jobExecution = new JobExecution(jobInstance); Step step = new StepSupport("bar"); resource.beforeStep(jobExecution.createStepExecution(step)); doTestPathName("spam-foo", "foo" + pathsep + "data" + pathsep); 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 43e7047df..13289df86 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 @@ -518,7 +518,7 @@ public class ItemOrientedStepTests extends TestCase { }; itemOrientedStep.setItemProcessor(new SimpleItemHandler(itemReader, itemWriter)); - JobExecution jobExecutionContext = jobInstance.createJobExecution(); + JobExecution jobExecutionContext = new JobExecution(jobInstance); StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext); stepExecution.setExecutionContext(new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar"))); @@ -551,7 +551,7 @@ public class ItemOrientedStepTests extends TestCase { } }); - JobExecution jobExecutionContext = jobInstance.createJobExecution(); + JobExecution jobExecutionContext = new JobExecution(jobInstance); StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext); stepExecution.setExecutionContext(new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar"))); @@ -579,7 +579,7 @@ public class ItemOrientedStepTests extends TestCase { } }); - JobExecution jobExecutionContext = jobInstance.createJobExecution(); + JobExecution jobExecutionContext = new JobExecution(jobInstance); StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext); stepExecution.setExecutionContext(new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar"))); @@ -611,7 +611,7 @@ public class ItemOrientedStepTests extends TestCase { } }); - JobExecution jobExecutionContext = jobInstance.createJobExecution(); + JobExecution jobExecutionContext = new JobExecution(jobInstance); StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext); try { @@ -642,7 +642,7 @@ public class ItemOrientedStepTests extends TestCase { itemOrientedStep.setItemProcessor(new SimpleItemHandler(itemReader, itemWriter)); itemOrientedStep.registerStream(itemReader); - JobExecution jobExecutionContext = jobInstance.createJobExecution(); + JobExecution jobExecutionContext = new JobExecution(jobInstance); StepExecution stepExecution = new StepExecution(itemOrientedStep, jobExecutionContext); stepExecution.setExecutionContext(new ExecutionContext(PropertiesConverter.stringToProperties("foo=bar")));