diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapJobDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapJobDao.java index 63d01cb6b..39595ace9 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapJobDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/MapJobDao.java @@ -21,14 +21,12 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.Map.Entry; 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.support.transaction.TransactionAwareProxyFactory; -import org.springframework.dao.IncorrectResultSizeDataAccessException; public class MapJobDao implements JobInstanceDao, JobExecutionDao { @@ -80,8 +78,8 @@ public class MapJobDao implements JobInstanceDao, JobExecutionDao { jobExecution.setId(new Long(currentId++)); } - public List findJobExecutions(JobInstance job) { - Set executions = (Set) executionsById.get(job.getId()); + public List findJobExecutions(JobInstance jobInstance) { + Set executions = (Set) executionsById.get(jobInstance.getId()); if( executions == null ){ return new ArrayList(); } @@ -90,43 +88,12 @@ public class MapJobDao implements JobInstanceDao, JobExecutionDao { } } - public void updateJobInstance(JobInstance job) { - // no-op - } - public void updateJobExecution(JobExecution jobExecution) { // no-op } - public JobExecution getJobExecution(Long jobExecutionId) { - - List jobExecutions = new ArrayList(); - - for(Iterator it = executionsById.entrySet().iterator();it.hasNext();){ - Entry entry = (Entry)it.next(); - Set executions = (Set)entry.getValue(); - for(Iterator executionsIt = executions.iterator();executionsIt.hasNext();){ - JobExecution jobExecution = (JobExecution)executionsIt.next(); - if(jobExecution.getId() == jobExecutionId){ - jobExecutions.add(jobExecution); - } - } - } - - if(jobExecutions.size() == 0){ - return null; - } - else if(jobExecutions.size() == 1){ - return (JobExecution)jobExecutions.get(0); - } - else{ - throw new IncorrectResultSizeDataAccessException("Multiple JobExecutions found for given id" - , 1, jobExecutions.size()); - } - } - public JobExecution getLastJobExecution(JobInstance jobInstance) { - // TODO Auto-generated method stub + // no-op return null; } 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 cfd5b3124..50f86f051 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 @@ -17,140 +17,70 @@ package org.springframework.batch.execution.repository.dao; import java.util.Map; -import java.util.Set; + 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; +import org.springframework.util.Assert; public class MapStepDao implements StepExecutionDao { - private static Map stepsByJobId; - private static Map executionsById; - private static Map restartsById; + private static Map executionsByJobExecutionId; + + private static Map contextsByStepExecutionId; + private static long currentId = 0; - + static { - stepsByJobId = TransactionAwareProxyFactory.createTransactionalMap(); - executionsById = TransactionAwareProxyFactory.createTransactionalMap(); - restartsById = TransactionAwareProxyFactory.createTransactionalMap(); + executionsByJobExecutionId = TransactionAwareProxyFactory.createTransactionalMap(); + contextsByStepExecutionId = TransactionAwareProxyFactory.createTransactionalMap(); } - + public static void clear() { - stepsByJobId.clear(); - executionsById.clear(); - restartsById.clear(); - } - - public ExecutionContext getExecutionContext(Long stepId) { - return (ExecutionContext) restartsById.get(stepId); - } - -// public int getStepExecutionCount(StepInstance stepInstance) { -// Set executions = (Set) executionsById.get(stepInstance.getId()); -// if (executions==null) return 0; -// return executions.size(); } - -// public void saveStepExecution(StepExecution stepExecution) { -// Set executions = (Set) executionsById.get(stepExecution.getStepId()); -// if (executions==null) { -// executions = TransactionAwareProxyFactory.createTransactionalSet(); -// executionsById.put(stepExecution.getStepId(), executions); -// } -// stepExecution.setId(new Long(currentId++)); -// executions.add(stepExecution); -// } -// -// public List findStepExecutions(StepInstance step, JobExecution jobExecution) { -// Set executions = (Set) executionsById.get(step.getId()); -// -// if(executions == null){ -// //no step executions, return empty array list. -// return new ArrayList(); -// } -// else{ -// return new ArrayList(executions); -// } -// } -// -// public StepExecution getStepExecution(Long stepExecutionId, -// StepInstance stepInstance) { -// -// List stepExecutions = new ArrayList(); -// -// for(Iterator it = executionsById.entrySet().iterator();it.hasNext();){ -// Entry entry = (Entry)it.next(); -// Set executions = (Set)entry.getValue(); -// for(Iterator executionsIt = executions.iterator();executionsIt.hasNext();){ -// Entity stepExecution = (Entity)executionsIt.next(); -// if(stepExecution.getId() == stepExecutionId){ -// stepExecutions.add(stepExecution); -// } -// } -// } -// -// if(stepExecutions.size() == 0){ -// return null; -// } -// else if(stepExecutions.size() == 1){ -// return (StepExecution)stepExecutions.get(0); -// } -// else{ -// throw new IncorrectResultSizeDataAccessException("Multiple StepExecutions found for given id" -// , 1, stepExecutions.size()); -// } -// } - - public void updateStepExecution(StepExecution stepExecution) { - // no-op + executionsByJobExecutionId.clear(); } public ExecutionContext findExecutionContext(StepExecution stepExecution) { - return null; + return (ExecutionContext) contextsByStepExecutionId.get(stepExecution.getId()); } public void saveExecutionContext(StepExecution stepExecution) { + contextsByStepExecutionId.put(stepExecution.getId(), stepExecution.getExecutionContext()); } public void updateExecutionContext(StepExecution stepExecution) { + Assert.notNull(contextsByStepExecutionId.get(stepExecution.getId()), "execution context should already be saved"); + contextsByStepExecutionId.put(stepExecution.getId(), stepExecution.getExecutionContext()); } public void saveStepExecution(StepExecution stepExecution) { - Set executions = (Set) executionsById.get(stepExecution.getId()); - if (executions==null) { - executions = TransactionAwareProxyFactory.createTransactionalSet(); - executionsById.put(stepExecution.getId(), executions); + Assert.notNull(stepExecution.getJobExecutionId()); + Map executions = (Map) executionsByJobExecutionId.get(stepExecution.getJobExecutionId()); + if (executions == null) { + executions = TransactionAwareProxyFactory.createTransactionalMap(); + executionsByJobExecutionId.put(stepExecution.getJobExecutionId(), executions); } stepExecution.setId(new Long(currentId++)); - executions.add(stepExecution); + executions.put(stepExecution.getStepName(), stepExecution); + } + + public void updateStepExecution(StepExecution stepExecution) { + Assert.notNull(stepExecution.getJobExecutionId()); + Map executions = (Map) executionsByJobExecutionId.get(stepExecution.getJobExecutionId()); + Assert.notNull(executions, "step executions for given job execution are expected to be already saved"); + Assert.notNull(executions.get(stepExecution.getStepName()), "step execution is expected to be already saved"); + executions.put(stepExecution.getStepName(), stepExecution); } 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(); -// if (stepExecution.getJobExecution().equals(jobExecution) && stepExecution.getStepName().equals(stepName)){ -// return stepExecution; -// } -// } - return null; + Map executions = (Map) executionsByJobExecutionId.get(jobExecution.getId()); + if (executions == null) { + return null; + } + + return (StepExecution) executions.get(step.getName()); } -// public StepExecution getLastStepExecution(String stepName, JobExecution jobExecution) { -// List executions = findStepExecutions(stepInstance, null); -// StepExecution lastExec = null; -// for (Iterator iterator = executions.iterator(); iterator.hasNext();) { -// StepExecution exec = (StepExecution) iterator.next(); -// if (lastExec == null) { -// lastExec = exec; -// continue; -// } -// if (lastExec.getStartTime().getTime() < exec.getStartTime().getTime()) { -// lastExec = exec; -// } -// } -// return lastExec; -// } } - diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java index 0f77d0f4f..fda801f5b 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java @@ -100,8 +100,10 @@ public class SimpleJobTests extends TestCase { job.setSteps(new ArrayList()); AbstractStep step = getStep("foo", "bar"); + step.setName("step1"); job.addStep(step); step = getStep("spam"); + step.setName("step2"); job.addStep(step); JobInstance jobInstance = repository.createJobExecution(job, new JobParameters()).getJobInstance(); @@ -168,8 +170,8 @@ public class SimpleJobTests extends TestCase { } public void testExceptionTerminates() throws Exception { -// Tasklet module = getTasklet(new String[] { "foo", "bar", "spam" }); AbstractStep step = getStep(new String[] { "foo", "bar", "spam" }); + step.setName("exceptionStep"); step.setItemWriter(new AbstractItemWriter() { public void write(Object data) throws Exception { throw new RuntimeException("Foo"); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapJobDaoTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapJobDaoTests.java index 4c6209ecd..e241d4f15 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapJobDaoTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapJobDaoTests.java @@ -78,22 +78,4 @@ public class MapJobDaoTests extends TestCase { dao.saveJobExecution(new JobExecution(job)); assertEquals(2, dao.getJobExecutionCount(job)); } - - public void testGetJobExecution(){ - - JobInstance jobInstance = dao.createJobInstance(fooJob, jobParameters); - JobExecution jobExecution = new JobExecution(jobInstance); - dao.saveJobExecution(jobExecution); - JobExecution tempExecution = dao.getJobExecution(jobExecution.getId()); - assertEquals(jobExecution, tempExecution); - } - - public void testGetNonExistantJobExecution(){ - - JobInstance jobInstance = dao.createJobInstance(fooJob, jobParameters); - JobExecution jobExecution = new JobExecution(jobInstance); - dao.saveJobExecution(jobExecution); - assertNull(dao.getJobExecution(new Long(999999))); - - } } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapStepDaoTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapStepDaoTests.java index 8d3179e5b..c48768d32 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapStepDaoTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/repository/dao/MapStepDaoTests.java @@ -44,28 +44,10 @@ public class MapStepDaoTests extends TestCase { } public void testSaveExecutionUpdatesId() throws Exception { - StepExecution execution = new StepExecution(step, new JobExecution(new JobInstance(new Long(1), - new JobParameters(), new JobSupport("jobName")))); + StepExecution execution = new StepExecution(step, new JobExecution(job, new Long(1))); assertNull(execution.getId()); dao.saveStepExecution(execution); assertNotNull(execution.getId()); } - public void testSaveExecutionContext() throws Exception { - // JobExecution jobExecution = new JobExecution(null); - // StepExecution stepExecution = new StepExecution(step, jobExecution, - // null); - // assertEquals(null, dao.findExecutionContext(stepExecution)); - // Properties data = new Properties(); - // data.setProperty("restart.key1", "restartData"); - // ExecutionContext executionContext = new ExecutionContext(data); - // stepExecution.setExecutionContext(executionContext); - // dao.saveStepExecution(stepExecution); - // StepExecution tempExecution = dao.getStepExecution(jobExecution, - // step); - // assertEquals(tempExecution, stepExecution); - // assertEquals(stepExecution.getExecutionContext(), - // tempExecution.getExecutionContext()); - } - }