new ClassPathXmlApplicationContext("org/springframework/batch/core/repository/dao/OptimisticLockingFailureTests-context.xml");
Job job = applicationContext.getBean(Job.class);
JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
JobOperator jobOperator = applicationContext.getBean(JobOperator.class);
JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder()
.addLong("test", 1L)
.toJobParameters());
Thread.sleep(1000);
jobOperator.stop(jobExecution.getId());
while(jobExecution.isRunning()) {
// wait for async launched job to complete execution
}
int numStepExecutions = jobExecution.getStepExecutions().size();
StepExecution stepExecution = jobExecution.getStepExecutions().iterator().next();
String stepName = stepExecution.getStepName();
BatchStatus stepExecutionStatus = stepExecution.getStatus();
BatchStatus jobExecutionStatus = jobExecution.getStatus();
assertTrue("Should only be one StepExecution but got: " + numStepExecutions, numStepExecutions == 1);
assertTrue("Step name for execution should be step1 but got: " + stepName, "step1".equals(stepName));
assertTrue("Step execution status should be STOPPED but got: " + stepExecutionStatus, stepExecutionStatus.equals(BatchStatus.STOPPED));
assertTrue("Job execution status should be STOPPED but got:" + jobExecutionStatus, jobExecutionStatus.equals(BatchStatus.STOPPED));
JobExecution restartJobExecution = jobLauncher.run(job, new JobParametersBuilder()
.addLong("test", 1L)
.toJobParameters());
while(restartJobExecution.isRunning()) {
// wait for async launched job to complete execution