Add tests and implement fix for very quick running (<1ms) jobs (incorporating suggestions made)

This commit is contained in:
David Thexton
2012-05-16 12:04:38 +01:00
committed by Dave Syer
parent bea966ce4a
commit 00aab3eafa
7 changed files with 52 additions and 5 deletions

View File

@@ -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<JobExecution> 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");

View File

@@ -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<String, JobParameter> parameters = new HashMap<String, JobParameter>();
parameters.put("timestamp", new JobParameter(new Date().getTime()));
parameters.put("random", new JobParameter((long) (Math.random() * JOB_PARAMETER_MAXIMUM)));
return new JobParameters(parameters);
}

View File

@@ -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");

View File

@@ -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;
}
}

View File

@@ -33,4 +33,16 @@
<constructor-arg value="2" />
</bean>
<batch:step id="s3" parent="taskletStep">
<batch:tasklet ref="tasklet3">
<batch:listeners>
<batch:listener ref="tasklet3"/>
</batch:listeners>
</batch:tasklet>
</batch:step>
<bean id="tasklet3" class="org.springframework.batch.test.sample.LoggingTasklet">
<constructor-arg value="3" />
</bean>
</beans>

View File

@@ -16,7 +16,8 @@
<job id="sampleFlowJob">
<step id="step1" parent="s1" next="step2"/>
<step id="step2" parent="s2"/>
<step id="step2" parent="s2" next="step3"/>
<step id="step3" parent="s3"/>
</job>
</beans:beans>

View File

@@ -14,6 +14,7 @@
<list>
<bean id="step1" parent="s1"/>
<bean id="step2" parent="s2"/>
<bean id="step3" parent="s3"/>
</list>
</property>
</bean>