Spring Batch 跳过 ItemWriter 异常

2024-04-11

我正在尝试将 Spring Batch 2.2.5 与 Java 配置一起使用。这是我的配置:

@Configuration
@EnableBatchProcessing
public class JobConfiguration {
    @Autowired
    private JobBuilderFactory jobBuilder;

    @Autowired
    private StepBuilderFactory stepBuilder;

    @Bean
    @Autowired
    public Job processDocumentsJob() {
        return jobBuilder.get("processDocumentsJob")
                .start(procesingStep())
                .build();
    }

    @Bean
    @Autowired
    public Step procesingStep() {
           CompositeItemProcessor<File, DocumentPackageFileMetadata> compositeProcessor = new CompositeItemProcessor<File, DocumentPackageFileMetadata>();
           compositeProcessor.setDelegates(Lists.newArrayList(
                   documentPackageFileValidationProcessor(),     
                   documentMetadataFileTransformer()
           ));
        return stepBuilder.get("procesingStep")
                .<File, DocumentPackageFileMetadata>chunk(1)
                .reader(documentPackageFileReader())
                .processor(compositeProcessor)
                .writer(documentMetadataFileMigrator())
                .faultTolerant()
                .skip(DocumentImportException.class)
                .skipLimit(10)
                .listener(stepExecutionListener())
                .build();
    }
....


}

使用上面的配置,如果 itemwriter(documentMetadataFileMigrator 指向的 bean)抛出“DocumentImportException”,则不会跳过该异常。 Spring Batch 实际上会再次重试相同的输入。即它将对“documentPackageFileValidationProcessor”使用相同的输入。

但是,如果我将 itemwriter 内部的逻辑移动到 itemprocessor 中:

 @Bean
        @Autowired
        public Step procesingStep() {
               CompositeItemProcessor<File, DocumentPackageFileMetadata> compositeProcessor = new CompositeItemProcessor<File, DocumentPackageFileMetadata>();
               compositeProcessor.setDelegates(Lists.newArrayList(
                       documentPackageFileValidationProcessor(),     
                       documentMetadataFileTransformer(),
                       documentMetadataFileMigratorAsProcessor() // same as itemwriter, but implemented as itemprocessor
               ));
            return stepBuilder.get("procesingStep")
                    .<File, DocumentPackageFileMetadata>chunk(1)
                    .reader(documentPackageFileReader())
                    .processor(compositeProcessor)                    
                    .faultTolerant()
                    .skip(DocumentImportException.class)
                    .skipLimit(10)
                    .listener(stepExecutionListener())
                    .build();
        }

那么异常将被正确跳过。即 Spring Batch 不会针对“documentPackageFileValidationProcessor”重试相同的项目。它将转到下一个要处理的项目(从“documentPackageFileReader”返回的项目)。

这是 Spring Batch 上的错误,还是它的行为符合预期?如果是这样,有人可以指出我相关的文档吗?

谢谢大家,如果这是一个基本问题,请道歉。

此致,

Alex


这种行为是正确的。 ItemWriter 接收要写入的项目列表。如果引发可跳过的异常,Spring Batch 会尝试确定实际导致异常的项目,以便仅跳过该项目。完成此操作的方式是回滚事务,将提交间隔更改为 1,然后重新处理每个项目并再次尝试写入。这允许仅跳过有错误的项目,而不需要跳过整个块。

这里讨论了同样的问题(仅使用 XML 配置):Spring Batch 中的跳过是如何实现的? https://stackoverflow.com/questions/16567432/how-is-the-skipping-implemented-in-spring-batch

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Batch 跳过 ItemWriter 异常 的相关文章

