• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring Batch - Delete a FlatFile after read

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:D:\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>
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic