我正在使用 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/