java - Spring Batch Item Writer 没有被调用

标签 java spring spring-batch itemwriter

我根据教程创建了一个简单的 spring 批处理,它使用 itemreader 和 item writer 读取文件并加载到数据库中。我遵循了相同的步骤,但只有项目读取器被调用,项目编写器没有被调用。谁能帮帮我吗。谢谢!!!

<?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 文件

    <?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>

项目作家

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 ");
        }
    }

}

最佳答案

我已经找到问题所在了。我在 FieldSetMapper(ORM 的域映射器) 类中返回 null,因此没有任何内容可写入数据库。这就是项目编写器没有运行的原因。这是我的错误!!!

关于java - Spring Batch Item Writer 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61700994/

相关文章:

java - 如何在 Eclipse 中使用 Hibernate Tools 生成 DAO?

java - 如何列出实现给定接口(interface)集的类路径上的所有类

java - JMX - Pivotal Cloud Foundry

java - Spring Boot - 我的单元测试被跳过

spring-batch - Spring Batch 和 Pivotal Cloud Foundry

java - 如何在Java中执行2个包含相同条件的While循环?

java - Apache Nifi 与 kafka SASL_PLAINTEXT 的连接问题

java - 我在 IntelliJ IDE 上出现错误,但在 Eclipse 上正常工作 - java.lang.ClassNotFoundException : com. sun.faces.config.ConfigureListener

java - Spring 批处理 : Use of ExcecutionContext to pass step attributes

java - 业务逻辑数据无法使用 Spring Batch(和 Spring-Admin)持久保存