diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java b/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java index 61eda773e..fa3b6ae46 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/BatchStatus.java @@ -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) { diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java index e38f14e11..3d6502ca8 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java @@ -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; diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java index 2126cba5b..6e64656bd 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java @@ -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 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 map = new LinkedHashMap(); 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; + + } + } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java index 96e66cabb..c90d987f3 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/launch/support/SimpleJobOperatorTests.java @@ -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()); + } + }