Google

Dec 18, 2013

Configure Spring batch to persist to a database

Q. Why do you have to persist Spring batch run information to a database?
A. If you write industrial strength large batch jobs, you need to store job runs to a database so that you can monitor the jobs, restart a job, etc.


Step 1: You need to create database tables. You can find the schema inside the spring-batch-core-xxx.jar as shown below.





The tables that gets created are



Step 2: You need to have the relevant dependency jar files in addition to the spring batch jars.

org.springframework.jdbc-3.0.0.RELEASE.jar
com.springsource.com.thoughtworks.xstream-1.3.0.jar
com.springsource.org.codehaus.jettison-1.0.0.jar

commons-dbcp-1.2.2.jar

Step 3: Configure your spring config file

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

 <bean id="jobExplorer"
  class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean"
  p:dataSource-ref="dataSource_oracle" p:tablePrefix="BATCH_" />

 <job-repository id="jobRepository" lob-handler="lobHandler"
  data-source="dataSource_oracle" transaction-manager="transactionManager"
  isolation-level-for-create="READ_COMMITTED" table-prefix="BATCH_"
  xmlns="http://www.springframework.org/schema/batch" />

 <bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler">
  <property name="nativeJdbcExtractor">
   <bean
    class="org.springframework.jdbc.support.nativejdbc.OracleJdbc4NativeJdbcExtractor" />
  </property>
 </bean>
 <bean id="jobLauncher"
  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
  <property name="jobRepository" ref="jobRepository" />
 </bean>
 <bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor">
  <property name="concurrencyLimit" value="3" />
 </bean>
 

</beans>




Search at the top for "Spring batch" or select the "Spring batch" tag cloud for more basic and advanced posts on spring batch.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home