随机推荐

  • python dask DataFrame,支持(可并行化)行应用吗?

    我最近发现dask http dask pydata org en latest index html旨在成为一个易于使用的 python 并行处理模块 对我来说最大的卖点是它可以与熊猫一起使用 在阅读了其手册页后 我找不到一种方法来完成这
  • 如何获取Python字典中的最高键?

    d 苹果 9 橙子 3 葡萄 22 如何返回最大的键 值 编辑 如何制作一个按最大到最低值排序的列表 gt gt gt d apple 9 oranges 3 grapes 22 gt gt gt v k max v k for k v i
  • 包 inputenc 错误:Unicode char \u8:β 未设置为与 LaTeX 一起使用

    我在 Bibdesk 中的参考文献之一包含一些拉丁 希腊字符 例如 我在 TEXMAKER 中使用参考时收到错误 包 inputenc 错误 Unicode char u8 未设置为与 LaTeX 一起使用 我该如何设置它才能工作 尽管使用
  • 数据按用户分组为每周、每月

    我有一个高库存折线图 显示给定股票的每日和每周股价 问题是 当数据数组足够大时 每日数据点将被 采样 为每周数据点 而每周数据点将被采样为每月数据点 有什么方法可以根据需要将用户设置为每周或每月 提前致谢 检查一下数据分组 http www
  • 返回时如何刷新片段 UI

    我是 Android 新手 正在学习片段的知识 并为其制作了一个演示 因为我有一个片段 我们可以从中转到另一个活动 那里正在执行一些计算 然后我们当时回到片 段我想将该计算值显示到我的片段的文本视图中 那么我应该使用哪种生命周期方法来执行此
  • TypeError:无法解压不可迭代的 NoneType 对象

    我知道这个问题之前已经被问过 但我似乎无法让我的问题工作 import numpy as np def load dataset def download filename source http yaan lecun com exdb m
  • 如何从store方法获取上传的图片名称

    当我在 Laravel 中存储图像时 path request gt file myImage gt store public src 它返回完整路径 但如何仅获取给定的文件名 这是返回路径的示例 public src ltX4COwEmv
  • JPackages JavaFX + Spring boot 无法启动

    我正在尝试使用 Spring Boot 构建 JavaFX 应用程序并使用 jpackage 进行部署 当使用javafx maven plugin javafx run命令 我可以看到项目启动 但在将其构建为 msi安装程序 安装并启动
  • 使用 rvmrc 或 ruby​​-version 文件通过 RVM 设置项目 gemset?

    我使用RVM Ruby 版本管理器 https rvm io 为我的每个 Rails 项目指定 Ruby 版本和一组 gem 我有一个 rvmrc文件来自动选择 Ruby 版本和 gemset 每当我cd进入项目目录 安装 RVM 1 19
  • git-p4 正在吃掉我的图像文件

    所以 我可以使用 git p4 从 p4 导入 没有任何问题 一切似乎都正常 但我的 PNG 文件 也许还有其他文件 正在损坏 我读过有关 gitattributes 和行结束问题的内容 但我所做的一切似乎都无法改变最终结果 破碎的图像 我
  • 如何使用 joblib.dump 在 s3 上保存 sklearn 模型?

    我有一个 sklearn 模型 我想使用 joblib dump 将 pickle 文件保存在我的 s3 存储桶上 I used joblib dump model model pkl 将模型保存到本地 但我不知道如何将其保存到 s3 存储
  • C、C++ 初始化中的排序

    考虑以下初始化 C C int a f g struct int x y foo f g C struct goo goo int x int y goo b f g goo c f g C 11 goo d f g 是执行顺序f and
  • 将内核内置模块替换为可加载模块

    我开发了一个内核模块来管理 nf4 标签作为字符设备 我在内核之外开发了这个模块 并在开发阶段将其编译为可加载内核模块 即 ko 进行了测试 一旦驱动程序功能正常且足够稳定 我就使用补丁将其插入 Linux 内核源代码 v4 9 30 以便
  • 可执行的 Spring Boot 2 jar

    我尝试安装我的 Spring Boot 应用程序 第一步 我尝试创建一个可执行 jar 如下所述 https docs spring io spring boot docs current reference html deployment
  • Django CreateView过滤选择字段中的外键

    我需要一些有关 Django 2 和 Python 3 的帮助 我正在使用一个CreateView在我的数据库中添加新记录 但我需要为我的记录创建一个过滤器Aviso表单页面使选择字段 fieldturma 仅显示其中的实例represen
  • 在 C# 中以科学记数法显示 IEEE-754 四倍精度 (binary128) 浮点值

    我正在尝试将原始二进制数据从线程上下文转换为人类可读的格式 并且在尝试转换时出现空的情况四精度浮点 http en wikipedia org wiki Quadruple precision floating point format I
  • 访问Scheme中的调用堆栈深度

    为了演示尾递归的有效性 我想要一种在Scheme中动态访问调用堆栈深度的方法 有没有办法做到这一点 如果没有 有没有办法在其他主要函数语言 OCaml Haskell 等 中做到这一点 Racket 允许您在调用堆栈中存储值 您可以使用它来
  • 如何删除indexedDB?

    我正在从事一个涉及使用 IndexedDB 的项目 当我开始了解这项技术时 我需要能够手动删除索引数据库 以便我可以重新开始 我在 Firefox 中找到了方法 但在 Google Chrome 中找不到方法 我尝试删除该文件夹的内容 我使
  • 在运行时访问 build-id

    我试图弄清楚如何访问链接器在运行时生成的构建 ID 从这个页面开始 https linux die net man 1 ld https linux die net man 1 ld 当我构建一个测试程序时 例如 gcc test c o
  • Spring Batch 跳过 ItemWriter 异常

    我正在尝试将 Spring Batch 2 2 5 与 Java 配置一起使用 这是我的配置 Configuration EnableBatchProcessing public class JobConfiguration Autowir