Add range parameters to JobInstanceDao

This commit is contained in:
dsyer
2009-01-02 11:31:19 +00:00
parent bd3ab51c60
commit 63023c0f87
8 changed files with 62 additions and 12 deletions

View File

@@ -120,7 +120,7 @@ public class SimpleJobExplorer implements JobExplorer {
* @see org.springframework.batch.core.explore.JobExplorer#getLastJobInstances(java.lang.String, int)
*/
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
return jobInstanceDao.getLastJobInstances(jobName, count);
return jobInstanceDao.getJobInstances(jobName, start, count);
}
/*

View File

@@ -369,7 +369,6 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
jobExecution.setStatus(BatchStatus.STOPPING);
jobRepository.update(jobExecution);
// TODO: I'm not sure that we can really know if the execution stopped
return true;
}

View File

@@ -250,7 +250,7 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
* @seeorg.springframework.batch.core.repository.dao.JobInstanceDao#
* getLastJobInstances(java.lang.String, int)
*/
public List<JobInstance> getLastJobInstances(String jobName, final int count) {
public List<JobInstance> getJobInstances(String jobName, final int start, final int count) {
ResultSetExtractor extractor = new ResultSetExtractor() {
@@ -258,7 +258,10 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements
public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
int rowNum = 0;
while (rowNum < count && rs.next()) {
while (rowNum < start && rs.next()) {
rowNum++;
}
while (rowNum < start + count && rs.next()) {
ParameterizedRowMapper<JobInstance> rowMapper = new JobInstanceRowMapper();
list.add(rowMapper.mapRow(rs, rowNum));
rowNum++;

View File

@@ -61,7 +61,7 @@ public interface JobExecutionDao {
/**
* Because it may be possible that the status of a JobExecution is updated
* while running, the following method while synchronize only the status and
* while running, the following method will synchronize only the status and
* version fields.
*
* @param jobExecution to be updated.

View File

@@ -63,10 +63,11 @@ public interface JobInstanceDao {
*
*
* @param jobName the job name
* @param count the number of objects to return
* @param start the start index of the instances to return
* @param count the maximum number of objects to return
* @return the job instances with this name or empty if none
*/
List<JobInstance> getLastJobInstances(String jobName, int count);
List<JobInstance> getJobInstances(String jobName, int start, int count);
/**
* Retrieve the names of all job instances sorted alphabetically - i.e. jobs

View File

@@ -65,7 +65,7 @@ public class MapJobInstanceDao implements JobInstanceDao {
return result;
}
public List<JobInstance> getLastJobInstances(String jobName, int count) {
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
List<JobInstance> result = new ArrayList<JobInstance>();
for (JobInstance instance : jobInstances) {
if (instance.getJobName().equals(jobName)) {
@@ -78,8 +78,13 @@ public class MapJobInstanceDao implements JobInstanceDao {
return Long.signum(o2.getId() - o1.getId());
}
});
int length = count > result.size() ? result.size() : count;
return result.subList(0, length);
if (start>=result.size()) {
start = result.size();
}
if (start + count >=result.size()) {
count = result.size();
}
return result.subList(start, count);
}
public JobInstance getJobInstance(JobExecution jobExecution) {

View File

@@ -124,7 +124,7 @@ public class SimpleJobExplorerTests extends TestCase {
@Test
public void testGetLastJobInstances() throws Exception {
jobInstanceDao.getLastJobInstances("foo", 1);
jobInstanceDao.getJobInstances("foo", 0, 1);
EasyMock.expectLastCall().andReturn(Collections.singletonList(jobInstance));
replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
jobExplorer.getJobInstances("foo", 0, 1);

View File

@@ -126,7 +126,7 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalJ
// we need two instances of the same job to check ordering
dao.createJobInstance(fooJob, new JobParameters());
List<JobInstance> jobInstances = dao.getLastJobInstances(fooJob, 2);
List<JobInstance> jobInstances = dao.getJobInstances(fooJob, 0, 2);
assertEquals(2, jobInstances.size());
assertEquals(fooJob, jobInstances.get(0).getJobName());
assertEquals(fooJob, jobInstances.get(1).getJobName());
@@ -137,6 +137,48 @@ public abstract class AbstractJobInstanceDaoTests extends AbstractTransactionalJ
}
/**
* Create and retrieve a job instance.
*/
@Transactional
@Test
public void testGetLastInstancesPaged() throws Exception {
testCreateAndRetrieve();
// unrelated job instance that should be ignored by the query
dao.createJobInstance("anotherJob", new JobParameters());
// we need two instances of the same job to check ordering
dao.createJobInstance(fooJob, new JobParameters());
List<JobInstance> jobInstances = dao.getJobInstances(fooJob, 1, 2);
assertEquals(1, jobInstances.size());
assertEquals(fooJob, jobInstances.get(0).getJobName());
assertEquals(Integer.valueOf(0), jobInstances.get(0).getVersion());
}
/**
* Create and retrieve a job instance.
*/
@Transactional
@Test
public void testGetLastInstancesPastEnd() throws Exception {
testCreateAndRetrieve();
// unrelated job instance that should be ignored by the query
dao.createJobInstance("anotherJob", new JobParameters());
// we need two instances of the same job to check ordering
dao.createJobInstance(fooJob, new JobParameters());
List<JobInstance> jobInstances = dao.getJobInstances(fooJob, 4, 2);
assertEquals(0, jobInstances.size());
}
/**
* Trying to create instance twice for the same job+parameters causes error
*/