In Listing 20-1, the change made is highlighted in bold. In Spring Batch, the DDL for creating the
metadata structure is stored in the spring-batch-core module, under the package
org.springframework.batch.core. Because we are using H2, we use the schema-h2.sql file. There are also
existing scripts for most commonly used databases.
Second, we need to configure Spring Batch infrastructure components. At a minimum, the job
repository and job launcher beans are required. Listing 20-2 shows the configuration file
(/src/main/resources/batch-context.xml).
Listing 20-2. Spring Batch Configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="jobRepository" class="org.springframework.batch.core.repository
.support.JobRepositoryFactoryBean">
<property name="databaseType" value="h2" />
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="isolationLevelForCreate" value="ISOLATION_DEFAULT"/>
</bean>
<bean id="jobLauncher" class="org.springframework.batch.core.launch
.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
In Listing 20-2, 2 Spring Batch beans are defined. First, the jobRepository bean is defined with the
JobRepositoryFactoryBean class, which will create an instance of the
org.springframework.batch.core.repository.support.SimpleJobRepository class using Spring Batch's
DAO implementations. The SimpleJobRepository class implements the
org.springframework.batch.core.repository.JobRepository interface, which stores JobInstances,
JobExecutions, and StepExecutions using the injected DAOs.
Second, the jobLauncher bean is defined with the SimpleJobLauncher implementation class, which
implements the org.springframework.batch.core.launch.JobLauncher interface. The SimpleJobLauncher
uses Spring Core's TaskExecutor interface for job launching. By default, a synchronized task executor
(the SyncTaskExecutor class) will be instantiated for job launching. The jobRepository bean will be
injected into the jobLauncher bean so that the job execution details can be updated to the underlying
Spring Batch metadata storage.
Implementing the Import Contact Job
Now we can proceed to implement the batch job. Let's define the XML file format first. Listing 20-3 shows
the sample XML file with a contact's information (the contacts.xml file in the project's root folder).
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home