eHarmony Engineering logo

Spring Batch Admin – The Tricky Parts

June 26, 2014

In the Matching Engineering team at eHarmony, our projects use the Spring Batch and Spring Data frameworks extensively. As the frameworks became more mature, we realized that a GUI control mechanism would be useful.

The new GUI needed to support features like monitoring executing runs, starting and stopping jobs, and using a well-defined API remotely.

After a bit of investigation, we decided on Spring Batch Admin, a framework implemented on top of Spring Batch. It contained all the features we needed: a GUI for monitoring, manual triggering and recovery of the jobs, and a RESTful API to trigger jobs.

At first glance everything was amazing.  However, when we ran a test setup of the stand alone Spring Batch Admin web application, issues arose when trying to integrate it with the existing application.

This post presents some tricky things we had to do to make it work with our existing setup.

Our project is written in Java/Groovy and uses the Maven project object model.

This post assumes that you have already visited the Spring Batch Admin website (http://static.springsource.org/spring-batch-admin/) and are trying to integrate it into your existing project.

First of all, to use Spring Batch Admin in your project, you need to declare the following dependency in the pom.xml file.

    org.springframework.batch
    spring-batch-admin-manager
    1.2.1.RELEASE

Here come the tricky parts!

In order to incorporate and override the spring configuration from Spring Batch Admin, the following exact structure needs to be specified.

In the src/main/resources, create a directory structure with these files:

|____META-INF
| |____spring
| | |____batch
| | | |____jobs
| | | | |____spring-batch-alias.xml
| | | |____override
| | | | |____manager
| | | | | |____env-context.xml

Please note that Spring Batch Admin loads application context only if it is in that specific directory structure, however instead of putting all my application-specific items in that directory, I prefer an alias spring-batch-alias.xml file to the actual context file.


    <!-- Instead of copying all the configuration files to directory, it is much cleaner  -->
    <!-- to create this alias config file. Spring-batch-admin-manager will scan this directory -->
    <!-- structure for loading the job definitions. META-INF/spring/batch/jobs/ -->

Please note that namespace URLs have been truncated for better readability.
Below is the content of the env-context.xml file, which allows you to load and use additional property files.


    <!--  Use this to set additional properties on beans at run time -->

                classpath:batch-${ENVIRONMENT:hsql}.properties
                classpath:*.properties
                classpath:spring-batch-admin/*.properties

Note that this property is not required, however I left it there for compatibility purposes (it is in all examples of Spring Batch Admin and is useful if you have different database configurations and want to specify which one to use for storing metadata externally):

classpath:batch-${ENVIRONMENT:hsql}.properties

In my case, there is only a default property file spring-batch-admin/batch-default.properties, so besides the META-INF directory, I also have the following files:

|____spring-batch-admin
| |____batch-default.properties
| |____business-schema.sql

batch-default.properties

# Default placeholders for database platform independent features
batch.remote.base.url=http://localhost:8092/app_name

# GENERAL:
batch.jdbc.testWhileIdle=true
batch.business.schema.script=classpath:/spring-batch-admin/business-schema.sql
# Set to False to stop the database from being wiped and re-created.
batch.data.source.init=false

# MySQL:
#batch.jdbc.driver=com.mysql.jdbc.Driver
#batch.jdbc.url=jdbc:mysql://localhost/db_name
#batch.jdbc.user=USERNAME
#batch.jdbc.password=*******
#batch.jdbc.validationQuery=SELECT 1
#batch.schema.script=classpath:/org/springframework/batch/core/schema-mysql.sql
#batch.drop.script=classpath*:/org/springframework/batch/core/schema-drop-mysql.sql
#batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer

# Oracle:
batch.jdbc.driver=oracle.jdbc.driver.OracleDriver
batch.jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
batch.jdbc.user=USERNAME
batch.jdbc.password=*******
batch.jdbc.validationQuery=
batch.schema.script=classpath:/org/springframework/batch/core/schema-oracle10g.sql
batch.drop.script=classpath*:/org/springframework/batch/core/schema-drop-oracle10g.sql
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer
batch.database.incrementer.parent=sequenceIncrementerParent
batch.verify.cursor.position=true

business-schema.sql

Note that it is an empty file, however I had to add it — the Spring Batch Admin requires that file to exist. I’m not sure whether they have already fixed that issue.

I have included comments in the files wherever possible, to make it clear why certain properties have specified values.

Those are the tricky things I had to do to incorporate Spring Batch Admin into our project. Hope this post saves you some trouble when trying to configure it for yours.