Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring Batch - Delete a FlatFile after read

 
Nadia Roestoff
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am new to Spring Batch. I have a simple setup that uses FlatFileItemReader to read in a file and then uses a writer to write it to a database. I would like to delete the file after it is read (or after the writer, doesn't matter). What is the best/simplest way to do this?

Here is part of the code snippet for the reader:

<beans:bean id="MultiResourceItemReader.MemberRegistration" class="org.springframework.batch.item.file.MultiRe sourceItemReader">
<beans:property name="resources" value="file:\DataExtractSampleFiles\*MEM_REG.txt "/>
<beans:property name="delegate" ref="ItemReader.MemberRegistration">
</beans:property>
</beans:bean>

<beans:bean id="ItemReader.MemberRegistration" class="org.springframework.batch.item.file.FlatFil eItemReader">
<beans:property name="lineMapper">
<beans:bean class="org.springframework.batch.item.file.mapping .DefaultLineMapper">
<beans:property name="lineTokenizer">
<beans:bean class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer">
<beans:property name="delimiter" value="|"/>
<beans:property name="names" value="memberId,loginName,email,registrationDate,l astAccessedDate,cobrandId,country,state,city,zipCo de,ipAddress,guid,extractDate,extractDateMilliseco nds"/>
</beans:bean>
</beans:property>
<beans:property name="fieldSetMapper">
<beans:bean class="za.com.MemRegFieldSetMapper" />
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First. When posting code or configuration. Please use the CODE tags to keep it formatting with tab to be readable. I can't read your configuration as posted.

On to your issue. You can't delete in read, because Spring Batch does chunk processing. It reads one item at a time, then passes a collection of the items to the writer. So if you have a chunk size of 10 and 100 lines in the file. It will read one line at a time, after it reads and processes 10 lines it passes the 10 lines to the writer, then the transaction is committed. Then it reads the next line and process, repeating that 10 times, then calls the writer with the collection of those 10 lines, all lines have been read and processes. That would be done in a step. So the Step is assigned to the Job. You can make a second Step in the Job, which the second step is the delete the file.

Mark
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Zenoe New Member"

JavaRanch requires all users to use their real first and real last names as their user/display name. Your does not meet this requirement. Please click the My Profile link above and change your display name.

Thanks for understanding.

Mark
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also you a listener and use the @AfterStep or @AfterJob to delete the file. The listener is a pretty suitable place to perform clean up again on a step.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic