Spring batch Spring使用ItemWriter有条件地批量写入不同的表(如果成功,则ItemProcessor输出到表A,如果失败,则输出到表B)

Spring batch Spring使用ItemWriter有条件地批量写入不同的表(如果成功,则ItemProcessor输出到表A,如果失败,则输出到表B),spring-batch,Spring Batch,我正在从源表中读取(使用JpaPagingItemReader)并将其传递给ItemProcessor。 我的要求是,若项目处理成功,则应将其写入表A;若处理失败,则应将其写入表B。 我让它工作起来了,但我感觉不太好。 我当前的实现是 // my processor public class MyItemProcessor implements ItemProcessor<SourceEntity, BaseOutputEntity>{ @Override publ

我正在从源表中读取(使用JpaPagingItemReader)并将其传递给ItemProcessor。 我的要求是,若项目处理成功,则应将其写入表A;若处理失败,则应将其写入表B。 我让它工作起来了,但我感觉不太好。 我当前的实现是

// my processor
public class MyItemProcessor implements ItemProcessor<SourceEntity, BaseOutputEntity>{

    @Override
    public BaseOutputEntity process(SourceEntity input) {
       // NOTE: EntityA, EntityB both extend BaseOutputEntity
       try {
           EntityA a = callMyBusiness.method(input);
           return a;
       } catch (MyBusinessException e) {
           EntityB b = createMyFailureObj(input)
           return b;
       }
   }
}

// my itemwriter
public class MyItemWriter extends JpaItemWriter<MyBaseOutputEntity> {
    // donthing as JpaItemWriter methods will take care
}
//我的处理器
公共类MyItemProcessor实现ItemProcessor{
@凌驾
公共BaseOutputenty进程(SourceEntity输入){
//注意:EntityA和EntityB都扩展了BaseOutpuntity
试一试{
EntityA=callMyBusiness.method(输入);
返回a;
}捕获(MyBusinessException e){
EntityB=createMyFailureObj(输入)
返回b;
}
}
}
//我的项目作者
公共类MyItemWriter扩展了JpaItemWriter{
//donthing as JpaItemWriter方法会注意的
}
它的功能正是我想要的。 上面的一个缺点是,当我看到作业执行/步骤执行历史记录时,我无法知道有多少成功或有多少失败,如图所示,例如,如果100次读取,那么100次写入


有谁能提出更好的方法吗。条件步骤在这里有用吗?

您可以在处理器上引发异常,并将此异常声明为可跳过(否则,chunck将被破坏)

如果实现了,则可以在onProcessError(Entry item,Exception t)函数上捕获无效项并将其写入表B。 (仔细阅读文档:某些侦听器函数用于事务,而其他函数则不用于事务)

在批处理结束时,WriteEdItemScont是有效项的数量,SkippeEdItemCount是无效项的数量

在不同表中写入的另一种方法是与BackToBackPatternClassifier一起使用,但会丢失无效项的计数