0

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?

adesai
  • 370
  • 3
  • 22

2 Answers2

1

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.

Sabby Anandan
  • 5,636
  • 2
  • 12
  • 21
  • I am aware of `@EnableTask` and already tried it. The issue is how do Spring Boot application identify the batch XML and run it. Also, I have tried to run the job using `JobOperator` in Config class however SCDF Server does not identify the Task as Batch! – adesai May 28 '19 at 15:27
  • @adesai If your job configuration is defined in a XML file, you need to tell Spring Boot where to find this file and import it with `@ImportResource`, see here: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-importing-xml-configuration. – Mahmoud Ben Hassine May 29 '19 at 07:02
  • @Mahmoud Ben Hassine - I have tried using @ImportResource and it is giving stack trace with `Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'job'. ` Bare in mind this XML file is JSR-352 based batch file not Spring beans XML file. And as I mentioned in my previous comment that it works with `JobOperator` - `BatchRuntime.getJobOperator.start(jobId, null)` but SCDF is not recognising the Task as a batch task. I would like to load this automatically on start up. – adesai May 29 '19 at 09:56
  • 1
    ok that's a different story. JSR 352 job descriptors should be placed under `META-INF/batch-jobs` (See [Oracle docs](https://docs.oracle.com/javaee/7/tutorial/batch-processing007.htm)) and not `META-INF/batch-job`. Note the missing 's' in your folder name. – Mahmoud Ben Hassine May 29 '19 at 13:10
  • @Mahmoud Ben Hassine - apologies for the typo. It has always been batch-jobs. Do you think it is possible to deploy a JSR-352 batch application on SCDF server as a 'Batch Job'? – adesai May 29 '19 at 15:11
  • If you manage to make it work as a Spring Boot app, then it should be possible. – Mahmoud Ben Hassine May 29 '19 at 17:43
0
@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

Dominic
  • 183
  • 2
  • 8