This is the scenario: I have an xml file that contains information that needs to be stored, updated or deleted from my database every month. I've taken two approaches to read the file every so often from Java:
- I used a cron with
@Scheduled
and propertycron
to run it every 30 days. Here I do validations on the name of the file, if I have the record of the date, I do not read it again. - I used
@EnableBatchProcessing
to run a process every time the program was executed, expecting the application to be deployed every two weeks, again validating if the file was previously read.
However, what they are asking me now is that the file be read every time they deposit a new file in the folder from where I read it and I don't know where to start. Is there in Java something like a "watcher" that is aware of that folder and can read that event (new file deposited in the folder)?
you can implement WatchService that allows you to detect when a file has been created, deleted or modified. Also using Quartz you can schedule tasks to your liking. In this way using WatchService you can launch a job(quartz) to do some task for you.
I hope it helped you.