Reputation: 389
I have a MultiResourceItemReader with a custom ItemReader as a delegate. The problem I'm facing is that when I launch the job, the same file is read over and over again.
This is the delegate class:
public class AllegatiReader implements ResourceAwareItemReaderItemStream<Allegato> {
@PersistenceContext
protected EntityManager em;
private Resource resource;
@Override
public void close() throws ItemStreamException {
}
@Override
public void open(ExecutionContext arg0) throws ItemStreamException {
}
@Override
public void update(ExecutionContext arg0) throws ItemStreamException {
}
@Override
public Allegato read() throws Exception, UnexpectedInputException,
ParseException, NonTransientResourceException {
// DO SOMETHING ...
byte[] fileContent = new byte[(int) resource.getFile().length()];
resource.getInputStream().read(fileContent);
resource.getInputStream().close();
allegato.getFile().setFile(fileContent);
return allegato;
}
@Override
public void setResource(Resource arg0) {
this.resource = arg0;
}
}
Here is my Spring Batch XML configuration file:
<batch:job id="allegati" incrementer="jobParametersIncrementer">
<batch:step id="allegati-import">
<batch:tasklet>
<batch:chunk reader="allegati-reader" writer="allegati-writer" commit-interval="1"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="allegati-reader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="file:#{jobParameters['FILEPATH']}/*" />
<property name="delegate" ref="allegati-filereader" />
</bean>
<bean id="allegati-writer" class="org.springframework.batch.item.database.JpaItemWriter">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="allegati-filereader" class="it.infogroup.vertenze.porting.reader.AllegatiReader" />
How can I tell Spring Batch to move to the next file?
Upvotes: 0
Views: 3296
Reputation: 6630
Your custom reader has to show Spring Batch when all is done, see http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/ItemReader.html#read--
Reads a piece of input data and advance to the next one. Implementations must return null at the end of the input data set.
in your case i would use an private attribute to save* the state for the resource of this reader instance is processed, it could be the Allegato object, but that seems to be a rather large one
*) your reader is stateful by design, so another state attribute should be no problem
Upvotes: 3