BatchStartAndRecoveryServiceImpl implements BatchStartAndRecoveryService, JobExplorer { @Override public JobExecution restart(Long jobInstanceId) throws JobExecutionAlreadyRunningException, NoSuchJobExecutionException, NoSuchJobException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { JobInstance jobInstance = jobInstanceDao.getJobInstance(jobInstanceId); if (jobInstance == null) { throw new NoSuchJobException(String.format( "The job with jobInstanceId=%d cannot be restarted because it has not be started yet", jobInstanceId )); } JobExecution jobExecution = jobExecutionDao.getLastJobExecution(jobInstance); if (jobExecution == null) { throw new NoSuchJobExecutionException(String.format( "No job execution belongs to the jobName=%s - jobInstanceId=%d", jobInstance.getJobName(), jobInstance.getInstanceId() )); } BatchStatus status = jobExecution.getStatus(); if (!(status == BatchStatus.FAILED || status == BatchStatus.STOPPED)) { throw new JobRestartException(String.format( "Cannot restart that job : jobName=%s - jobInstanceId=%d! Because it's neither STOPPED or FAILED!", jobInstance.getJobName(), jobInstance.getInstanceId() )); } LOGGER.info("Attempting to resume job instance with id={}", jobInstanceId); return restart(jobInstance.getJobName(), jobExecution.getJobParameters()); } BatchStartAndRecoveryServiceImpl(JobInstanceDao jobInstanceDao,
                                            JobExecutionDao jobExecutionDao,
                                            StepExecutionDao stepExecutionDao,
                                            ExecutionContextDao executionContextDao
    ); @Override List<JobExecution> stopAllRunningJobs(); @Override List<JobExecution> markInconsistentJobsAsFailed(); Set<Long> getRunningExecutions(String jobName); @Override List<JobExecution> restartAllFailedJobs(); @Override List<JobExecution> restartAllStoppedJobs(); List<JobExecution> restartJobs(String jobName, BatchStatus status); @Override BatchStatus getStatus(Long jobInstanceId); List<JobInstance> getJobInstances(String jobName, int start, int count); @Override JobExecution start(String jobName, JobParameters jobParameters); JobExecution start(String jobName, JobParameters jobParameters, boolean newInstance); @Override JobExecution restart(Long jobInstanceId); JobExecution restart(String jobName, JobParameters parameters); JobExecution findExecutionById(Long jobExecutionId); @Override JobExecution startNewInstance(String jobName, JobParameters jobParameters); @Override JobExecution stop(Long jobExecutionId); @Override List<String> getJobNames(); @Override List<JobInstance> findJobInstancesByJobName(String jobName, int start, int count); @Override int getJobInstanceCount(String jobName); @Override JobExecution refresh(JobExecution jobExecution); @Override JobExecution abandon(Long jobExecutionId); @Override JobExecution getJobExecution(Long executionId); @Override StepExecution getStepExecution(Long jobExecutionId, Long stepExecutionId); @Override JobInstance getJobInstance(Long instanceId); @Override List<JobExecution> getJobExecutions(JobInstance jobInstance); @Override Set<JobExecution> findRunningJobExecutions(String jobName); void updateJobExecution(JobExecution jobExecution); void updateStepExecution(StepExecution stepExecution); void setJobRegistry(JobRegistry jobRegistry); void setJobLauncher(JobLauncher jobLauncher); void setJobInstanceDao(JobInstanceDao jobInstanceDao); void setJobExecutionDao(JobExecutionDao jobExecutionDao); void setStepExecutionDao(StepExecutionDao stepExecutionDao); void setExecutionContextDao(ExecutionContextDao executionContextDao);  }
@Test(expected = NoSuchJobException.class) public void testRestartNoSuchJobException() throws Exception { service.restart(INSTANCE_ID); }
@Test(expected = NoSuchJobExecutionException.class) public void testRestartNoSuchJobExecutionException() throws Exception { expectGetJobInstance(); replayAll(); service.restart(INSTANCE_ID); }
@Test(expected = JobRestartException.class) public void testRestartJobRestartException() throws Exception { jobExecution.setStatus(BatchStatus.COMPLETED); expectGetJobInstance(); expectGetLastJobExecution(); replayAll(); service.restart(INSTANCE_ID); }