Spring Batch run by Spring Batch Admin

By | March 31, 2015
Share the joy
  •  
  •  
  •  
  •  
  •  
  •  

The second way to run Spring Batch job is by Spring Batch Admin. It is more powerful. By SBA, we can execute/stop/monitor our batch job in web UI. I downloaded the SBA sample code from Spring Batch website and made it more simple version.

I used the mysql to store the metadata. The metadata tables can be created by several ways. We can create it in normal spring batch job by adding below code in the config xml.

<jdbc:initialize-database data-source="dataSource">
      <jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
      <jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
</jdbc:initialize-database>

Or we can manually copy the sql and run it from spring-batch-core.jar: /org/springframework/batch/core/schema-mysql.sql

Once we have meta tables in database, let me explain the following configuration.

batch-mysql.properties

# Placeholders batch.*
#    for MySQL:
batch.jdbc.driver=com.mysql.jdbc.Driver  //In the project, remember we should add this dependency in maven
batch.jdbc.url=jdbc:mysql://localhost:3306/spring_batch
batch.jdbc.user=username
batch.jdbc.password=password
batch.jdbc.testWhileIdle=true
batch.jdbc.validationQuery=SELECT 1    //test the database connection
batch.schema.script=classpath*:/org/springframework/batch/core/schema-mysql.sql  //to build the meta tables
batch.drop.script=classpath*:/org/springframework/batch/core/schema-drop-mysql.sql  //to swipte the meta tables
batch.business.schema.script=classpath:/business-schema-mysql.sql
batch.database.incrementer.class=org.springframework.jdbc.support.incrementer.MySQLMaxValueIncrementer

# Non-platform dependent settings that you might like to change
batch.data.source.init=false  //Always set false, or it will swipe and rebuild the meta tables

infinite-context.xml
The configuration file is different from normal one. In the config xml in SBA, we should delete the jobRepository, transacionManager and jobLauncher. We only define the job itself. Pay attention to the namespace version, spring-batch-admin and spring version. Sometimes, it may not correct! According to my experience, spring-batch-admin 1.2.1, spring3.0.5 are together good ones.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:file="http://www.springframework.org/schema/integration/file"
      xmlns:integration="http://www.springframework.org/schema/integration" xmlns:p="http://www.springframework.org/schema/p"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
      http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-1.0.xsd">

<job id="infinite" xmlns="http://www.springframework.org/schema/batch">
   <step id="step1" next="step1">
      <tasklet start-limit="100">
         <chunk commit-interval="1" reader="itemReader" writer="itemWriter" />
      </tasklet>
   </step>
</job>

<bean id="itemWriter" class="com.wfs.springbatch.springbatchadmin.helloworld.ExampleItemWriter"/>

<bean id="itemReader" class="com.wfs.springbatch.springbatchadmin.helloworld.ExampleItemReader" scope="step"/>

</beans>

In my case, I use mysql. So before run the batch job, we should set -DENVIRONMENT=mysql parameter for the JBoss or Tomcat server. If you use other databases, you should replace the batch-mysql.properties with batch-[other-databse-name].properties

My ppt summary for Spring Batch Admin.




source code: link