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:

@SpringBootApplication
public class ApplicationLauncher {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationLauncher.class, args);
    }
}

After that we modify our BatchConfiguration.

@EnableBatchProcessing
@Configuration
class BatchConfiguration {

    @Bean
    public JobLauncher jobLauncher() {
        final SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        final SimpleAsyncTaskExecutor simpleAsyncTaskExecutor = new SimpleAsyncTaskExecutor();
        jobLauncher.setTaskExecutor(simpleAsyncTaskExecutor);
        return jobLauncher;
    }

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

    @Autowired
    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 application.properties:

spring.batch.job.enabled=false

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

@RestController
@AllArgsConstructor
public class StartJobController {

    private final JobLauncher jobLauncher;

    private final Job ourJob;

    @RequestMapping("/invokejob")
    public String handle() throws Exception {

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

        jobLauncher.run(ourJob, 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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s