This is the second post of the New in Quartz.Net 2.0 series. Today we will look at the DirectoryScanJob, which is a new job in Quartz.Net 2.0.
The DirectoryScanJob is very similar to the FileScanJob, which is available in Quartz.Net 1.0. As you can probably tell from the names, the main difference between the two jobs is that the DirectoryScanJob scans for changes in… you quessed it… a directory , whereas the FileScanJob scans for changes in… yes, you guessed it again… a file. If you’re not familiar with the FileScanJob, don’t worry, we will look at the DirectoryScanJob in detail.
Introducing the DirectoryScanJobFirst, let’s take a look at what the documentation says about this new job:
“Inspects a directory and compares whether any files' "last modified dates" have changed since the last time it was inspected. If one or more files have been updated (or created), the job invokes a "call-back" method on an identified DirectoryScanListener that can be found in the SchedulerContext.”That seems pretty self explanatory, no? Basically, you tell the job that it needs to check a certain directory to see if new files have been added or if existing files have been modified. When the job is executed it checks the files and stores the file information in its JobDataMap. The next time the job runs, it compares the information stored in the job’s JobDataMap, with the files in the directory. If any changes are found, the job calls the FilesUpdatedOrAdded method of the IDirectoryScanListener that was configured for that job, passing in an arrary of FileInfo objects.
Configuring the DirectoryScanJobTo configure and use a DirectoryScanJob, you will need to set the following keys in the JobDataMap:
- DIRECTORY_NAME: this is the path to the directory that you want to monitor.
- DIRECTORY_SCAN_LISTENER_NAME: this is the name of the DirectoryScanListener that should called if changes are detected
- MINIMUM_UPDATE_AGE: this is an optional parameter which lets you specify how many milliseconds must have passed since the file’s last modified time in order to consider the file modified. If this parameter is not specified, it defaults to 5000 milliseconds.
Possible Uses for the DirectoryScanJobThere are many uses for the DirectoryScanJob, so let’s list a few here:
- You could replace any FileWatcher type services that you might have implemented
- Scan files uploaded by users for viruses
- Schedule other jobs to process files that have been copied to the directory for processing