4 mai 2015

Spring Boot your Java batch

What I hate with batch development is the packaging phase.
Where Java EE normalizes everything, batches have been totally forgotten.

As often, Spring comes with a solution and makes our life easier with Spring Boot.

A revolution.

It's "magic".

1. Your batch

Develop your batch, as usual. Using Spring Batch framework is 'not' a requirement. It simply eases the integration and the configuration after packaging.
I personnally rely on maven. Just add the following lines in you pom.xml

 org.springframework.boot
 spring-boot-starter-batch
 1.2.3.RELEASE
 
  
   org.springframework.boot
   spring-boot-starter-logging
  
 



 
  
   org.springframework.boot
   spring-boot-maven-plugin
   1.2.3.RELEASE
   
    
     
      repackage
     
    
   
  
 

Running mvn package now create 2 jar :
- .jar.original -> the jar before Spring Boot complete it
- .jar -> the runnable fat jar built by Spring Boot

You can run it using the command :
java -jar myfatjar.jar


2. If your batch is developed with Spring

Name your configuration file application.properties.
No need to load it by Spring configuration. This file name is a "keyword".
More details about this "trick" in pitfall section.

3. Write you "main" class

Just write it the following way.
Notice the Spring loading. You can provide a Configuration Bean or a XML file. Depends on your habits.
@SpringBootApplication
public class HelloApp {
    public static void main(String[] args) {
        SpringApplication.run(new Object[]{HelloApp.class, "spring-boot.xml"}, args);
    }
}

4. Run it with external configuration

Externalizing the configuration is not an option for production, it's a requirement.

Run your batch with the following arguments allow you overwrite any configuration present in your application.properties.

... --spring.config.location=file:conf/file1.properties,file:conf/file2.conf ...

5. And external logging

To externalize the logging (log4j.xml file in this example), this requires a bit of additionnal configuration.
As you may have noticed, spring-boot-starter-logging has been excluded from the pom.xml.
The reason is simple : the default transitive dependency run with slf4j, whereas I work with log4j.

Add this dependency instead :

...

 org.springframework.boot
 spring-boot-starter-log4j
 1.2.3.RELEASE

...

Reference the log4j file as an argument on batch startup command line.

... --logging.config=file:conf/log4j.xml 

6. Final packaging and runtime configuration

Finally, we end up with the following run command line (Windows style) :

cd %~dp0
java -jar myfatjar.jar --spring.config.location=file:conf/file1.properties,file:conf/file2.conf --logging.config=file:conf/log4j.xml


And the following production file structure :

7. Pitfall

I had some issues with overriding property file loaded in my Spring configuration.
The internal jar configuration took precedence over configuration provided as arguments during launch.
I don't think this loading order is logical, I may have missed something... I fell back naming my configuration file application.properties

1 commentaire:

Fourni par Blogger.