[英]Spring Batch: How to read items manually from an ItemReader in a listener method?
I'm looking for a way to use an ItemReader
in an @AfterStep
listener method to check the database for a specific status for items being processed in the batch job.我正在寻找一种方式来使用
ItemReader
在@AfterStep
监听方法检查数据库的特定状态在批处理作业正在处理项目。 (The items are child objects of a row in another table, and those parent objects are not handled in the batch job step.) I'm currently using a JdbcCursorItemReader
to perform SQL against an H2 database. (这些项目是另一个表中一行的子对象,这些父对象不在批处理作业步骤中处理。)我目前正在使用
JdbcCursorItemReader
对 H2 数据库执行 SQL。
However, I'm not sure how to just read a line from the resultset, or if I need to programmatically open/close the JdbcCursorItemReader
, as I'm simply using the ItemReader<T>
interface to represent it.但是,我不确定如何从结果
JdbcCursorItemReader
读取一行,或者是否需要以编程方式打开/关闭JdbcCursorItemReader
,因为我只是使用ItemReader<T>
接口来表示它。 The only method I have access to that way is read()
.我可以通过这种方式访问的唯一方法是
read()
。 Should I cast as JdbcCursorItemReader<T>
instead, and open/read each item/close?我是否应该将其
JdbcCursorItemReader<T>
为JdbcCursorItemReader<T>
,然后打开/读取每个项目/关闭? Is it possible to get the chunk size from the step execution object in order to process these in chunks?是否可以从步骤执行对象中获取块大小以便以块的形式处理这些块?
I realize this isn't an ideal way to do this, but I need to be able to do this after every step, and it would, IMO, unnecessarily complicate the Java config to add a separate RPW step after each existing step to do this.我意识到这不是一个理想的方法来做到这一点,但我需要能够在每一步之后都做到这一点,而且 IMO 会不必要地使 Java 配置复杂化,以便在每个现有步骤之后添加一个单独的 RPW 步骤来做到这一点.
In short, what I would like to do in the @AfterStep
listener is:简而言之,我想在
@AfterStep
监听器中做的是:
ItemWriter
to write the errored items.) ItemWriter
来写入错误项目而ItemWriter
。)ItemWriter
commits after write(), or that the db is able to be read "dirty") - Use an ItemReader
to read in all parent objects where all the child objects have a FAILED status. ItemWriter
在 write() 之后提交,或者数据库能够被“脏”读取) - 使用ItemReader
读取所有子对象都具有 FAILED 状态的所有父对象。ItemWriter
for the parent.ItemWriter
为父写入。 However, I don't know how to read items from the ItemReader
(and properly maintaining the cursor), whether individually or in a collection.但是,我不知道如何从
ItemReader
读取项目(并正确维护游标),无论是单独还是在集合中。 The StepBuilderFactory.reader()
method builds the reader in the step config and Spring Batch magic does the rest. StepBuilderFactory.reader()
方法在步骤配置中构建阅读器,其余的由 Spring Batch 魔法完成。 Using an ItemWriter
in a listener method seems pretty straight-forward, and write(Collection<T> items)
just seems to work.在侦听器方法中使用
ItemWriter
似乎非常简单,而write(Collection<T> items)
似乎可以正常工作。 I was just hoping someone here might know how to correctly read items from the ItemReader
in this way.我只是希望这里有人可能知道如何以这种方式从
ItemReader
正确读取项目。
Thanks in advance!提前致谢!
Yes, casting is an option.是的,铸造是一种选择。 Something like this should work:
这样的事情应该工作:
if (reader instanceof ItemStreamReader) {
(ItemStreamReader) reader.open(new ExecutionContext());
}
while((in = reader.read()) != null) {
<do what you need here>
}
if (reader instanceof ItemStreamReader) {
(ItemStreamReader) reader.close();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.