private boolean block = true;
@Test
public void testRunningJobExecution() throws Exception {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
MapJobRepositoryFactoryBean repositoryFactory = new MapJobRepositoryFactoryBean();
repositoryFactory.afterPropertiesSet();
JobRepository jobRepository = repositoryFactory.getObject();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
SimpleJob job = new SimpleJob("job");
TaskletStep step = new TaskletStep("step");
step.setTasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
while (block) {
Thread.sleep(100L);
}
return RepeatStatus.FINISHED;
}
});
step.setTransactionManager(repositoryFactory.getTransactionManager());
step.setJobRepository(jobRepository);
step.afterPropertiesSet();
job.addStep(step);
job.setJobRepository(jobRepository);
job.afterPropertiesSet();
jobLauncher.run(job, new JobParametersBuilder().addString("test", getClass().getName()).toJobParameters());
Thread.sleep(500L);
JobExplorer explorer = new MapJobExplorerFactoryBean(repositoryFactory).getObject();
Set<JobExecution> executions = explorer.findRunningJobExecutions("job");
assertEquals(1, executions.size());