diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java index 4eb7b9e6d..2cdad87f6 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/repository/dao/JdbcJobExecutionDao.java @@ -68,7 +68,7 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements + " from %PREFIX%JOB_EXECUTION where JOB_INSTANCE_ID = ? order by JOB_EXECUTION_ID desc"; private static final String GET_LAST_EXECUTION = "SELECT JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, EXIT_CODE, EXIT_MESSAGE, CREATE_TIME, LAST_UPDATED, VERSION " - + "from %PREFIX%JOB_EXECUTION where JOB_INSTANCE_ID = ? and CREATE_TIME = (SELECT max(CREATE_TIME) from %PREFIX%JOB_EXECUTION where JOB_INSTANCE_ID = ?)"; + + "from %PREFIX%JOB_EXECUTION E where JOB_INSTANCE_ID = ? and JOB_EXECUTION_ID = (SELECT max(JOB_EXECUTION_ID) from %PREFIX%JOB_EXECUTION E2 where E.JOB_INSTANCE_ID = E2.JOB_INSTANCE_ID)"; private static final String GET_EXECUTION_BY_ID = "SELECT JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, EXIT_CODE, EXIT_MESSAGE, CREATE_TIME, LAST_UPDATED, VERSION" + " from %PREFIX%JOB_EXECUTION where JOB_EXECUTION_ID = ?"; @@ -221,7 +221,7 @@ public class JdbcJobExecutionDao extends AbstractJdbcBatchMetadataDao implements Long id = jobInstance.getId(); List executions = getJdbcTemplate().query(getQuery(GET_LAST_EXECUTION), - new JobExecutionRowMapper(jobInstance), id, id); + new JobExecutionRowMapper(jobInstance), id); Assert.state(executions.size() <= 1, "There must be at most one latest job execution"); diff --git a/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java b/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java index 5d0a80574..9bdd78ab1 100644 --- a/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java +++ b/spring-batch-test/src/main/java/org/springframework/batch/test/JobLauncherTestUtils.java @@ -16,7 +16,6 @@ package org.springframework.batch.test; -import java.util.Date; import java.util.HashMap; import java.util.Map; @@ -67,6 +66,8 @@ import org.springframework.context.ApplicationContext; */ public class JobLauncherTestUtils { + private static final long JOB_PARAMETER_MAXIMUM = 1000000; + /** Logger */ protected final Log logger = LogFactory.getLog(getClass()); @@ -157,7 +158,7 @@ public class JobLauncherTestUtils { */ public JobParameters getUniqueJobParameters() { Map parameters = new HashMap(); - parameters.put("timestamp", new JobParameter(new Date().getTime())); + parameters.put("random", new JobParameter((long) (Math.random() * JOB_PARAMETER_MAXIMUM))); return new JobParameters(parameters); } diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java index 8d5055f10..ee143529d 100644 --- a/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/AbstractSampleJobTests.java @@ -12,6 +12,7 @@ import org.springframework.batch.test.sample.SampleTasklet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; +import org.springframework.test.annotation.Repeat; import org.springframework.test.context.ContextConfiguration; /** @@ -74,6 +75,13 @@ public abstract class AbstractSampleJobTests { } @Test + @Repeat(10) + public void testStep3Execution() throws Exception { + // logging only, may complete in < 1ms (repeat so that it's likely to for at least one of those times) + assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchStep("step3").getStatus()); + } + + @Test public void testStepLaunchJobContextEntry() { ExecutionContext jobContext = new ExecutionContext(); jobContext.put("key1", "value1"); diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/sample/LoggingTasklet.java b/spring-batch-test/src/test/java/org/springframework/batch/test/sample/LoggingTasklet.java new file mode 100644 index 000000000..1a94fcc26 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/sample/LoggingTasklet.java @@ -0,0 +1,24 @@ +package org.springframework.batch.test.sample; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +public class LoggingTasklet implements Tasklet { + + protected static final Log logger = LogFactory.getLog(LoggingTasklet.class); + + private int id = 0; + + public LoggingTasklet(int id) { + this.id = id; + } + + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { + logger.info("tasklet executing: id=" + id); + return RepeatStatus.FINISHED; + } +} diff --git a/spring-batch-test/src/test/resources/jobs/sample-steps.xml b/spring-batch-test/src/test/resources/jobs/sample-steps.xml index 3c98666a7..99a5505da 100644 --- a/spring-batch-test/src/test/resources/jobs/sample-steps.xml +++ b/spring-batch-test/src/test/resources/jobs/sample-steps.xml @@ -33,4 +33,16 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml b/spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml index dbd008b80..043514a94 100644 --- a/spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml +++ b/spring-batch-test/src/test/resources/jobs/sampleFlowJob.xml @@ -16,7 +16,8 @@ - + + \ No newline at end of file diff --git a/spring-batch-test/src/test/resources/jobs/sampleSimpleJob.xml b/spring-batch-test/src/test/resources/jobs/sampleSimpleJob.xml index cd07407a8..cd87fdeee 100644 --- a/spring-batch-test/src/test/resources/jobs/sampleSimpleJob.xml +++ b/spring-batch-test/src/test/resources/jobs/sampleSimpleJob.xml @@ -14,6 +14,7 @@ +