Spring 批处理 - 在处理器跳过时禁用 ItemReader 缓存,并再次处理过滤的行

Spring Batch - disable ItemReader cache on processor skip, and process filtered rows again

我使用 Spring Batch 已经有一段时间了,今天我尝试增强我的批处理,使它们更加可靠。所以我启用了.faultTolerant和.skipLimit/.skip,但是我没有启用任何重试策略。

我几乎所有的批次都有 1 个 reader(JpaPagingItemReader 从我的数据库读取数据)、1 个处理器和 1 个编写器来创建 XML 文件。这3个部分一步到位,我的chunk size一般在50左右

我注意到发生可跳过异常时的两个副作用,我想知道是否有办法更改这些默认行为=>

  1. 我通常在我的 reader 中读取 JPA 实体,将它们发送到我的处理器,我将在其中更改它们的属性以更新我的数据库。但是当发生可跳过的异常并再次处理实体时,它们会变成分离的实体并且它们不再与数据库匹配,因为它们的状态在先前的过程中被更新,而数据库已被回滚。 我知道 reader 的结果被缓存了,并且文档说 ItemReader 的结果应该保持 "idempotent" (不变)。但是有没有办法禁用 ItemReader 的缓存,并在再次处理元素时强制加载新实体?它会更适合我的用例。
  2. 我注意到当一个元素被过滤时(处理器returns null),然后发生可跳过的异常,过滤后的元素在回滚后不会再次处理。有没有办法改变它?

在问这个问题之前,我在 Whosebug、spring 批处理文档和 google 上搜索了很多,但找不到我的答案。

在此先感谢您的宝贵帮助,并非常感谢 Spring Batch 的开发人员提供了这么棒的工具!

  1. But is there a way to disable the cache of the ItemReader

readerTransactionalQueue 是您要查找的内容(尽管名称中的 "queue" 不适用于您的情况,但这仍然是禁用缓存的原因)。

  1. I noticed that when an element is filtered (the processor returns null), and then a skippable exception occurs, the filtered elements are not processed again after the rollback. Is there a way to change that ?

我看不出有什么明显的方法可以改变它。这是how it worked since the beginning I think (looking at the last modification date). But this makes sense to me, why would one want to reprocess an item if it was filtered? This item would be re-filtered again anyway, unless the processor is not idempotent which is not recommended