ItemReader中的reader()进入无限循环

4

我使用JdbcTemplate实现了ItemReader。

问题在于read()被无限循环调用。

public class MyReader implements ItemReader<Col>, InitializingBean {
    private JdbcTemplate jdbcTemplate;
    private RowMapper<Col> rowMapper;
    private String sql;
    private DataSource dataSource;

public Col read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
     Col col = jdbcTemplate.queryForObject(sql,null, rowMapper);            
     return col;
    }

}

Spring Batch配置:

<chunk reader="itemReader" writer="itemWriter"
            processor="itemProcessor" commit-interval="1" />


<bean id="itemReader"
    class="batch.MyReader"
    scope="step">
    <property name="dataSource" ref="dataSource" />
    <property name="sql" value="#{stepExecutionContext[sql]}" />
    <property name="rowMapper">
        <bean class="batch.ColMapper" />
    </property>
</bean>

1
为什么不呢?只要read方法返回内容,它就会被调用,当该方法返回null时,它将结束。另外,为什么不使用默认的ItemReader之一呢?看起来你可以简单地使用其中一个默认值,而不是实现自己的... - M. Deinum
我之前使用的是JdbcCursorItemReader,但在我的情况下,我只需要返回第一行(一个字段)。 - Tirmean
1个回答

5

Spring Batch步骤的工作方式如下:ItemReader.read()方法将被调用,直到它返回null。


那我该怎么办呢? 我不使用JdbcCursorItemReader,因为我只需要返回第一行。(一个字段) - Tirmean
使用ItemReader仅读取一行似乎有些浪费...但是最简单的解决方案是设置一个标志,在调用read方法超过一次时返回null。 - Pablo Lozano
我正在使用ItemReader,因为我需要从多个SQL中检索多个值。因此,我正在使用SimpleAsyncTaskExecutor通过分区执行。 - Tirmean

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接