IN PROGRESS - BATCH-858: Pause / resume of Job

added pause to JobOperator and introduced BatchStatus.PAUSED
This commit is contained in:
robokaso
2008-10-21 12:40:37 +00:00
parent c3df035607
commit 9802f531cb
4 changed files with 51 additions and 31 deletions

View File

@@ -25,7 +25,7 @@ package org.springframework.batch.core;
public enum BatchStatus {
COMPLETED, STARTED, STARTING, FAILED, STOPPING, STOPPED, UNKNOWN;
COMPLETED, STARTED, STARTING, FAILED, STOPPING, STOPPED, UNKNOWN, PAUSED;
public static BatchStatus max(BatchStatus status1, BatchStatus status2) {
if (status1.compareTo(status2)<0) {

View File

@@ -74,6 +74,8 @@ public interface JobOperator {
JobRestartException, JobExecutionAlreadyRunningException, JobInstanceAlreadyCompleteException;
boolean stop(long executionId) throws NoSuchJobExecutionException;
boolean pause(long executionId) throws NoSuchJobExecutionException;
String getSummary(long executionId) throws NoSuchJobExecutionException;

View File

@@ -70,7 +70,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
private JobExplorer jobExplorer;
private JobLauncher jobLauncher;
private JobRepository jobRepository;
private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter();
@@ -112,7 +112,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
public void setJobExplorer(JobExplorer jobExplorer) {
this.jobExplorer = jobExplorer;
}
public void setJobRepository(JobRepository jobRepository) {
this.jobRepository = jobRepository;
}
@@ -166,7 +166,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
list.add(jobInstance.getId());
}
if (list.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) {
throw new NoSuchJobException("No such job (either in registry or in historical data): "+jobName);
throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName);
}
return list;
}
@@ -179,10 +179,8 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
* lang.Long)
*/
public String getParameters(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
if (jobExecution == null) {
throw new NoSuchJobExecutionException(String.format("No job execution with id=%d", executionId));
}
JobExecution jobExecution = findExecutionById(executionId);
return PropertiesConverter.propertiesToString(jobParametersConverter.getProperties(jobExecution
.getJobInstance().getJobParameters()));
}
@@ -200,7 +198,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
set.add(jobExecution.getId());
}
if (set.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) {
throw new NoSuchJobException("No such job (either in registry or in historical data): "+jobName);
throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName);
}
return set;
}
@@ -213,10 +211,8 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
* (java.lang.Long)
*/
public Map<Long, String> getStepExecutionSummaries(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
if (jobExecution == null) {
throw new NoSuchJobExecutionException(String.format("No job execution with id=%d", executionId));
}
JobExecution jobExecution = findExecutionById(executionId);
Map<Long, String> map = new LinkedHashMap<Long, String>();
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
map.put(stepExecution.getId(), stepExecution.toString());
@@ -232,10 +228,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
* .Long)
*/
public String getSummary(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
if (jobExecution == null) {
throw new NoSuchJobExecutionException(String.format("No job execution with id=%d", executionId));
}
JobExecution jobExecution = findExecutionById(executionId);
return jobExecution.toString();
}
@@ -250,10 +243,7 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
logger.info("Checking status of job execution with id=" + executionId);
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
if (jobExecution == null) {
throw new NoSuchJobExecutionException(String.format("No job execution with id=%d", executionId));
}
JobExecution jobExecution = findExecutionById(executionId);
String jobName = jobExecution.getJobInstance().getJobName();
Job job = jobRegistry.getJob(jobName);
@@ -366,20 +356,33 @@ public class SimpleJobOperator implements JobOperator, InitializingBean {
* org.springframework.batch.core.launch.JobOperator#stop(java.lang.Long)
*/
public boolean stop(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
if(jobExecution == null){
throw new NoSuchJobExecutionException("No JobExecution found for id: [" + executionId + "]");
}
//Indicate the execution should be stopped by setting it's status to 'STOPPING'. It is assumed that
//the step implementation will check this status at chunk boundaries.
JobExecution jobExecution = findExecutionById(executionId);
// Indicate the execution should be stopped by setting it's status to
// 'STOPPING'. It is assumed that
// the step implementation will check this status at chunk boundaries.
jobExecution.setStatus(BatchStatus.STOPPING);
jobRepository.update(jobExecution);
// TODO: I'm not sure that we can really know if the execution stopped
return true;
}
public boolean pause(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = findExecutionById(executionId);
jobExecution.setStatus(BatchStatus.PAUSED);
jobRepository.update(jobExecution);
return true;
}
private JobExecution findExecutionById(long executionId) throws NoSuchJobExecutionException {
JobExecution jobExecution = jobExplorer.getJobExecution(executionId);
if (jobExecution == null) {
throw new NoSuchJobExecutionException("No JobExecution found for id: [" + executionId + "]");
}
return jobExecution;
}
}

View File

@@ -390,4 +390,19 @@ public class SimpleJobOperatorTests {
assertEquals(BatchStatus.STOPPING, jobExecution.getStatus());
}
@Test
public void testPause() throws Exception{
JobInstance jobInstance = new JobInstance(123L, jobParameters, job.getName());
JobExecution jobExecution = new JobExecution(jobInstance, 111L);
jobExplorer.getJobExecution(111L);
expectLastCall().andReturn(jobExecution);
jobRepository.update(jobExecution);
replay(jobExplorer);
replay(jobRepository);
jobOperator.pause(111L);
verify(jobExplorer);
verify(jobRepository);
assertEquals(BatchStatus.PAUSED, jobExecution.getStatus());
}
}