私は春のバッチジョブを書いていますが、私のステップの1つに、プロセッサ用の次のコードがあります:
@Component
public class SubscriberProcessor implements ItemProcessor<NewsletterSubscriber, Account>, InitializingBean {
@Autowired
private AccountService service;
@Override public Account process(NewsletterSubscriber item) throws Exception {
if (!Strings.isNullOrEmpty(item.getId())) {
return service.getAccount(item.getId());
}
// search with email address
List<Account> accounts = service.findByEmail(item.getEmail());
checkState(accounts.size() <= 1, "Found more than one account with email %s", item.getEmail());
return accounts.isEmpty() ? null : accounts.get(0);
}
@Override public void afterPropertiesSet() throws Exception {
Assert.notNull(service, "account service must be set");
}
}
Account
上記のコードは機能しますが、 1つ以上のコードを使用できるエッジ ケースがいくつかあることがわかりましたNewsletterSubscriber
。Account
そのため、状態チェックを削除して、アイテム ライターに複数渡す必要があります。
私が見つけた1つの解決策は、両方を変更し、ItemProcessor
代わりにタイプItemWriter
を処理することですが、これには2つの欠点があります。List<Account>
Account
- ライターのネストされたリストのために、コードとテストはより醜く、作成と保守が困難です
- 最も重要なのは、複数の
Account
オブジェクトが同じトランザクションで書き込まれる可能性があることです。ライターに指定されたリストには複数のアカウントが含まれる可能性があり、これを避けたいからです。
おそらくリスナーを使用するか、スプリングバッチで使用される内部コンポーネントを置き換えて、プロセッサのリストを回避する方法はありますか?
アップデート
この問題について、 Spring Jira でイシューをオープンしました。
拡張ポイントとしてマークされているisCompleteおよびgetAdjustedOutputsメソッドを調べて、目標を達成するために何らかの方法でそれらを使用できるかどうかを確認しています。FaultTolerantChunkProcessor
SimpleChunkProcessor
どんなヒントでも大歓迎です。