Search code examples
spring-batchlistenerspring-transactions

Move file after transaction commit


I just started using Spring Batch and I don't know how I can implement my business need.

The behavior is quite simple : I have a directory where files are saved. My batch should detect those files, import them into my database and move the file to a backup directory (or an error directory if the data can't be saved).

So I create chunks of 1 file. The reader retrieve them and the processor imports the data.

I read Spring Batch create a global transaction for the whole chunk, and only the ChunkListener is called out of the transaction. It seems to be OK, but the input parameter is a ChunkContext. How can I retrieve the file managed in the chunk ? I don't see where it's stored in the ChunkContext.

I need to be sure the DB accepts the insertions before choosing where the file must be moved. That's why I need to do that after the commit.


Solution

  • Here is how you can proceed:

    • Create a service (based on a file system watching API or something like Spring Integration directory polling) that launches a batch job for the new file
    • The batch job can use a chunk-oriented step to read data and write it to the database. In that job, you can use a job/step execution listener or a separate step to move files to the backup/error directory according to the success or failure of the previous step.