adesai
adesai

Reputation: 420

Run Spring Batch (JSR-352) application on Spring Boot

I have a simple Spring Batch application complying with JSR-352.

I need to deploy this as a managed Task on Spring Cloud Data Flow server. As far as I know - to be able to deploy this as a Task I need to convert this application as a Spring Boot app.

I have tried to add Spring Boot dependencies and Main class however it is not running the Batch job when I start the app.

Main Class

@SpringBootConfiguration
@EnableAutoConfiguration
@EnableBatchProcessing
public class Application {

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

}

Batch File created at

META-INF/batch-jobs/myjob.xml

It works when I use JobOperator in the main class to start the job (without Spring Boot).

What am I missing to run this as a Spring Boot app?

Upvotes: 0

Views: 463

Answers (2)

Dominic
Dominic

Reputation: 193

@EnableBatchProcessing
@SpringBootApplication
public class BatchApplication {

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

    @Bean
    public CommandLineRunner run(JobOperator jobOperator) {
        return $ -> jobOperator.start("myjob", new Properties());
    }

    @Bean
    JobParametersConverter jobParametersConverter(DataSource dataSource) {
        return new JsrJobParametersConverter(dataSource);
    }

    @Bean
    JobOperator jsrJobOperator(ApplicationContext applicationContext, JobExplorer jobExplorer,
                               JobRepository jobRepository, JobParametersConverter jobParametersConverter,
                               PlatformTransactionManager transactionManager) {

        JsrJobOperator jobOperator = new JsrJobOperator(jobExplorer, jobRepository, jobParametersConverter,
                                                        transactionManager);
        jobOperator.setApplicationContext(applicationContext);
        jobOperator.setTaskExecutor(new SimpleAsyncTaskExecutor());

        return jobOperator;
    }

}

https://gist.github.com/rixwwd/8091a717ca24fd810ff71b4fdebbf9cc

Upvotes: 0

Sabby Anandan
Sabby Anandan

Reputation: 5651

You're missing @EnableTask annotation. With that, your batch-job will be run as a short-lived application. In other words, the application will run as long as the business logic in your XML needs to run, and it will gracefully shut down and free-up resources.

Please clone and try out the Spring Cloud Task samples [see: BatchJobApplication]. All of them should work as-is in SCDF as well.

Upvotes: 1

Related Questions