IN PROGRESS - issue BATCH-340: Refactor JobRepository for greater clarity and consistency.

http://jira.springframework.org/browse/BATCH-340

Added getLastStepExecution method to StepExecutionDao interface and liberated JdbcStepInstanceDao from StepExecutionDao.
Removed the STEP_INSTANCE.LAST_EXECUTION_ID column from schema.
This commit is contained in:
robokaso
2008-02-13 16:13:11 +00:00
parent 9dbc53ed0f
commit 695cf0817d
20 changed files with 195 additions and 126 deletions

View File

@@ -57,7 +57,7 @@ import org.springframework.util.Assert;
*/
public class SimpleJobRepository implements JobRepository {
private JobInstanceDao jobDao;
private JobInstanceDao jobInstanceDao;
private JobExecutionDao jobExecutionDao;
@@ -75,7 +75,7 @@ public class SimpleJobRepository implements JobRepository {
public SimpleJobRepository(JobInstanceDao jobDao, JobExecutionDao jobExecutionDao, StepInstanceDao stepInstanceDao,
StepExecutionDao stepExecutionDao) {
super();
this.jobDao = jobDao;
this.jobInstanceDao = jobDao;
this.jobExecutionDao = jobExecutionDao;
this.stepInstanceDao = stepInstanceDao;
this.stepExecutionDao = stepExecutionDao;
@@ -165,7 +165,7 @@ public class SimpleJobRepository implements JobRepository {
* thread or process will block until this transaction has finished.
*/
jobs = jobDao.findJobInstances(job.getName(), jobParameters);
jobs = jobInstanceDao.findJobInstances(job.getName(), jobParameters);
}
if (jobs.size() == 1) {
@@ -247,7 +247,7 @@ public class SimpleJobRepository implements JobRepository {
Assert.notNull(job.getId(), "Job cannot be updated if it's ID is null. It must be obtained"
+ "from SimpleJobRepository.findOrCreateJob to be considered valid.");
jobDao.updateJobInstance(job);
jobInstanceDao.updateJobInstance(job);
}
/**
@@ -288,7 +288,8 @@ public class SimpleJobRepository implements JobRepository {
Assert.notNull(step.getId(), "Step cannot be updated if it's ID is null. It must be obtained"
+ "from SimpleJobRepository.findOrCreateJob to be considered valid.");
stepInstanceDao.updateStepInstance(step);
//TODO no-op to be removed
//stepInstanceDao.updateStepInstance(step);
}
@@ -299,7 +300,7 @@ public class SimpleJobRepository implements JobRepository {
*/
private JobInstance createJobInstance(Job job, JobParameters jobParameters) {
JobInstance jobInstance = jobDao.createJobInstance(job.getName(), jobParameters);
JobInstance jobInstance = jobInstanceDao.createJobInstance(job.getName(), jobParameters);
jobInstance.setJob(job);
jobInstance.setStepInstances(createStepInstances(jobInstance, job.getSteps()));
return jobInstance;
@@ -321,7 +322,7 @@ public class SimpleJobRepository implements JobRepository {
return stepInstances;
}
/*
/**
* Find Steps for the given list of Steps with a given JobId
*/
protected List findStepInstances(List steps, JobInstance jobInstance) {
@@ -332,7 +333,7 @@ public class SimpleJobRepository implements JobRepository {
Step stepConfiguration = (Step) i.next();
StepInstance stepInstance = stepInstanceDao.findStepInstance(jobInstance, stepConfiguration.getName());
if (stepInstance != null) {
stepInstance.setLastExecution(stepExecutionDao.getLastStepExecution(stepInstance));
if (stepInstance.getLastExecution() != null) {
ExecutionAttributes executionAttributes = stepExecutionDao.findExecutionAttributes(stepInstance
.getLastExecution().getId());

View File

@@ -84,6 +84,11 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao
private static final String UPDATE_STEP_EXECUTION = "UPDATE %PREFIX%STEP_EXECUTION set START_TIME = ?, END_TIME = ?, "
+ "STATUS = ?, COMMIT_COUNT = ?, TASK_COUNT = ?, TASK_STATISTICS = ?, CONTINUABLE = ? , EXIT_CODE = ?, "
+ "EXIT_MESSAGE = ?, VERSION = ? where STEP_EXECUTION_ID = ? and VERSION = ?";
private static final String FIND_LAST_STEP_EXECUTION = "SELECT STEP_EXECUTION_ID, JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, COMMIT_COUNT,"
+ " TASK_COUNT, TASK_STATISTICS, CONTINUABLE, EXIT_CODE, EXIT_MESSAGE from %PREFIX%STEP_EXECUTION where STEP_INSTANCE_ID = ?"
+ " and START_TIME = (SELECT max(START_TIME) FROM %PREFIX%STEP_EXECUTION where STEP_INSTANCE_ID = ?)";
private static final int EXIT_MESSAGE_LENGTH = 250;
@@ -169,6 +174,21 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao
return stepExecution;
}
public StepExecution getLastStepExecution(StepInstance stepInstance) {
Long stepInstanceId = stepInstance.getId();
List executions = getJdbcTemplate().query(getQuery(FIND_LAST_STEP_EXECUTION),
new Object[] { stepInstanceId, stepInstanceId }, new StepExecutionRowMapper(stepInstance));
Assert.state(executions.size() <= 1, "There must be at most one latest execution");
if (executions.size() == 0) {
return null;
}
else {
return (StepExecution) executions.get(0);
}
}
public int getStepExecutionCount(StepInstance step) {
@@ -500,4 +520,6 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao
return null;
}
}
}

View File

@@ -5,7 +5,6 @@ import java.sql.SQLException;
import java.util.List;
import org.springframework.batch.core.domain.JobInstance;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.domain.StepInstance;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
@@ -36,17 +35,13 @@ public class JdbcStepInstanceDao extends AbstractJdbcBatchMetadataDao implements
private static final String CREATE_STEP = "INSERT into %PREFIX%STEP_INSTANCE(STEP_INSTANCE_ID, JOB_INSTANCE_ID, STEP_NAME) values (?, ?, ?)";
private static final String FIND_STEP = "SELECT STEP_INSTANCE_ID, LAST_STEP_EXECUTION_ID from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ? "
private static final String FIND_STEP = "SELECT STEP_INSTANCE_ID from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ? "
+ "and STEP_NAME = ?";
private static final String FIND_STEPS = "SELECT STEP_INSTANCE_ID, LAST_STEP_EXECUTION_ID, STEP_NAME from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ?";
private static final String UPDATE_STEP = "UPDATE %PREFIX%STEP_INSTANCE set LAST_STEP_EXECUTION_ID = ? where STEP_INSTANCE_ID = ?";
private static final String FIND_STEPS = "SELECT STEP_INSTANCE_ID, STEP_NAME from %PREFIX%STEP_INSTANCE where JOB_INSTANCE_ID = ?";
private DataFieldMaxValueIncrementer stepIncrementer;
private StepExecutionDao stepExecutionDao;
/**
* Create a step with the given job's id, and the provided step name. A
* unique id is created for the step using an incrementer. (@link
@@ -129,19 +124,19 @@ public class JdbcStepInstanceDao extends AbstractJdbcBatchMetadataDao implements
return getJdbcTemplate().query(getQuery(FIND_STEPS), parameters, rowMapper);
}
/**
* @see StepDao#updateStepInstance(StepInstance)
* @throws IllegalArgumentException if step, or it's status and id is null.
*/
public void updateStepInstance(final StepInstance step) {
Assert.notNull(step, "Step cannot be null.");
Assert.notNull(step.getId(), "Step Id cannot be null.");
Object[] parameters = new Object[] { step.getLastExecution().getId(), step.getId() };
getJdbcTemplate().update(getQuery(UPDATE_STEP), parameters);
}
// /**
// * @see StepDao#updateStepInstance(StepInstance)
// * @throws IllegalArgumentException if step, or it's status and id is null.
// */
// public void updateStepInstance(final StepInstance step) {
//
// Assert.notNull(step, "Step cannot be null.");
// Assert.notNull(step.getId(), "Step Id cannot be null.");
//
// Object[] parameters = new Object[] { step.getLastExecution().getId(), step.getId() };
//
// getJdbcTemplate().update(getQuery(UPDATE_STEP), parameters);
// }
public void setStepIncrementer(DataFieldMaxValueIncrementer stepIncrementer) {
this.stepIncrementer = stepIncrementer;
@@ -161,23 +156,16 @@ public class JdbcStepInstanceDao extends AbstractJdbcBatchMetadataDao implements
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
if (stepName == null) {
stepName = rs.getString(3);
stepName = rs.getString(2);
}
StepInstance stepInstance = new StepInstance(jobInstance, stepName, new Long(rs.getLong(1)));
StepExecution lastExecution = stepExecutionDao.getStepExecution(new Long(rs.getLong(2)), stepInstance);
stepInstance.setLastExecution(lastExecution);
return stepInstance;
}
}
public void setStepExecutionDao(StepExecutionDao stepExecutionDao) {
this.stepExecutionDao = stepExecutionDao;
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(stepIncrementer, "StepIncrementer cannot be null.");
Assert.notNull(stepExecutionDao, "StepExecutionDao cannot be null.");
}
}

View File

@@ -161,5 +161,21 @@ public class MapStepDao implements StepDao {
public void updateExecutionAttributes(Long executionId,
ExecutionAttributes executionAttributes) {
}
public StepExecution getLastStepExecution(StepInstance stepInstance) {
List executions = findStepExecutions(stepInstance);
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;
}
}

View File

@@ -83,4 +83,8 @@ public interface StepExecutionDao {
*/
void updateExecutionAttributes(final Long executionId, ExecutionAttributes executionAttributes);
/**
* @return the last execution of the given instance
*/
StepExecution getLastStepExecution(StepInstance stepInstance);
}

View File

@@ -35,12 +35,12 @@ public interface StepInstanceDao {
*/
StepInstance createStepInstance(JobInstance jobInstance, String stepName);
/**
* Update an existing StepInstance.
*
* Preconditions: StepInstance must have an ID.
*
* @param job
*/
void updateStepInstance(StepInstance stepInstance);
// /**
// * Update an existing StepInstance.
// *
// * Preconditions: StepInstance must have an ID.
// *
// * @param job
// */
// void updateStepInstance(StepInstance stepInstance);
}

View File

@@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID BIGINT PRIMARY KEY ,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT PRIMARY KEY ,

View File

@@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,

View File

@@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID BIGINT IDENTITY PRIMARY KEY ,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY ,

View File

@@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID BIGINT unsigned PRIMARY KEY ,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT unsigned PRIMARY KEY ,

View File

@@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID NUMBER(38) PRIMARY KEY ,
VERSION NUMBER(38),
JOB_INSTANCE_ID NUMBER(38) NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID NUMBER(38) PRIMARY KEY ,

View File

@@ -42,8 +42,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID BIGINT PRIMARY KEY ,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT PRIMARY KEY ,

View File

@@ -29,8 +29,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID ${BIGINT} $!{IDENTITY} PRIMARY KEY $!{GENERATED},
VERSION ${BIGINT},
JOB_INSTANCE_ID ${BIGINT} NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID ${BIGINT} $!{IDENTITY} PRIMARY KEY $!{GENERATED},

View File

@@ -89,4 +89,9 @@ public class MockStepDao implements StepDao {
return null;
}
public StepExecution getLastStepExecution(StepInstance stepInstance) {
// TODO Auto-generated method stub
return null;
}
}

View File

@@ -40,12 +40,12 @@ import org.springframework.batch.execution.repository.dao.StepDao;
import org.springframework.batch.item.ExecutionAttributes;
/**
* Test SimpleJobRepository. The majority of test cases are tested using EasyMock,
* however, there were some issues with using it for the stepDao when testing finding
* or creating steps, so an actual mock class had to be written.
*
* Test SimpleJobRepository. The majority of test cases are tested using
* EasyMock, however, there were some issues with using it for the stepDao when
* testing finding or creating steps, so an actual mock class had to be written.
*
* @author Lucas Ward
*
*
*/
public class SimpleJobRepositoryTests extends TestCase {
@@ -76,7 +76,7 @@ public class SimpleJobRepositoryTests extends TestCase {
StepInstance databaseStep2;
List steps;
ExecutionAttributes executionAttributes;
private JobExecution jobExecution;
@@ -86,10 +86,9 @@ public class SimpleJobRepositoryTests extends TestCase {
jobDao = (JobDao) jobDaoControl.getMock();
stepDao = (StepDao) stepDaoControl.getMock();
jobRepository = new SimpleJobRepository(jobDao, jobDao, stepDao, stepDao);
jobRepository = new SimpleJobRepository(jobDao, jobDao, stepDao, stepDao);
jobParameters = new JobParametersBuilder().toJobParameters();
jobConfiguration = new JobSupport();
jobConfiguration.setBeanName("RepositoryTest");
@@ -104,7 +103,7 @@ public class SimpleJobRepositoryTests extends TestCase {
stepConfigurations.add(stepConfiguration2);
jobConfiguration.setSteps(stepConfigurations);
databaseJob = new JobInstance(new Long(1), jobParameters) {
public JobExecution createJobExecution() {
jobExecution = super.createJobExecution();
@@ -120,7 +119,7 @@ public class SimpleJobRepositoryTests extends TestCase {
steps = new ArrayList();
steps.add(databaseStep1);
steps.add(databaseStep2);
executionAttributes = new ExecutionAttributes();
}
@@ -140,12 +139,13 @@ public class SimpleJobRepositoryTests extends TestCase {
stepDao.createStepInstance(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
jobDao.saveJobExecution(new JobExecution(databaseJob));
jobDaoControl.setMatcher(new ArgumentsMatcher(){
jobDaoControl.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob);
}
public String toString(Object[] arguments) {
return ""+arguments[0];
return "" + arguments[0];
}
});
stepDaoControl.replay();
@@ -160,20 +160,35 @@ public class SimpleJobRepositoryTests extends TestCase {
assertTrue(step.equals(databaseStep2));
}
public void testRestartedJob() throws Exception{
public void testRestartedJob() throws Exception {
final List executions = new ArrayList();
JobExecution execution = databaseJob.createJobExecution();
executions.add(execution);
// For this test it is important that the execution is finished
// and the executions in the list contain one with an end date
execution.setEndTime(new Date(System.currentTimeMillis()));
StepExecution databaseStep1Exec = new StepExecution(databaseStep1, execution, new Long(1));
StepExecution databaseStep2Exec = new StepExecution(databaseStep2, execution, new Long(2));
List jobs = new ArrayList();
jobDao.findJobInstances(jobConfiguration.getName(), jobParameters);
jobs.add(databaseJob);
jobDaoControl.setReturnValue(jobs);
stepDao.findStepInstance(databaseJob, "TestStep1");
stepDaoControl.setReturnValue(databaseStep1);
stepDao.findExecutionAttributes(databaseStep1.getLastExecution().getId());
stepDao.getLastStepExecution(databaseStep1);
stepDaoControl.setReturnValue(databaseStep1Exec);
stepDao.findExecutionAttributes(databaseStep1Exec.getId());
stepDaoControl.setReturnValue(executionAttributes);
stepDao.getStepExecutionCount(databaseStep1);
stepDaoControl.setReturnValue(1);
stepDao.findStepInstance(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
stepDao.findExecutionAttributes(databaseStep2.getLastExecution().getId());
stepDao.getLastStepExecution(databaseStep2);
stepDaoControl.setReturnValue(databaseStep2Exec);
stepDao.findExecutionAttributes(databaseStep2Exec.getId());
stepDaoControl.setReturnValue(executionAttributes);
stepDao.getStepExecutionCount(databaseStep2);
stepDaoControl.setReturnValue(1);
@@ -181,22 +196,17 @@ public class SimpleJobRepositoryTests extends TestCase {
jobDao.getJobExecutionCount(databaseJob.getId());
jobDaoControl.setReturnValue(1);
jobDao.findJobExecutions(databaseJob);
final List executions = new ArrayList();
JobExecution execution =databaseJob.createJobExecution();
executions.add(execution);
// For this test it is important that the execution is finished
// and the executions in the list contain one with an end date
execution.setEndTime(new Date(System.currentTimeMillis()));
jobDaoControl.setReturnValue(executions);
jobDao.updateJobInstance(databaseJob);
jobDao.saveJobExecution(new JobExecution(databaseJob));
jobDaoControl.setMatcher(new ArgumentsMatcher(){
jobDaoControl.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
JobExecution execution = (JobExecution) actual[0];
return execution.getJobInstance().equals(databaseJob);
}
public String toString(Object[] arguments) {
return ""+arguments[0];
return "" + arguments[0];
}
});
jobDaoControl.setVoidCallable();
@@ -213,8 +223,9 @@ public class SimpleJobRepositoryTests extends TestCase {
assertTrue(step.getStepExecutionCount() == 1);
}
//Test that a restartable job that has multiple instances throws an exception.
public void testFindRestartableJobWithMultipleInstances() throws Exception{
// Test that a restartable job that has multiple instances throws an
// exception.
public void testFindRestartableJobWithMultipleInstances() throws Exception {
List jobs = new ArrayList();
jobs.add(databaseJob);
@@ -223,54 +234,64 @@ public class SimpleJobRepositoryTests extends TestCase {
jobDaoControl.setReturnValue(jobs);
jobDaoControl.replay();
try{
try {
jobRepository.createJobExecution(jobConfiguration, jobParameters);
fail("Expected BatchRestartException");
}catch(BatchRestartException e){
//expected
}
catch (BatchRestartException e) {
// expected
}
jobDaoControl.verify();
}
public void testRestartJobStartLimitExceeded() throws Exception{
public void testRestartJobStartLimitExceeded() throws Exception {
jobConfiguration.setStartLimit(1);
StepExecution databaseStep1Exec = new StepExecution(databaseStep1, null, new Long(1));
StepExecution databaseStep2Exec = new StepExecution(databaseStep2, null, new Long(2));
List jobs = new ArrayList();
jobDao.findJobInstances(jobConfiguration.getName(), jobParameters);
jobs.add(databaseJob);
jobDaoControl.setReturnValue(jobs);
stepDao.findStepInstance(databaseJob, "TestStep1");
stepDaoControl.setReturnValue(databaseStep1);
stepDao.findExecutionAttributes(databaseStep1.getLastExecution().getId());
stepDao.getLastStepExecution(databaseStep1);
stepDaoControl.setReturnValue(databaseStep1Exec);
stepDao.findExecutionAttributes(databaseStep1Exec.getId());
stepDaoControl.setReturnValue(executionAttributes);
stepDao.getStepExecutionCount(databaseStep1);
stepDaoControl.setReturnValue(1);
stepDao.findStepInstance(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
stepDao.findExecutionAttributes(databaseStep2.getLastExecution().getId());
stepDao.getLastStepExecution(databaseStep2);
stepDaoControl.setReturnValue(databaseStep2Exec);
stepDao.findExecutionAttributes(databaseStep2Exec.getId());
stepDaoControl.setReturnValue(executionAttributes);
stepDao.getStepExecutionCount(databaseStep2);
stepDaoControl.setReturnValue(1);
stepDaoControl.replay();
jobDao.getJobExecutionCount(databaseJob.getId());
//return a greater execution count then the start limit, should throw exception
// return a greater execution count then the start limit, should throw
// exception
jobDaoControl.setReturnValue(2);
jobDaoControl.replay();
try{
try {
jobRepository.createJobExecution(jobConfiguration, jobParameters);
fail();
}catch(BatchRestartException ex){
//expected
}
catch (BatchRestartException ex) {
// expected
}
jobDaoControl.verify();
stepDaoControl.verify();
}
public void testCreateNonRestartableJob() throws Exception{
public void testCreateNonRestartableJob() throws Exception {
List jobs = new ArrayList();
jobConfiguration.setRestartable(false);
@@ -284,12 +305,13 @@ public class SimpleJobRepositoryTests extends TestCase {
stepDao.createStepInstance(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
jobDao.saveJobExecution(new JobExecution(databaseJob));
jobDaoControl.setMatcher(new ArgumentsMatcher(){
jobDaoControl.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob);
}
public String toString(Object[] arguments) {
return ""+arguments[0];
return "" + arguments[0];
}
});
stepDaoControl.replay();
@@ -307,7 +329,7 @@ public class SimpleJobRepositoryTests extends TestCase {
public void testUpdateJob() {
// failure scenario - no ID
JobInstance updateJob;
JobInstance updateJob;
try {
updateJob = new JobInstance(null, jobParameters);
jobRepository.update(updateJob);
@@ -370,12 +392,12 @@ public class SimpleJobRepositoryTests extends TestCase {
// successful update
step = new StepInstance(new Long(0L));
stepDao.updateStepInstance(step);
// stepDao.updateStepInstance(step);
stepDaoControl.replay();
jobRepository.update(step);
}
public void testUpdateStepExecution(){
public void testUpdateStepExecution() {
StepExecution stepExecution = new StepExecution(new StepInstance(new Long(10L)), null, new Long(1));
stepExecution.setId(new Long(11));
ExecutionAttributes executionAttributes = new ExecutionAttributes();
@@ -387,7 +409,7 @@ public class SimpleJobRepositoryTests extends TestCase {
stepDaoControl.verify();
}
public void testSaveExistingStepExecution(){
public void testSaveExistingStepExecution() {
StepExecution stepExecution = new StepExecution(new StepInstance(new Long(10L)), null, null);
ExecutionAttributes executionAttributes = new ExecutionAttributes();
stepExecution.setExecutionAttributes(executionAttributes);
@@ -413,10 +435,10 @@ public class SimpleJobRepositoryTests extends TestCase {
}
/*
* Test to ensure that if a StepDao returns invalid
* restart data, it is corrected.
* Test to ensure that if a StepDao returns invalid restart data, it is
* corrected.
*/
public void testCreateStepsFixesInvalidExecutionAttributes() throws Exception{
public void testCreateStepsFixesInvalidExecutionAttributes() throws Exception {
List jobs = new ArrayList();
@@ -429,12 +451,13 @@ public class SimpleJobRepositoryTests extends TestCase {
stepDao.createStepInstance(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
jobDao.saveJobExecution(new JobExecution(databaseJob));
jobDaoControl.setMatcher(new ArgumentsMatcher(){
jobDaoControl.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob);
}
public String toString(Object[] arguments) {
return ""+arguments[0];
return "" + arguments[0];
}
});
stepDaoControl.replay();
@@ -448,20 +471,28 @@ public class SimpleJobRepositoryTests extends TestCase {
assertTrue(step.equals(databaseStep2));
}
public void testFindStepsFixesInvalidExecutionAttributes() throws Exception{
public void testFindStepsFixesInvalidExecutionAttributes() throws Exception {
StepExecution databaseStep1Exec = new StepExecution(databaseStep1, null, new Long(1));
StepExecution databaseStep2Exec = new StepExecution(databaseStep2, null, new Long(2));
List jobs = new ArrayList();
jobDao.findJobInstances(jobConfiguration.getName(), jobParameters);
jobs.add(databaseJob);
jobDaoControl.setReturnValue(jobs);
stepDao.findStepInstance(databaseJob, "TestStep1");
stepDaoControl.setReturnValue(databaseStep1);
stepDao.findExecutionAttributes(databaseStep1.getLastExecution().getId());
stepDao.getLastStepExecution(databaseStep1);
stepDaoControl.setReturnValue(databaseStep1Exec);
stepDao.findExecutionAttributes(databaseStep1Exec.getId());
stepDaoControl.setReturnValue(executionAttributes);
stepDao.getStepExecutionCount(databaseStep1);
stepDaoControl.setReturnValue(1);
stepDao.findStepInstance(databaseJob, "TestStep2");
stepDaoControl.setReturnValue(databaseStep2);
stepDao.findExecutionAttributes(databaseStep2.getLastExecution().getId());
stepDao.getLastStepExecution(databaseStep2);
stepDaoControl.setReturnValue(databaseStep2Exec);
stepDao.findExecutionAttributes(databaseStep2Exec.getId());
stepDaoControl.setReturnValue(executionAttributes);
stepDao.getStepExecutionCount(databaseStep2);
stepDaoControl.setReturnValue(1);
@@ -473,12 +504,13 @@ public class SimpleJobRepositoryTests extends TestCase {
jobDao.updateJobInstance(databaseJob);
jobDaoControl.setVoidCallable();
jobDao.saveJobExecution(new JobExecution(databaseJob));
jobDaoControl.setMatcher(new ArgumentsMatcher(){
jobDaoControl.setMatcher(new ArgumentsMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
return ((JobExecution) actual[0]).getJobInstance().equals(databaseJob);
}
public String toString(Object[] arguments) {
return ""+arguments[0];
return "" + arguments[0];
}
});
jobDaoControl.replay();

View File

@@ -100,7 +100,7 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour
stepExecution.setStartTime(new Date(System.currentTimeMillis()));
stepExecutionDao.saveStepExecution(stepExecution);
step1.setLastExecution(stepExecution);
stepInstanceDao.updateStepInstance(step1);
//stepInstanceDao.updateStepInstance(step1);
executionAttributes = new ExecutionAttributes();
executionAttributes.putString("1", "testString1");
@@ -158,7 +158,7 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour
public void testUpdateStepWithoutExecutionAttributes() {
stepInstanceDao.updateStepInstance(step1);
//stepInstanceDao.updateStepInstance(step1);
StepInstance tempStep = stepInstanceDao.findStepInstance(jobInstance, step1.getName());
assertEquals(tempStep, step1);
}
@@ -265,5 +265,16 @@ public abstract class AbstractStepDaoTests extends AbstractTransactionalDataSour
attributes = stepExecutionDao.findExecutionAttributes(stepExecution.getId());
assertEquals(executionAttributes, attributes);
}
public void testGetLastStepExecution() {
StepExecution lastExecution = new StepExecution(step1, jobExecution, null);
lastExecution.setStatus(BatchStatus.STARTED);
int JUMP_INTO_FUTURE = 1000; // makes sure start time is 'greatest'
lastExecution.setStartTime(new Date(System.currentTimeMillis() + JUMP_INTO_FUTURE));
stepExecutionDao.saveStepExecution(lastExecution);
assertEquals(lastExecution, stepExecutionDao.getLastStepExecution(step1));
}
}

View File

@@ -83,11 +83,11 @@ public class JdbcStepDaoPrefixTests extends TestCase {
assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP_EXECUTION") != -1);
}
public void testModifiedUpdateStep(){
stepInstanceDao.setTablePrefix("FOO_");
stepInstanceDao.updateStepInstance(step);
assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP") != -1);
}
// public void testModifiedUpdateStep(){
// stepInstanceDao.setTablePrefix("FOO_");
// stepInstanceDao.updateStepInstance(step);
// assertTrue(jdbcTemplate.getSqlStatement().indexOf("FOO_STEP") != -1);
// }
public void testModifiedCreateStep(){
stepInstanceDao.setTablePrefix("FOO_");
@@ -143,10 +143,10 @@ public class JdbcStepDaoPrefixTests extends TestCase {
assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP") != -1);
}
public void testDefaultUpdateStep(){
stepInstanceDao.updateStepInstance(step);
assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP") != -1);
}
// public void testDefaultUpdateStep(){
// stepInstanceDao.updateStepInstance(step);
// assertTrue(jdbcTemplate.getSqlStatement().indexOf("BATCH_STEP") != -1);
// }
public void testDefaultFindStepExecutions(){
stepExecutionDao.findStepExecutions(step);

View File

@@ -29,8 +29,7 @@ CREATE TABLE BATCH_STEP_INSTANCE (
STEP_INSTANCE_ID BIGINT IDENTITY PRIMARY KEY ,
VERSION BIGINT,
JOB_INSTANCE_ID BIGINT NOT NULL,
STEP_NAME VARCHAR(100) NOT NULL,
LAST_STEP_EXECUTION_ID BIGINT);
STEP_NAME VARCHAR(100) NOT NULL);
CREATE TABLE BATCH_STEP_EXECUTION (
STEP_EXECUTION_ID BIGINT IDENTITY PRIMARY KEY ,

View File

@@ -18,8 +18,7 @@
<bean id="stepInstanceDao" class="org.springframework.batch.execution.repository.dao.JdbcStepInstanceDao" >
<property name="jdbcTemplate" ref="jdbcTemplate" />
<property name="stepIncrementer" ref="stepIncrementer" />
<property name="stepExecutionDao" ref="stepExecutionDao" />
<property name="stepIncrementer" ref="stepIncrementer" />
</bean>
<bean id="stepExecutionDao" class="org.springframework.batch.execution.repository.dao.JdbcStepExecutionDao" >

View File

@@ -68,7 +68,6 @@
class="org.springframework.batch.execution.repository.dao.JdbcStepInstanceDao">
<property name="jdbcTemplate" ref="jdbcTemplate" />
<property name="stepIncrementer" ref="stepIncrementer" />
<property name="stepExecutionDao" ref="stepExecutionDao" />
</bean>
<bean id="stepExecutionDao" lazy-init="true"