java - 在下次运行中使用上次成功运行日期进行查询

标签 java spring spring-batch

我正在使用 Spring Batch 的 JdbcPagingItemReader 来处理数据库中的条目。我正在查询的表中有一个时间戳列,我希望 JdbcPagingItemReader 在下次运行中仅处理 其中时间戳 >“上次成功执行作业”

的项目

我认为这应该是一个相当常见的用例,但不知何故我不知道如何配置它。感谢您的帮助!

最佳答案

JdbcPagingItemReader有它自己的自定义重启逻辑。它搜索映射到唯一索引字段的最后检索到的值,并从那里重新启 Action 业。

来自JavaDocs :

On restart it uses the last sort key value to locate the first page to read (so it doesn't matter if the successfully processed itmes have been removed or modified).

如您所见,您的时间戳字段不会产生任何显着差异。

<小时/>

阅读评论后更新:

好的,那么如何为 PagingQueryProvider 动态创建 where 子句呢? ?

<bean id="itemReader" class="org.spr...JdbcPagingItemReader">
    <property name="dataSource" ref="dataSource"/>
    <property name="queryProvider">
        <bean class="org.spr...SqlPagingQueryProviderFactoryBean">
            <property name="selectClause" value="select id, name, credit"/>
            <property name="fromClause" value="from customer"/>
            <property name="whereClause">
                <bean class="your.company.WhereClauseFactorybean" />
            <property />
            <property name="sortKey" value="id"/>
        </bean>
    </property>
    <property name="parameterValues">
        <map>
            <entry key="status" value="NEW"/>
        </map>
    </property>
    <property name="pageSize" value="1000"/>
    <property name="rowMapper" ref="customerMapper"/>
</bean>

现在实现WhereClauseFactorybean作为FactoryBean使用 JdbcTemplate查找最后一个时间戳并返回类似 where timestamp > <your time stamp> 的内容或null如果没有找到时间戳。

引用:

<小时/>

阅读更多评论后更新:

那么我想你必须implement a custom StepExecutionListener ,注入(inject) AbstractSqlPagingQueryProvider 进入它并 set the where clause beforeStep(StepExecution) 方法。

关于java - 在下次运行中使用上次成功运行日期进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5088933/

相关文章:

java - 使用 BouncyCaSTLe 加密字符串的问题

java - 具有参数 PersistentEntityResourceAssembler 的 RestController 的 JUnit

java - OSGi 列表,那些 header 是什么?

spring - org.springframework.batch.item.ItemStreamException : File is not writable: Spring Batch

java - 多线程批处理写入和读取数据库

java - 从 Spring 作业配置中删除重复代码

java - 获取Set中的多个连接数据

Java源代码解析

java - 使用异步任务将大字符串从 Android 发送到 Servlet

Spring Social Twitter Oauth