private static IDirectoryScanListener GetListener(JobDataMap mergedJobDataMap, SchedulerContext schedCtxt)
        {
            var listenerName = mergedJobDataMap.GetString(DirectoryScanJob.DirectoryScanListenerName);

            if (listenerName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DirectoryScanJob.DirectoryScanListenerName + "' not found in merged JobDataMap");
            }

            schedCtxt.TryGetValue(listenerName, out var temp);
            IDirectoryScanListener listener = (IDirectoryScanListener)temp;

            if (listener == null)
            {
                throw new JobExecutionException("IDirectoryScanListener named '" +
                                                listenerName + "' not found in SchedulerContext");
            }

            return(listener);
        }
Beispiel #2
0
        /// <summary>
        /// This is the main entry point for job execution. The scheduler will call this method on the
        /// job once it is triggered.
        /// </summary>
        /// <param name="context">The <see cref="IJobExecutionContext"/> that
        /// the job will use during execution.</param>
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap       mergedJobDataMap = context.MergedJobDataMap;
            SchedulerContext schedCtxt        = null;

            try
            {
                schedCtxt = context.Scheduler.Context;
            }
            catch (SchedulerException e)
            {
                throw new JobExecutionException("Error obtaining scheduler context.", e, false);
            }

            string dirName      = mergedJobDataMap.GetString(DIRECTORY_NAME);
            string listenerName = mergedJobDataMap.GetString(DIRECTORY_SCAN_LISTENER_NAME);

            if (dirName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DIRECTORY_NAME + "' not found in merged JobDataMap");
            }
            if (listenerName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DIRECTORY_SCAN_LISTENER_NAME + "' not found in merged JobDataMap");
            }

            object temp;

            schedCtxt.TryGetValue(listenerName, out temp);
            IDirectoryScanListener listener = (IDirectoryScanListener)temp;

            if (listener == null)
            {
                throw new JobExecutionException("DirectoryScanListener named '" +
                                                listenerName + "' not found in SchedulerContext");
            }

            DateTime lastDate = DateTime.MinValue;

            if (mergedJobDataMap.ContainsKey(LAST_MODIFIED_TIME))
            {
                lastDate = mergedJobDataMap.GetDateTime(LAST_MODIFIED_TIME);
            }

            long minAge = 5000;

            if (mergedJobDataMap.ContainsKey(MINIMUM_UPDATE_AGE))
            {
                minAge = mergedJobDataMap.GetLong(MINIMUM_UPDATE_AGE);
            }
            DateTime maxAgeDate = DateTime.Now.AddMilliseconds(minAge);

            FileInfo[] updatedFiles = GetUpdatedOrNewFiles(dirName, lastDate, maxAgeDate);

            if (updatedFiles == null)
            {
                return;
            }

            DateTime latestMod = DateTime.MinValue;

            foreach (FileInfo updFile in updatedFiles)
            {
                DateTime lm = updFile.LastWriteTime;
                latestMod = (lm > latestMod) ? lm : latestMod;
            }

            if (updatedFiles.Length > 0)
            {
                // notify call back...
                listener.FilesUpdatedOrAdded(updatedFiles);
            }

            // It is the JobDataMap on the JobDetail which is actually stateful
            context.JobDetail.JobDataMap.Put(LAST_MODIFIED_TIME, latestMod);
        }
Beispiel #3
0
        /// <summary>
        /// This is the main entry point for job execution. The scheduler will call this method on the
        /// job once it is triggered.
        /// </summary>
        /// <param name="context">The <see cref="IJobExecutionContext"/> that
        /// the job will use during execution.</param>
        public void Execute(IJobExecutionContext context)
        {
            JobDataMap       mergedJobDataMap = context.MergedJobDataMap;
            SchedulerContext schedCtxt;

            try
            {
                schedCtxt = context.Scheduler.Context;
            }
            catch (SchedulerException e)
            {
                throw new JobExecutionException("Error obtaining scheduler context.", e, false);
            }

            string dirName      = mergedJobDataMap.GetString(DirectoryName);
            string listenerName = mergedJobDataMap.GetString(DirectoryScanListenerName);

            if (dirName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DirectoryName + "' not found in merged JobDataMap");
            }
            if (listenerName == null)
            {
                throw new JobExecutionException("Required parameter '" +
                                                DirectoryScanListenerName + "' not found in merged JobDataMap");
            }

            object temp;

            schedCtxt.TryGetValue(listenerName, out temp);
            IDirectoryScanListener listener = (IDirectoryScanListener)temp;

            if (listener == null)
            {
                throw new JobExecutionException("DirectoryScanListener named '" +
                                                listenerName + "' not found in SchedulerContext");
            }

            DateTime lastDate = DateTime.MinValue;

            if (mergedJobDataMap.ContainsKey(LastModifiedTime))
            {
                lastDate = mergedJobDataMap.GetDateTime(LastModifiedTime);
            }

            TimeSpan minAge = TimeSpan.FromSeconds(5);

            if (mergedJobDataMap.ContainsKey(MinimumUpdateAge))
            {
                minAge = TimeSpan.FromMilliseconds(mergedJobDataMap.GetLong(MinimumUpdateAge));
            }

            DateTime maxAgeDate = DateTime.Now - minAge;

            FileInfo[] updatedFiles = GetUpdatedOrNewFiles(dirName, lastDate, maxAgeDate);

            if (updatedFiles == null)
            {
                log.Warn("Directory '" + dirName + "' does not exist.");
                return;
            }

            DateTime latestMod = lastDate;

            foreach (FileInfo updFile in updatedFiles)
            {
                DateTime lm = updFile.LastWriteTime;
                latestMod = (lm > latestMod) ? lm : latestMod;
            }

            if (updatedFiles.Length > 0)
            {
                // notify call back...
                log.Info("Directory '" + dirName + "' contents updated, notifying listener.");
                listener.FilesUpdatedOrAdded(updatedFiles);
            }
            else if (log.IsDebugEnabled)
            {
                log.Debug("Directory '" + dirName + "' contents unchanged.");
            }

            // It is the JobDataMap on the JobDetail which is actually stateful
            context.JobDetail.JobDataMap.Put(LastModifiedTime, latestMod);
        }