OPEN - issue BATCH-814: JobRepository should not require Step or Job (only their names)
Moved isJobInstanceExists check to JobRepository
This commit is contained in:
@@ -20,7 +20,6 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -35,13 +34,6 @@ public interface JobExplorer {
|
||||
*/
|
||||
List<JobInstance> getLastJobInstances(String jobName, int count);
|
||||
|
||||
/**
|
||||
* @param jobName the name of the job
|
||||
* @param jobParameters the parameters to match
|
||||
* @return true if a {@link JobInstance} already exists for this job name and job parameters
|
||||
*/
|
||||
boolean isJobInstanceExists(String jobName, JobParameters jobParameters);
|
||||
|
||||
/**
|
||||
* @param executionId
|
||||
* @return the {@link JobExecution} with this id, or null if not found
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Set;
|
||||
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobInstance;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.explore.JobExplorer;
|
||||
import org.springframework.batch.core.repository.dao.JobExecutionDao;
|
||||
import org.springframework.batch.core.repository.dao.JobInstanceDao;
|
||||
@@ -109,13 +108,6 @@ public class SimpleJobExplorer implements JobExplorer {
|
||||
return jobInstanceDao.getLastJobInstances(jobName, count);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.explore.JobExplorer#isJobInstanceExists(java.lang.String, org.springframework.batch.core.JobParameters)
|
||||
*/
|
||||
public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
|
||||
return jobInstanceDao.getJobInstance(jobName, jobParameters)!=null;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find all dependencies for a JobExecution, including JobInstance (which requires JobParameters)
|
||||
* plus StepExecutions
|
||||
|
||||
@@ -283,7 +283,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
|
||||
JobParameters jobParameters = jobParametersConverter.getJobParameters(PropertiesConverter
|
||||
.stringToProperties(parameters));
|
||||
|
||||
if (jobExplorer.isJobInstanceExists(jobName, jobParameters)) {
|
||||
if (jobRepository.isJobInstanceExists(jobName, jobParameters)) {
|
||||
throw new JobInstanceAlreadyExistsException(String.format(
|
||||
"Cannot start a job instance that already exists with name=%s and parameters=%s", jobName,
|
||||
parameters));
|
||||
|
||||
@@ -33,10 +33,20 @@ import org.springframework.batch.item.ExecutionContext;
|
||||
* </p>
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public interface JobRepository {
|
||||
|
||||
/**
|
||||
* Check if an instance of this job already exists with the parameters provided.
|
||||
*
|
||||
* @param jobName the name of the job
|
||||
* @param jobParameters the parameters to match
|
||||
* @return true if a {@link JobInstance} already exists for this job name and job parameters
|
||||
*/
|
||||
boolean isJobInstanceExists(String jobName, JobParameters jobParameters);
|
||||
|
||||
/**
|
||||
* Find or create a {@link JobExecution} for a given {@link Job} and
|
||||
* {@link JobParameters}. If the {@link Job} was already executed with these
|
||||
|
||||
@@ -81,6 +81,13 @@ public class SimpleJobRepository implements JobRepository {
|
||||
this.ecDao = ecDao;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see JobRepository#isJobInstanceExists(String, JobParameters)
|
||||
*/
|
||||
public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
|
||||
return jobInstanceDao.getJobInstance(jobName, jobParameters)!=null;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Create a {@link JobExecution} based on the passed in {@link Job} and
|
||||
|
||||
@@ -110,22 +110,4 @@ public class SimpleJobExplorerTests extends TestCase {
|
||||
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsJobInstanceFalse() throws Exception {
|
||||
jobInstanceDao.getJobInstance("foo", new JobParameters());
|
||||
EasyMock.expectLastCall().andReturn(null);
|
||||
replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
assertFalse(jobExplorer.isJobInstanceExists("foo", new JobParameters()));
|
||||
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsJobInstanceTrue() throws Exception {
|
||||
jobInstanceDao.getJobInstance("foo", new JobParameters());
|
||||
EasyMock.expectLastCall().andReturn(jobInstance);
|
||||
replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
assertTrue(jobExplorer.isJobInstanceExists("foo", new JobParameters()));
|
||||
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -165,20 +165,20 @@ public class SimpleJobOperatorTests {
|
||||
@Test
|
||||
public void testStartNewInstanceSunnyDay() throws Exception {
|
||||
jobParameters = new JobParameters();
|
||||
jobExplorer.isJobInstanceExists("foo", jobParameters);
|
||||
jobRepository.isJobInstanceExists("foo", jobParameters);
|
||||
EasyMock.expectLastCall().andReturn(false);
|
||||
EasyMock.replay(jobExplorer);
|
||||
EasyMock.replay(jobRepository);
|
||||
Long value = jobOperator.start("foo", "a=b");
|
||||
assertEquals(999, value.longValue());
|
||||
EasyMock.verify(jobExplorer);
|
||||
EasyMock.verify(jobRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartNewInstanceAlreadyExists() throws Exception {
|
||||
jobParameters = new JobParameters();
|
||||
jobExplorer.isJobInstanceExists("foo", jobParameters);
|
||||
jobRepository.isJobInstanceExists("foo", jobParameters);
|
||||
EasyMock.expectLastCall().andReturn(true);
|
||||
EasyMock.replay(jobExplorer);
|
||||
EasyMock.replay(jobRepository);
|
||||
try {
|
||||
jobOperator.start("foo", "a=b");
|
||||
fail("Expected JobInstanceAlreadyExistsException");
|
||||
@@ -186,7 +186,7 @@ public class SimpleJobOperatorTests {
|
||||
catch (JobInstanceAlreadyExistsException e) {
|
||||
// expected
|
||||
}
|
||||
EasyMock.verify(jobExplorer);
|
||||
EasyMock.verify(jobRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.easymock.EasyMock;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
@@ -66,7 +67,7 @@ public class SimpleJobRepositoryTests {
|
||||
|
||||
ExecutionContextDao ecDao;
|
||||
|
||||
JobInstance databaseJob;
|
||||
JobInstance jobInstance;
|
||||
|
||||
String databaseStep1;
|
||||
|
||||
@@ -102,7 +103,7 @@ public class SimpleJobRepositoryTests {
|
||||
|
||||
job.setSteps(stepConfigurations);
|
||||
|
||||
databaseJob = new JobInstance(new Long(1), jobParameters, job.getName());
|
||||
jobInstance = new JobInstance(new Long(1), jobParameters, job.getName());
|
||||
|
||||
databaseStep1 = "dbStep1";
|
||||
databaseStep2 = "dbStep2";
|
||||
@@ -198,4 +199,22 @@ public class SimpleJobRepositoryTests {
|
||||
assertTrue(stepExecution.isTerminateOnly());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsJobInstanceFalse() throws Exception {
|
||||
jobInstanceDao.getJobInstance("foo", new JobParameters());
|
||||
EasyMock.expectLastCall().andReturn(null);
|
||||
replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
assertFalse(jobRepository.isJobInstanceExists("foo", new JobParameters()));
|
||||
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsJobInstanceTrue() throws Exception {
|
||||
jobInstanceDao.getJobInstance("foo", new JobParameters());
|
||||
EasyMock.expectLastCall().andReturn(jobInstance);
|
||||
replay(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
assertTrue(jobRepository.isJobInstanceExists("foo", new JobParameters()));
|
||||
verify(jobExecutionDao, jobInstanceDao, stepExecutionDao);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -72,4 +72,12 @@ public class JobRepositorySupport implements JobRepository {
|
||||
public void updateExecutionContext(StepExecution stepExecution) {
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.batch.core.repository.JobRepository#isJobInstanceExists(java.lang.String, org.springframework.batch.core.JobParameters)
|
||||
*/
|
||||
public boolean isJobInstanceExists(String jobName, JobParameters jobParameters) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user