Spring Batch - Stopping a Job
In this example, we'll show you how to stop the batch job.
JobConfiguration.java
@Configuration
public class JobConfiguration extends DefaultBatchConfigurer implements ApplicationContextAware {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobExplorer jobExplorer;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobRegistry jobRegistry;
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
@Bean
public JobRegistryBeanPostProcessor jobRegistrar() throws Exception {
JobRegistryBeanPostProcessor beanPostProcessor = new JobRegistryBeanPostProcessor();
beanPostProcessor.setJobRegistry(jobRegistry);
beanPostProcessor.setBeanFactory(this.applicationContext.getAutowireCapableBeanFactory());
beanPostProcessor.afterPropertiesSet();
return beanPostProcessor;
}
@Bean
public JobOperator jobOperator() throws Exception {
SimpleJobOperator simpleJobOperator = new SimpleJobOperator();
simpleJobOperator.setJobExplorer(jobExplorer);
simpleJobOperator.setJobLauncher(jobLauncher);
simpleJobOperator.setJobParametersConverter(new DefaultJobParametersConverter());
simpleJobOperator.setJobRepository(jobRepository);
simpleJobOperator.setJobRegistry(jobRegistry);
simpleJobOperator.afterPropertiesSet();
return simpleJobOperator;
}
@Bean
@StepScope
public Tasklet tasklet(@Value("#{jobParameters['name']}") String name) {
System.out.println("NAME VALUE = "+name);
return (contribution, chunkContext) -> {
System.out.println(String.format(">>> %s is sleeping again........", name));
Thread.sleep(1000);
return RepeatStatus.CONTINUABLE;
};
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet(tasklet(null))
.build())
.build();
}
@Override
public JobLauncher getJobLauncher() {
SimpleJobLauncher jobLauncher = null;
try {
jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
} catch (Exception e) {
e.printStackTrace();
}
return jobLauncher;
}
}
In this example, we'll show you how to stop the batch job.
JobConfiguration.java
@Configuration
public class JobConfiguration extends DefaultBatchConfigurer implements ApplicationContextAware {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private JobExplorer jobExplorer;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobRegistry jobRegistry;
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
@Bean
public JobRegistryBeanPostProcessor jobRegistrar() throws Exception {
JobRegistryBeanPostProcessor beanPostProcessor = new JobRegistryBeanPostProcessor();
beanPostProcessor.setJobRegistry(jobRegistry);
beanPostProcessor.setBeanFactory(this.applicationContext.getAutowireCapableBeanFactory());
beanPostProcessor.afterPropertiesSet();
return beanPostProcessor;
}
@Bean
public JobOperator jobOperator() throws Exception {
SimpleJobOperator simpleJobOperator = new SimpleJobOperator();
simpleJobOperator.setJobExplorer(jobExplorer);
simpleJobOperator.setJobLauncher(jobLauncher);
simpleJobOperator.setJobParametersConverter(new DefaultJobParametersConverter());
simpleJobOperator.setJobRepository(jobRepository);
simpleJobOperator.setJobRegistry(jobRegistry);
simpleJobOperator.afterPropertiesSet();
return simpleJobOperator;
}
@Bean
@StepScope
public Tasklet tasklet(@Value("#{jobParameters['name']}") String name) {
System.out.println("NAME VALUE = "+name);
return (contribution, chunkContext) -> {
System.out.println(String.format(">>> %s is sleeping again........", name));
Thread.sleep(1000);
return RepeatStatus.CONTINUABLE;
};
}
@Bean
public Job job() {
return jobBuilderFactory.get("job")
.start(stepBuilderFactory.get("step1")
.tasklet(tasklet(null))
.build())
.build();
}
@Override
public JobLauncher getJobLauncher() {
SimpleJobLauncher jobLauncher = null;
try {
jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
jobLauncher.afterPropertiesSet();
} catch (Exception e) {
e.printStackTrace();
}
return jobLauncher;
}
}
JobLaunchingController.java
@RestController
public class JobLaunchingController {
@Autowired
private JobOperator jobOperator;
@PostMapping("/")
@ResponseStatus(HttpStatus.ACCEPTED)
public long launch(@RequestParam("name") String name) throws Exception {
return this.jobOperator.start("job", String.format("name=%s", name));
}
@DeleteMapping(value = "/{id}")
@ResponseStatus(HttpStatus.OK)
public void stop(@PathVariable("id") long id) throws Exception {
this.jobOperator.stop(id);
}
}
StoppingAJobApplication.java
@SpringBootApplication
@EnableBatchProcessing
public class StoppingAJobApplication {
public static void main(String[] args) {
SpringApplication.run(StoppingAJobApplication.class, args);
}
}
When you start the batch job using curl --data "name=foo" localhost:8080. You can see below logs and when you hit curl -X DELETE localhost:8080/1, batch job stops.
2018-12-22 12:14:00.173 INFO 5336 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 4 ms
2018-12-22 12:14:00.193 WARN 5336 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'name' is not present]
2018-12-22 12:14:31.695 INFO 5336 --- [nio-8080-exec-2] o.s.b.c.l.support.SimpleJobOperator : Checking status of job with name=job
2018-12-22 12:14:31.711 INFO 5336 --- [nio-8080-exec-2] o.s.b.c.l.support.SimpleJobOperator : Attempting to launch job with name=job and parameters=name=foo
2018-12-22 12:14:31.725 INFO 5336 --- [cTaskExecutor-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] launched with the following parameters: [{name=foo}]
2018-12-22 12:14:31.740 INFO 5336 --- [cTaskExecutor-1] o.s.batch.core.job.SimpleStepHandler : Executing step: [step1]
NAME VALUE = foo
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
>>> foo is sleeping again........
2018-12-22 12:14:43.782 INFO 5336 --- [cTaskExecutor-1] o.s.b.c.r.support.SimpleJobRepository : Parent JobExecution is stopped, so passing message on to StepExecution
2018-12-22 12:14:43.782 INFO 5336 --- [cTaskExecutor-1] o.s.b.c.s.ThreadStepInterruptionPolicy : Step interrupted through StepExecution
2018-12-22 12:14:43.783 INFO 5336 --- [cTaskExecutor-1] o.s.batch.core.step.AbstractStep : Encountered interruption executing step step1 in job job : Job interrupted status detected.
2018-12-22 12:14:43.784 INFO 5336 --- [cTaskExecutor-1] o.s.b.c.r.support.SimpleJobRepository : Parent JobExecution is stopped, so passing message on to StepExecution
2018-12-22 12:14:43.785 INFO 5336 --- [cTaskExecutor-1] o.s.batch.core.job.AbstractJob : Encountered interruption executing job: Job interrupted by step execution
2018-12-22 12:14:43.786 INFO 5336 --- [cTaskExecutor-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=job]] completed with the following parameters: [{name=foo}] and the following status: [STOPPED]
Comments
Post a Comment