Add new methods to get the last job instance/execution in JobInstance/JobExecution DAOs

Before this commit, getting the last job instance/execution required to
load all job instances/executions from the database and filter them
on the client side in memory.

This commit introduces new methods that use database queries to get
the last job instance/execution without the need to load all job
instances/executions. This change improves memory consumption as well as
the performance of starting the next instance of a job.

Resolves BATCH-1784
This commit is contained in:
Mahmoud Ben Hassine
2019-06-17 15:32:25 +02:00
committed by Michael Minella
parent afc25dc873
commit 7c1fbede1b
9 changed files with 155 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2013 the original author or authors.
* Copyright 2006-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@ import org.springframework.batch.core.repository.dao.StepExecutionDao;
* @author Dave Syer
* @author Will Schipp
* @author Michael Minella
* @author Mahmoud Ben Hassine
*
*/
public class SimpleJobExplorerTests {
@@ -82,6 +83,13 @@ public class SimpleJobExplorerTests {
jobExplorer.getJobExecution(123L);
}
@Test
public void testGetLastJobExecution() {
when(jobExecutionDao.getLastJobExecution(jobInstance)).thenReturn(jobExecution);
JobExecution lastJobExecution = jobExplorer.getLastJobExecution(jobInstance);
assertEquals(jobExecution, lastJobExecution);
}
@Test
public void testMissingGetJobExecution() throws Exception {
when(jobExecutionDao.getJobExecution(123L)).thenReturn(null);
@@ -156,6 +164,13 @@ public class SimpleJobExplorerTests {
jobExplorer.getJobInstances("foo", 0, 1);
}
@Test
public void testGetLastJobInstance() {
when(jobInstanceDao.getLastJobInstance("foo")).thenReturn(jobInstance);
JobInstance lastJobInstance = jobExplorer.getLastJobInstance("foo");
assertEquals(jobInstance, lastJobInstance);
}
@Test
public void testGetJobNames() throws Exception {
jobInstanceDao.getJobNames();

View File

@@ -505,6 +505,16 @@ public class CommandLineJobRunnerTests {
throw new UnsupportedOperationException();
}
@Override
public JobInstance getLastJobInstance(String jobName) {
return null;
}
@Override
public JobExecution getLastJobExecution(JobInstance jobInstance) {
return null;
}
@Override
public List<JobInstance> getJobInstances(String jobName, int start, int count) {
if (jobInstances == null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -154,6 +154,33 @@ public abstract class AbstractJobInstanceDaoTests {
}
@Transactional
@Test
public void testGetLastInstance() 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, 0, 2);
assertEquals(2, jobInstances.size());
JobInstance lastJobInstance = dao.getLastJobInstance(fooJob);
assertNotNull(lastJobInstance);
assertEquals(fooJob, lastJobInstance.getJobName());
assertEquals("Last instance should be first on the list",
jobInstances.get(0), lastJobInstance);
}
@Transactional
@Test
public void testGetLastInstanceWhenNoInstance() {
JobInstance lastJobInstance = dao.getLastJobInstance("NonExistingJob");
assertNull(lastJobInstance);
}
/**
* Create and retrieve a job instance.
*/