22

I am trying to run my batch job from a controller. It will be either fired up by a cron job or by accessing a specific link. I am using Spring Boot, no XML just annotations.

In my current setting I have a service that contains the following beans:

@EnableBatchProcessing
@PersistenceContext
public class batchService {

    @Bean
    public ItemReader<Somemodel> reader() {
        ...
    }

    @Bean
    public ItemProcessor<Somemodel, Somemodel> processor() {
        return new SomemodelProcessor();
    }

    @Bean
    public ItemWriter writer() {
        return new CustomItemWriter();
    }

    @Bean
    public Job importUserJob(JobBuilderFactory jobs, Step step1) {
        return jobs.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .flow(step1)
                .end()
                .build();
    }

    @Bean
    public Step step1(StepBuilderFactory stepBuilderFactory,       
            ItemReader<somemodel> reader,
            ItemWriter<somemodel> writer,
            ItemProcessor<somemodel, somemodel> processor) {

        return stepBuilderFactory.get("step1")
                .<somemodel, somemodel> chunk(100)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }   
}   

As soon as I put the @Configuration annotation on top of my batchService class, job will start as soon as I run the application. It finished successfully, everything is fine. Now I am trying to remove @Configuration annotation and run it whenever I want. Is there a way to fire it from the controller?

Thanks!

1

3 Answers 3

32

You need to create a application.yml file in the src/main/resources and add following configuration:

spring.batch.job.enabled: false

With this change, the batch job will not automatically execute with the start of Spring Boot. And batch job will be triggered when specific link.

Check out my sample code here: https://github.com/pauldeng/aws-elastic-beanstalk-worker-spring-boot-spring-batch-template

1
  • Or @SpringBootApplication(exclude = {BatchAutoConfiguration.class})
    – gavenkoa
    Jan 4, 2020 at 23:34
29

You can launch a batch job programmatically using JobLauncher which can be injected into your controller. See the Spring Batch documentation for more details, including this example controller:

@Controller
public class JobLauncherController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @RequestMapping("/jobLauncher.html")
    public void handle() throws Exception{
        jobLauncher.run(job, new JobParameters());
    }
}
2
  • Ive seen JobLauncher before, yet I dont know how to set it up so that it knows whitch job to run (importUserJob in my case that takes 2 parameters). Do I have to create CustomJobLauncher bean, set it there and then inject my CustomJobLauncher? Currently after just injecting JobLauncher as per your advice I get No qualifying bean of type [org.springframework.batch.core.launch.JobLauncher] found for dependency
    – Damian
    Feb 17, 2015 at 17:42
  • 1
    As long as you have a @Configuration class annotated with @EnableBatchProcessing a JobLauncher bean should be created automatically Feb 17, 2015 at 18:07
4

Since you're using Spring Boot, you should leave the @Configuration annotation in there and instead configure your application.properties to not launch the jobs on startup. You can read more about the autoconfiguration options for running jobs at startup (or not) in the Spring Boot documentation here: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-execute-spring-batch-jobs-on-startup

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.