Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Spring Batch jobs from the command line

I don't know how to call a Job defined in Spring Batch using CommandLineJobRunner, documentation details are not enough for me.

I've followed the Spring Batch official guide to write Jobs in Spring Batch using Java annotations e.g. @EnableBatchProcessing because I wanted to avoid XML configuration files for the description of the job, the steps, etc.

So far I have:

  • a configuration class (com.package.bla.bla.ClassContainingTheBatchConfiguration see below) where I've put all the stuff defining ItemReader, ItemProcessor, ItemWriter, Job, and Step (with return jobs.get("nameOfTheJob") see below) using a @Bean annotaion.
  • a class with a main method with SpringApplication.run(...) and and annotation with @ImportResource("classpath:META-INF/spring/applicationContext.xml") to import some beans I need when processing the data in the Job.

On the Maven side I am currently using some plugins:

  • maven-jar-plugin specifying <addClasspath>true</addClasspath> and the class containing the main method in the tag <mainClass>
  • maven-assembly-plugin because I would like a unique executable jar containing all the stuff in the dependencies, I am using <phase>package</package> to be able to build the jar in the package phase, I am also using <goal>single</goal> to be able to properly build the jar using the assembly
  • maven-compiler-plugin specifying I am using Java 1.7

I think I've configured all the things I need to configure, however after having a Maven BUILD SUCCESS I am not able to run the job from the command line:

java -cp ./target/JAR_FILE_NAME.jar org.springframework.batch.core.launch.support.CommandLineJobRunner com.package.bla.bla.ClassContainingTheBatchConfiguration nameOfTheJob

Is throwing IOException due to the java.io.FileNotFoundException regarding com.package.bla.bla.ClassContainingTheBatchConfiguration. How should I specify the parameters in the command line in order to get the Job executed?

like image 351
TPPZ Avatar asked Feb 24 '14 16:02

TPPZ


2 Answers

If you are already using SpringApplication from Spring Boot, why not finish the job and use @EnableAutoConfiguration as well, and also the Maven plugin (see for example this guide)? That way you will get something working pretty quickly and you can always add your own features later.

like image 146
Dave Syer Avatar answered Oct 14 '22 09:10

Dave Syer


If the first argument to the CommandLineJobRunner is your @Configuration FQCN instead of a resource path, the ClassPathXmlApplicationContext constructor that's called from the CommandLineJobRunner's start() method will break.

int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) {

    ConfigurableApplicationContext context = null;

    try {
        context = new ClassPathXmlApplicationContext(jobPath);

If you've already written a class with a main(), that replaces the CLJR, you shouldn't be passing CLJR as the class name in the command line. Pass that instead.

like image 39
Emerson Farrugia Avatar answered Oct 14 '22 09:10

Emerson Farrugia