Java ウィザードの皆さん、こんにちは。
春バッチに入るのにとても苦労しています。とりあえず本題へ。フォルダーからすべてのファイル (xml) を処理する必要があり、少し追加してそれらを書き戻します。問題は、入力ファイル名を保持したいということでした。これに対する私が持っている解決策は、StaxEventItemReader を呼び出してマーシャリングされた xml とファイル名を保持するカスタム項目を返すカスタム Itemreader に委任する MultiResourceItemReader です。
質問: 無限ループで同じファイルだけが読み取られ、別の奇妙なことに、毎回 10 回の再試行があります。解決策の 1 つは、ファイルが一度読み取られた後にリーダーが null を返すことですが、それは、処理されたファイルのリストを保持する必要があることを意味します。
今はそうしようと思いますが、もっとスマートなものが欲しいです。
ジョブ構成:
<batch:job id="job1">
<batch:step id="step1" >
<batch:tasklet transaction-manager="transactionManager" start-limit="100" >
<batch:chunk reader="reader" writer="writer" commit-interval="10" />
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="reader" class="org.springframework.batch.item.file.MultiResourceItemReader">
<property name="resources" value="files/*.xml" />
<property name="delegate" ref="myItemReader" />
</bean>
私のアイテムの読み取り方法、基本的に:
public class MyItemReader implements ResourceAwareItemReaderItemStream<MyItem>, ApplicationContextAware {
public MyItem read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
StaxEventItemReader<JAXBElement<RootObject>> reader = new StaxEventItemReader<JAXBElement<RootObject>>();
reader.setResource(currentResource);
reader.setFragmentRootElementName("RootObject");
// ... create jaxb unmarshaller
reader.setUnmarshaller(unmarshaller);
reader.setSaveState(true);
reader.afterPropertiesSet();
reader.open(executionContext);
JAXBElement<RootObject> jaxbElem = reader.read();
MyItem item = new MyItem();
item.setFilename(currentResource.getFile().getName());
item.setJaxbElement(jaxbElem);
return item;
}
}
誰かがここで私をまっすぐにすることができますか?
解決策 したがって、最終的には、読み取ったファイルのリストを保持し、既に読み取られている場合は null を返します。一度に 10 回の読み取りに関しては、まあ、それはチャンクのサイズなので、理にかなっています。