Read xml file and persist information every so often


This is the scenario: I have an xml file that contains information that must be stored, updated or deleted from my database every month. I have taken two approaches to read the file from Java every so often:

  • I used a cron with @Scheduled and the property cron to run it every 30 days. Here I validate the name of the file, if I have the record of the date, I do not read it again.
  • I used @EnableBatchProcessing to execute a process every time the program was executed, foreseeing that the application 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 do not know where to start. Is there in Java something like a "watcher" that is pending from that folder and can read that event (new file deposited in the folder)?

asked by AndreFontaine 16.03.2018 в 11:55

1 answer


you can implement WatchService which allows you to detect when has created a file, deleted or modified. Also used Quartz you can schedule tasks to your liking. In this way using WatchService you can launch a job (quartz) to do some task.

I hope it helped you.

answered by 16.03.2018 / 12:23