Step에 @JobScope 걸어두면 stop할때 No context holder available for job scope 에러가 발생합니다.
BatchConfig.java @Configuration @RequiredArgsConstructor public class BatchConfig { private final JobRegistry jobRegistry; @Bean public BeanPostProcessor JobRegistryBeanPostProcessor() { JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor(); jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry); return jobRegistryBeanPostProcessor; } } MyJobConfig.java @Configuration @RequiredArgsConstructor public class MyJobConfig { private final JobBuilderFactory jobBuilderFactory; private final StepBuilderFactory stepBuilderFactory; @Bean public Job myJob() { return jobBuilderFactory.get("myJob") .start(myStep1()) .next(myStep2()) .build(); } @Bean @JobScope public Step myStep1() { return stepBuilderFactory.get("myStep1") .tasklet((contribution, context) -> { System.out.println("myStep1 start"); Thread.sleep(10000); System.out.println("myStep1 end"); return RepeatStatus.FINISHED; }) .build(); } @Bean @JobScope public Step myStep2() { return stepBuilderFactory.get("myStep2") .tasklet((contribution, context) -> { System.out.println("myStep2 start"); Thread.sleep(10000); System.out.println("myStep2 end"); return RepeatStatus.FINISHED; }) .build(); } } BatchController.java @RestController @RequiredArgsConstructor @RequestMapping("/batch") public class BatchController { private final JobOperator jobOperator; private final JobRegistry jobRegistry; private final JobExplorer jobExplorer; @GetMapping("/start") public String jobStart(String data) throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException { for(String jobName : jobRegistry.getJobNames()) { jobOperator.start(jobName, "data="+data); } return "SUCCESS"; } @GetMapping("/restart") public String jobRestart(String data) throws JobInstanceAlreadyCompleteException, NoSuchJobExecutionException, NoSuchJobException, JobRestartException, JobParametersInvalidException { for(String jobName : jobRegistry.getJobNames()) { Long executionId = jobExplorer.getLastJobExecution(jobExplorer.getLastJobInstance(jobName)).getId(); jobOperator.restart(executionId); } return "SUCCESS"; } @GetMapping("/stop") public String jobStop() throws NoSuchJobExecutionException, JobExecutionNotRunningException { for(String jobName : jobRegistry.getJobNames()) { for(JobExecution jobExecution : jobExplorer.findRunningJobExecutions(jobName)) { jobOperator.stop(jobExecution.getId()); } } return "SUCCESS"; } } 위 코드에서 http://localhost:8080/batch/start?data=data로 job실행하고, 끝나기전에 http://localhost:8080/batch/stop 호출하면 "java.lang.IllegalStateException: No context holder available for job scope" 에러 발생합니다.