Search code examples
spring-bootspring-batch

spring-batch: are commandlinejobrunner and spring-boot compatible? (version 4.3.2)


I'm looking at CommandLineJobRunner under spring-batch version 4.3.2.

The start() method of CommandLineJobRunner instantiates an ApplicationContext.

Doesn't this mean that CommandLineJobRunner is not compatible with a SpringBootApplication which itself creates an ApplicationContext?

Thanks for any help on this question.


Solution

  • I think the short answer is, "No, but if you are using boot, you don't need to use the Spring Batch CommandLineJobRunner."

    This is because Spring Boot has it's own CommandLineRunner interface, that the main boot application class can implement. This will require you to override the run() method in the interface, and you can use this a your entry point for your Spring Batch job launcher. See, for example, https://www.javainuse.com/spring/batch/jobparam

    Do make note of their warning:

    Also by default, Spring Boot executes any job in out (sic) application context at startup. So our tasklet will be executed twice: once at application startup and once when we invoke the API using the JobLauncher. On application startup when the tasklet is involved the job parameters are null and so our application will throw a null pointer exception. We will be disabling running jobs at startup using following property: spring.batch.job.enabled=false

    Another common use for the JobLauncher is from within the context of a restful endpoint. Create a rest controller method that uses the job launcher. Some benefits of this approach are that you can launch a job using a HTTP request, and you can also enforce security policies on the endpoint using Spring security.