permaculture playing cards*
The moose likes Spring and the fly likes Spring Batch - Delete a FlatFile after read Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring Batch - Delete a FlatFile after read" Watch "Spring Batch - Delete a FlatFile after read" New topic
Author

Spring Batch - Delete a FlatFile after read

Nadia Roestoff
Greenhorn

Joined: May 10, 2012
Posts: 2
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

"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

Joined: Feb 08, 2009
Posts: 597

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.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring Batch - Delete a FlatFile after read
 
Similar Threads
Issue with getting Message from properties file in Controller, but works in JSP
“j_spring_security_check” not found after configuring spring security without http namespace
javax.servlet.ServletException: Could not resolve view with name home-- apache Velocity, spring
REST web service return both XML or JSON
transaction rollbacks, but a record is inserted?