Launch a Spring Batch Job Anywhere

Today we are going to launch a Spring Batch job with an HTTP request. For this we will need a batch job. Like the one we made yesterday and we will continue to use here. But any Spring Batch job will suffice.

We will need the Batch job and a dependency on spring-boot-starter-web.

First we create a class to launch our application:

public class ApplicationLauncher {

    public static void main(String[] args) {, args);

After that we modify our BatchConfiguration.

class BatchConfiguration {

    public JobLauncher jobLauncher() {
        final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
        return jobLauncher;

    private final JobRepository jobRepository;
    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;
    private final DataSource dataSource;

    BatchConfiguration(JobRepository jobRepository, StepBuilderFactory stepBuilderFactory, JobBuilderFactory jobBuilderFactory,
                       @Qualifier("dataSource") DataSource dataSource) {
        this.jobRepository = jobRepository;
        this.stepBuilderFactory = stepBuilderFactory;
        this.jobBuilderFactory = jobBuilderFactory;
        this.dataSource = dataSource;

We remove the main method, because that’s now in our ApplicationLauncher. We change @SpringBootApplication to @Configuration, because this functionality happens in our ApplicationLauncher now.

We add private final JobRepository jobRepository which has been provided by @EnableBatchProcessing. we add an Qualifier for the datasource, because Spring MVC creates default datasources. This way we explicitly tell Spring Boot which datasource to use.

Finally we create a new JobLauncher which uses the jobRepository and give it an asyc taskexecutor. This is because otherwise our controller will wait for the Job to be completed to return our message.

To start using the batch job with a controller we need to disable the default configuration of running a Job on application startup. We do this by adding the following line to


The last thing we need is a controller to start the Job.

public class StartJobController {

    private final JobLauncher jobLauncher;

    private final Job ourJob;

    public String handle() throws Exception {

        JobParameters jobParameters = new JobParametersBuilder().toJobParameters();, jobParameters);

        return "Batch job has been invoked";
  • @RestController makes the class a restcontroller so we can use it in SPA’s (single page applications) if we want.
  • @AllArgsConstructor annotation from Lombok to create constructor with all class attributes.
  • JobLaucher starts the Job. It requires a Job and JobParameters, which may be empty as in this case.

When we start our application and go to the URL: http://localhost:8080/invokejob the Job will run, and we see the message appear.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s