1

I am reading from source table (using JpaPagingItemReader) and passing to ItemProcessor. My requirement is if Item is processed successfully then it should write to TABLE_A and if processing failed then write to TABLE_B. I got it working, but I dont feel it as nice way. My current implementation is

// 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
}

It is doing functionally what exactly I want. One drawback of above is when I see job execution / step execution history, I can't know how many are successful or how many are failure, as it shows e.g. if 100 reads then 100 writes.

Can anyone suggest better approach. Are conditional steps useful here?

4

1 回答 1

2

你可以在你的处理器上抛出一个异常,并将这个异常声明为 Skipable(如果不是,chunck 将被破坏)。

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

批处理结束时,writedItemsCount 为有效项数,skippedItemCount 为无效项数。

在不同表中写入的其他方法是将ClassifierCompositeItemWriter与 BackToBackPatternClassifier 一起使用,但您会丢失无效项目的数量。

于 2013-02-12T16:47:53.157 回答