简体   繁体   English

Spring Batch:如何在侦听器方法中从 ItemReader 手动读取项目?

[英]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监听器中做的是:

  1. Update failed items from the step.更新步骤中失败的项目。 (This is already working by keeping a list in the listener of failed items on skip, and using a separate ItemWriter to write the errored items.) (这已经通过在跳过失败项目的侦听器中保留一个列表,并使用单独的ItemWriter来写入错误项目而ItemWriter 。)
  2. (Assuming the 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 状态的所有父对象。
  3. Change the parent status to FAILED and write using an existing ItemWriter for the parent.将父状态更改为 FAILED 并使用现有的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.

相关问题 Spring 批处理 - 如何将多个项目的列表从输入传递到 ItemReader、ItemProcessor 和 ItemWriter - Spring Batch- how to pass list of multiple items from input to ItemReader, ItemProcessor and ItemWriter 如何从 Spring 批处理中的资源(itemReader)获取文件名? - How to get filename from resource (itemReader) in Spring batch? 如何在spring-batch中将参数从ItemReader传递到ItemProcessor? - How to pass parameters from ItemReader to ItemProcessor in spring-batch? 如何在 Spring Batch 中从 ItemReader 访问作业参数? - How to get access to job parameters from ItemReader, in Spring Batch? 如何在Spring Batch中从ItemReader动态设置MultiResourceItemWriter的资源 - How to set the resource of MultiResourceItemWriter dynamically from ItemReader in Spring Batch 如何在Spring Batch 4中的ItemReader中获取JobExecutionId - How to get the JobExecutionId in ItemReader in Spring Batch 4 如何在 Spring-Batch 中使用 ItemReader 跳过行? - How to skip lines with ItemReader in Spring-Batch? Spring Batch:自定义ItemReader - Spring Batch : custom ItemReader 如何在Spring Boot Web应用程序中从Spring批处理作业的ItemReader类访问jobparameter - How to access jobparameter from ItemReader class of Spring batch job in spring boot web application Spring Batch:如何从ItemReader或ItemWriter中访问当前步骤的ID /名称 - Spring Batch: How to access the current step's id / name from within an ItemReader or ItemWriter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM