I have created a simple spring batch based on tutorials which reads a file and loads into database using itemreader and item writer. I have followed the same steps but only item reader is getting called, item writer is not getting called. can anyone please help me. Thanks !!!
<?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:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">
<job id="importProducts" xmlns="http://www.springframework.org/schema/batch" >
<step id="decompress" next="readWriteProducts">
<tasklet ref="decompressTasklet" />
</step>
<step id="readWriteProducts">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="3" skip-limit="5">
<skippable-exception-classes>
<include class="org.springframework.batch.item.file.FlatFileParseException" />
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
</job>
<bean id="decompressTasklet" class="com.saimuga.abp.fileupload.tasklet.ReadInputFile" scope="step">
<property name="inputResource" value="#{jobParameters['inputResource']}" />
<property name="targetDirectory" value="#{jobParameters['targetDirectory']}" />
<property name="targetFile" value="#{jobParameters['targetFile']}" />
</bean>
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="file:#{jobParameters['targetDirectory']+jobParameters['targetFile']}" />
<property name="linesToSkip" value="1" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="PRODUCT_ID,NAME,DESCRIPTION,PRICE" />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="com.saimuga.abp.fileupload.domain.mapper.FiletoDomainMapper" />
</property>
</bean>
</property>
</bean>
<bean id="writer" class="com.saimuga.abp.fileupload.writer.ProductJdbcItemWriter" >
<constructor-arg ref="dataSource" />
</bean>
InfraStructure XML File
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-
beans.xsd">
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="create-tables.sql"/>
</jdbc:embedded-database>
<bean class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource" />
</bean>
</beans>
Item Writer
package com.saimuga.abp.fileupload.writer;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.batch.item.ItemWriter;
import org.springframework.jdbc.core.JdbcTemplate;
import com.saimuga.abp.fileupload.domain.Product;
/**
*
*
*/
public class ProductJdbcItemWriter implements ItemWriter<Product> {
private static final String INSERT_PRODUCT = "insert into product (id,name,description,price) values(?,?,?,?)";
private static final String UPDATE_PRODUCT = "update product set name=?, description=?, price=? where id = ?";
private JdbcTemplate jdbcTemplate;
public ProductJdbcItemWriter(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/* (non-Javadoc)
* @see org.springframework.batch.item.ItemWriter#write(java.util.List)
*/
public void write(List<? extends Product> items) throws Exception {
System.out.println("cxf ProductJdbcItemWriter starts ");
for(Product item : items) {
int updated = jdbcTemplate.update(UPDATE_PRODUCT,
item.getName(),item.getDescription(),item.getPrice(),item.getId()
);
if(updated == 0) {
jdbcTemplate.update(
INSERT_PRODUCT,
item.getId(),item.getName(),item.getDescription(),item.getPrice()
);
}
System.out.println("cxf ProductJdbcItemWriter ends ");
}
}
}
1 Answer 1
I have figured out the problem. I was returning null in FieldSetMapper(Domain Mapper for ORM) class so there was nothing to write to DB. Thats why item writer didnt run. Its my mistake !!!
Comments
Explore related questions
See similar questions with these tags.