private static System.Collections.Generic.Dictionary<IJobDetail, ISet<ITrigger>> CreateJobs(int[] intervals) { Log.Info("Building jobs"); var jobDictionary = new System.Collections.Generic.Dictionary<IJobDetail, ISet<ITrigger>>(); for (var index = 0; index < intervals.Length; index++) { var interval = intervals[index]; Log.InfoFormat("Creating Job {0} with interval {1}...", index, interval); var job = JobBuilder.Create<SendMessageJob>() .WithIdentity("sendMessageJob" + index) .UsingJobData("message", $"Job {index} with interval {interval} from process {Process.GetCurrentProcess().Id}") .Build(); var trigger = TriggerBuilder.Create() .WithIdentity("trigger" + index) .StartNow() .WithSimpleSchedule(x => x.WithIntervalInSeconds(interval).RepeatForever()) .Build(); var set = new HashSet<ITrigger> { trigger }; jobDictionary.Add(job, set); } return jobDictionary; }
/// <summary> /// Adds the name-value pairs in the given <see cref="IDictionary" /> to the <see cref="JobDataMap" />. /// <p> /// All keys must be <see cref="String" />s, and all values must be serializable. /// </p> /// </summary> public override void PutAll(IDictionary map) { IEnumerator itr = new HashSet(map.Keys).GetEnumerator(); while (itr.MoveNext()) { object key = itr.Current; object val = map[key]; Put(key, val); // will throw ArgumentException if value not serializable } }
/// <summary> /// Gets the property groups. /// </summary> /// <param name="prefix">The prefix.</param> /// <returns></returns> public virtual string[] GetPropertyGroups(string prefix) { HashSet groups = new HashSet(10); if (!prefix.EndsWith(".")) { prefix += "."; } foreach (string key in props.Keys) { if (key.StartsWith(prefix)) { string groupName = key.Substring(prefix.Length, (key.IndexOf('.', prefix.Length)) - (prefix.Length)); groups.Add(groupName); } } return (string[]) groups.ToArray(typeof (string)); }
protected override TriggerPropertyBundle GetTriggerPropertyBundle(SimplePropertiesTriggerProperties props) { int repeatCount = (int)props.Long1; int interval = props.Int1; string intervalUnitStr = props.String1; string daysOfWeekStr = props.String2; string timeOfDayStr = props.String3; IntervalUnit intervalUnit = (IntervalUnit) Enum.Parse(typeof(IntervalUnit), intervalUnitStr, true); DailyTimeIntervalScheduleBuilder scheduleBuilder = DailyTimeIntervalScheduleBuilder.Create() .WithInterval(interval, intervalUnit) .WithRepeatCount(repeatCount); if (daysOfWeekStr != null) { ISet<DayOfWeek> daysOfWeek = new HashSet<DayOfWeek>(); String[] nums = daysOfWeekStr.Split(','); if (nums.Length > 0) { foreach (String num in nums) { daysOfWeek.Add((DayOfWeek) Int32.Parse(num)); } scheduleBuilder.OnDaysOfTheWeek(daysOfWeek); } } else { scheduleBuilder.OnDaysOfTheWeek(DailyTimeIntervalScheduleBuilder.AllDaysOfTheWeek); } if (timeOfDayStr != null) { string[] nums = timeOfDayStr.Split(','); TimeOfDay startTimeOfDay = null; if (nums.Length >= 3) { int hour = Int32.Parse(nums[0]); int min = Int32.Parse(nums[1]); int sec = Int32.Parse(nums[2]); startTimeOfDay = new TimeOfDay(hour, min, sec); } else { startTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(0, 0, 0); } scheduleBuilder.StartingDailyAt(startTimeOfDay); TimeOfDay endTimeOfDay; if (nums.Length >= 6) { int hour = Int32.Parse(nums[3]); int min = Int32.Parse(nums[4]); int sec = Int32.Parse(nums[5]); endTimeOfDay = new TimeOfDay(hour, min, sec); } else { endTimeOfDay = TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59); } scheduleBuilder.EndingDailyAt(endTimeOfDay); } else { scheduleBuilder.StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(0, 0, 0)); scheduleBuilder.EndingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(23, 59, 59)); } int timesTriggered = props.Int2; String[] statePropertyNames = {"timesTriggered"}; Object[] statePropertyValues = {timesTriggered}; return new TriggerPropertyBundle(scheduleBuilder, statePropertyNames, statePropertyValues); }
protected virtual void ClusterRecover(ConnectionAndTransactionHolder conn, IList failedInstances) { if (failedInstances.Count > 0) { long recoverIds = DateTime.UtcNow.Ticks; LogWarnIfNonZero(failedInstances.Count, "ClusterManager: detected " + failedInstances.Count + " failed or restarted instances."); try { foreach (SchedulerStateRecord rec in failedInstances) { Log.Info("ClusterManager: Scanning for instance \"" + rec.SchedulerInstanceId + "\"'s failed in-progress jobs."); IList firedTriggerRecs = Delegate.SelectInstancesFiredTriggerRecords(conn, rec.SchedulerInstanceId); int acquiredCount = 0; int recoveredCount = 0; int otherCount = 0; ISet triggerKeys = new HashSet(); foreach (FiredTriggerRecord ftRec in firedTriggerRecs) { Key tKey = ftRec.TriggerKey; Key jKey = ftRec.JobKey; triggerKeys.Add(tKey); // release blocked triggers.. if (ftRec.FireInstanceState.Equals(StateBlocked)) { Delegate.UpdateTriggerStatesForJobFromOtherState(conn, jKey.Name, jKey.Group, StateWaiting, StateBlocked); } else if (ftRec.FireInstanceState.Equals(StatePausedBlocked)) { Delegate.UpdateTriggerStatesForJobFromOtherState(conn, jKey.Name, jKey.Group, StatePaused, StatePausedBlocked); } // release acquired triggers.. if (ftRec.FireInstanceState.Equals(StateAcquired)) { Delegate.UpdateTriggerStateFromOtherState(conn, tKey.Name, tKey.Group, StateWaiting, StateAcquired); acquiredCount++; } else if (ftRec.JobRequestsRecovery) { // handle jobs marked for recovery that were not fully // executed.. if (JobExists(conn, jKey.Name, jKey.Group)) { DateTime tempAux = new DateTime(ftRec.FireTimestamp); SimpleTrigger rcvryTrig = new SimpleTrigger( "recover_" + rec.SchedulerInstanceId + "_" + Convert.ToString(recoverIds++, CultureInfo.InvariantCulture), SchedulerConstants.DefaultRecoveryGroup, tempAux); rcvryTrig.Volatile = ftRec.TriggerIsVolatile; rcvryTrig.JobName = jKey.Name; rcvryTrig.JobGroup = jKey.Group; rcvryTrig.MisfireInstruction = MisfireInstruction.SimpleTrigger.FireNow; rcvryTrig.Priority = ftRec.Priority; JobDataMap jd = Delegate.SelectTriggerJobDataMap(conn, tKey.Name, tKey.Group); jd.Put(SchedulerConstants.FailedJobOriginalTriggerName, tKey.Name); jd.Put(SchedulerConstants.FailedJobOriginalTriggerGroup, tKey.Group); jd.Put(SchedulerConstants.FailedJobOriginalTriggerFiretimeInMillisecoonds, Convert.ToString(ftRec.FireTimestamp, CultureInfo.InvariantCulture)); rcvryTrig.JobDataMap = jd; rcvryTrig.ComputeFirstFireTimeUtc(null); StoreTrigger(conn, null, rcvryTrig, null, false, StateWaiting, false, true); recoveredCount++; } else { Log.Warn("ClusterManager: failed job '" + jKey + "' no longer exists, cannot schedule recovery."); otherCount++; } } else { otherCount++; } // free up stateful job's triggers if (ftRec.JobIsStateful) { Delegate.UpdateTriggerStatesForJobFromOtherState(conn, jKey.Name, jKey.Group, StateWaiting, StateBlocked); Delegate.UpdateTriggerStatesForJobFromOtherState(conn, jKey.Name, jKey.Group, StatePaused, StatePausedBlocked); } } Delegate.DeleteFiredTriggers(conn, rec.SchedulerInstanceId); // Check if any of the fired triggers we just deleted were the last fired trigger // records of a COMPLETE trigger. int completeCount = 0; foreach (Key triggerKey in triggerKeys) { if ( Delegate.SelectTriggerState(conn, triggerKey.Name, triggerKey.Group).Equals( StateComplete)) { IList firedTriggers = Delegate.SelectFiredTriggerRecords(conn, triggerKey.Name, triggerKey.Group); if (firedTriggers.Count == 0) { SchedulingContext schedulingContext = new SchedulingContext(); schedulingContext.InstanceId = instanceId; if (RemoveTrigger(conn, schedulingContext, triggerKey.Name, triggerKey.Group)) { completeCount++; } } } } LogWarnIfNonZero(acquiredCount, "ClusterManager: ......Freed " + acquiredCount + " acquired trigger(s)."); LogWarnIfNonZero(completeCount, "ClusterManager: ......Deleted " + completeCount + " complete triggers(s)."); LogWarnIfNonZero(recoveredCount, "ClusterManager: ......Scheduled " + recoveredCount + " recoverable job(s) for recovery."); LogWarnIfNonZero(otherCount, "ClusterManager: ......Cleaned-up " + otherCount + " other failed job(s)."); if (rec.SchedulerInstanceId.Equals(InstanceId) == false) { Delegate.DeleteSchedulerState(conn, rec.SchedulerInstanceId); } } } catch (Exception e) { throw new JobPersistenceException("Failure recovering jobs: " + e.Message, e); } } }
/// <summary> </summary> private IScheduler Instantiate() { if (cfg == null) { Initialize(); } if (initException != null) { throw initException; } ISchedulerExporter exporter = null; IJobStore js; IThreadPool tp; QuartzScheduler qs; SchedulingContext schedCtxt; DBConnectionManager dbMgr = null; string instanceIdGeneratorType = null; NameValueCollection tProps; bool autoId = false; TimeSpan idleWaitTime = TimeSpan.Zero; TimeSpan dbFailureRetry = TimeSpan.Zero; string typeLoadHelperType; string jobFactoryType; SchedulerRepository schedRep = SchedulerRepository.Instance; // Get Scheduler Properties // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string schedName = cfg.GetStringProperty(PropertySchedulerInstanceName, "QuartzScheduler"); string threadName = cfg.GetStringProperty(PropertySchedulerThreadName, string.Format(CultureInfo.InvariantCulture, "{0}_QuartzSchedulerThread", schedName)); string schedInstId = cfg.GetStringProperty(PropertySchedulerInstanceId, DefaultInstanceId); if (schedInstId.Equals(AutoGenerateInstanceId)) { autoId = true; instanceIdGeneratorType = cfg.GetStringProperty(PropertySchedulerInstanceIdGeneratorType, "Quartz.Simpl.SimpleInstanceIdGenerator, Quartz"); } typeLoadHelperType = cfg.GetStringProperty(PropertySchedulerTypeLoadHelperType, "Quartz.Simpl.CascadingClassLoadHelper, Quartz"); jobFactoryType = cfg.GetStringProperty(PropertySchedulerJobFactoryType, null); idleWaitTime = cfg.GetTimeSpanProperty(PropertySchedulerIdleWaitTime, idleWaitTime); dbFailureRetry = cfg.GetTimeSpanProperty(PropertySchedulerDbFailureRetryInterval, dbFailureRetry); bool makeSchedulerThreadDaemon = cfg.GetBooleanProperty(PropertySchedulerMakeSchedulerThreadDaemon); NameValueCollection schedCtxtProps = cfg.GetPropertyGroup(PropertySchedulerContextPrefix, true); bool proxyScheduler = cfg.GetBooleanProperty(PropertySchedulerProxy, false); // If Proxying to remote scheduler, short-circuit here... // ~~~~~~~~~~~~~~~~~~ if (proxyScheduler) { if (autoId) { schedInstId = DefaultInstanceId; } schedCtxt = new SchedulingContext(); schedCtxt.InstanceId = schedInstId; string uid = QuartzSchedulerResources.GetUniqueIdentifier(schedName, schedInstId); RemoteScheduler remoteScheduler = new RemoteScheduler(schedCtxt, uid); string remoteSchedulerAddress = cfg.GetStringProperty(PropertySchedulerProxyAddress); remoteScheduler.RemoteSchedulerAddress = remoteSchedulerAddress; schedRep.Bind(remoteScheduler); return remoteScheduler; } // Create type load helper ITypeLoadHelper loadHelper; try { loadHelper = (ITypeLoadHelper)ObjectUtils.InstantiateType(LoadType(typeLoadHelperType)); } catch (Exception e) { throw new SchedulerConfigException( string.Format(CultureInfo.InvariantCulture, "Unable to instantiate type load helper: {0}", e.Message), e); } loadHelper.Initialize(); IJobFactory jobFactory = null; if (jobFactoryType != null) { try { jobFactory = (IJobFactory) ObjectUtils.InstantiateType(loadHelper.LoadType(jobFactoryType)); } catch (Exception e) { throw new SchedulerConfigException( string.Format(CultureInfo.InvariantCulture, "Unable to Instantiate JobFactory: {0}", e.Message), e); } tProps = cfg.GetPropertyGroup(PropertySchedulerJobFactoryPrefix, true); try { ObjectUtils.SetObjectProperties(jobFactory, tProps); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "JobFactory of type '{0}' props could not be configured.", jobFactoryType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } } IInstanceIdGenerator instanceIdGenerator = null; if (instanceIdGeneratorType != null) { try { instanceIdGenerator = (IInstanceIdGenerator) ObjectUtils.InstantiateType(loadHelper.LoadType(instanceIdGeneratorType)); } catch (Exception e) { throw new SchedulerConfigException( string.Format(CultureInfo.InvariantCulture, "Unable to Instantiate InstanceIdGenerator: {0}", e.Message), e); } tProps = cfg.GetPropertyGroup(PropertySchedulerInstanceIdGeneratorPrefix, true); try { ObjectUtils.SetObjectProperties(instanceIdGenerator, tProps); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "InstanceIdGenerator of type '{0}' props could not be configured.", instanceIdGeneratorType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } } // Get ThreadPool Properties // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string tpType = cfg.GetStringProperty(PropertyThreadPoolType, typeof(SimpleThreadPool).AssemblyQualifiedName); if (tpType == null) { initException = new SchedulerException("ThreadPool type not specified. ", SchedulerException.ErrorBadConfiguration); throw initException; } try { tp = (IThreadPool) ObjectUtils.InstantiateType(loadHelper.LoadType(tpType)); } catch (Exception e) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "ThreadPool type '{0}' could not be instantiated.", tpType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } tProps = cfg.GetPropertyGroup(PropertyThreadPoolPrefix, true); try { ObjectUtils.SetObjectProperties(tp, tProps); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "ThreadPool type '{0}' props could not be configured.", tpType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } // Set up any DataSources // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string[] dsNames = cfg.GetPropertyGroups(PropertyDataSourcePrefix); for (int i = 0; i < dsNames.Length; i++) { PropertiesParser pp = new PropertiesParser( cfg.GetPropertyGroup(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", PropertyDataSourcePrefix, dsNames[i]), true)); string cpType = pp.GetStringProperty(PropertyDbProviderType, null); // custom connectionProvider... if (cpType != null) { IDbProvider cp; try { cp = (IDbProvider) ObjectUtils.InstantiateType(loadHelper.LoadType(cpType)); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "ConnectionProvider of type '{0}' could not be instantiated.", cpType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } try { // remove the type name, so it isn't attempted to be set pp.UnderlyingProperties.Remove(PropertyDbProviderType); ObjectUtils.SetObjectProperties(cp, pp.UnderlyingProperties); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "ConnectionProvider type '{0}' props could not be configured.", cpType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } dbMgr = DBConnectionManager.Instance; dbMgr.AddConnectionProvider(dsNames[i], cp); } else { string dsProvider = pp.GetStringProperty(PropertyDataSourceProvider, null); string dsConnectionString = pp.GetStringProperty(PropertyDataSourceConnectionString, null); string dsConnectionStringName = pp.GetStringProperty(PropertyDataSourceConnectionStringName, null); if (dsConnectionString == null && dsConnectionStringName != null && dsConnectionStringName.Length > 0) { #if NET_20 ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings[dsConnectionStringName]; if (connectionStringSettings == null) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Named connection string '{0}' not found for DataSource: {1}", dsConnectionStringName, dsNames[i])); throw initException; } dsConnectionString = connectionStringSettings.ConnectionString; #endif } if (dsProvider == null) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Provider not specified for DataSource: {0}", dsNames[i])); throw initException; } if (dsConnectionString == null) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Connection string not specified for DataSource: {0}", dsNames[i])); throw initException; } try { DbProvider dbp = new DbProvider(dsProvider, dsConnectionString); dbMgr = DBConnectionManager.Instance; dbMgr.AddConnectionProvider(dsNames[i], dbp); } catch (Exception exception) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Could not Initialize DataSource: {0}", dsNames[i]), exception); throw initException; } } } // Get JobStore Properties // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string jsType = cfg.GetStringProperty(PropertyJobStoreType, typeof (RAMJobStore).FullName); if (jsType == null) { initException = new SchedulerException("JobStore type not specified. ", SchedulerException.ErrorBadConfiguration); throw initException; } try { js = (IJobStore) ObjectUtils.InstantiateType(loadHelper.LoadType(jsType)); } catch (Exception e) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "JobStore of type '{0}' could not be instantiated.", jsType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } tProps = cfg.GetPropertyGroup(PropertyJobStorePrefix, true, new string[] {PropertyJobStoreLockHandlerPrefix}); try { ObjectUtils.SetObjectProperties(js, tProps); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "JobStore type '{0}' props could not be configured.", jsType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } if (js is JobStoreSupport) { ((JobStoreSupport) js).InstanceId = schedInstId; ((JobStoreSupport) js).InstanceName = schedName; // Install custom lock handler (Semaphore) string lockHandlerTypeName = cfg.GetStringProperty(PropertyJobStoreLockHandlerType); if (lockHandlerTypeName != null) { try { Type lockHandlerType = loadHelper.LoadType(lockHandlerTypeName); ISemaphore lockHandler; ConstructorInfo cWithDbProvider = lockHandlerType.GetConstructor(new Type[] {typeof (DbProvider)}); if (cWithDbProvider != null) { // takes db provider IDbProvider dbProvider = DBConnectionManager.Instance.GetDbProvider(((JobStoreSupport) js).DataSource); lockHandler = (ISemaphore) cWithDbProvider.Invoke(new object[] { dbProvider }); } else { lockHandler = (ISemaphore)ObjectUtils.InstantiateType(lockHandlerType); } tProps = cfg.GetPropertyGroup(PropertyJobStoreLockHandlerPrefix, true); // If this lock handler requires the table prefix, add it to its properties. if (lockHandler is ITablePrefixAware) { tProps[PropertyTablePrefix] = ((JobStoreSupport) js).TablePrefix; } try { ObjectUtils.SetObjectProperties(lockHandler, tProps); } catch (Exception e) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "JobStore LockHandler type '{0}' props could not be configured.", lockHandlerTypeName), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } ((JobStoreSupport) js).LockHandler = lockHandler; Log.Info("Using custom data access locking (synchronization): " + lockHandlerType); } catch (Exception e) { initException = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "JobStore LockHandler type '{0}' could not be instantiated.", lockHandlerTypeName), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } } } // Set up any SchedulerPlugins // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string[] pluginNames = cfg.GetPropertyGroups(PropertyPluginPrefix); ISchedulerPlugin[] plugins = new ISchedulerPlugin[pluginNames.Length]; for (int i = 0; i < pluginNames.Length; i++) { NameValueCollection pp = cfg.GetPropertyGroup(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", PropertyPluginPrefix, pluginNames[i]), true); string plugInType = pp[PropertyPluginType] == null ? null : pp[PropertyPluginType]; if (plugInType == null) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "SchedulerPlugin type not specified for plugin '{0}'", pluginNames[i]), SchedulerException.ErrorBadConfiguration); throw initException; } ISchedulerPlugin plugin; try { plugin = (ISchedulerPlugin) ObjectUtils.InstantiateType(LoadType(plugInType)); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "SchedulerPlugin of type '{0}' could not be instantiated.", plugInType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } try { ObjectUtils.SetObjectProperties(plugin, pp); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "JobStore SchedulerPlugin '{0}' props could not be configured.", plugInType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } plugins[i] = plugin; } // Set up any JobListeners // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Type[] strArg = new Type[] {typeof (string)}; string[] jobListenerNames = cfg.GetPropertyGroups(PropertyJobListenerPrefix); IJobListener[] jobListeners = new IJobListener[jobListenerNames.Length]; for (int i = 0; i < jobListenerNames.Length; i++) { NameValueCollection lp = cfg.GetPropertyGroup(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", PropertyJobListenerPrefix, jobListenerNames[i]), true); string listenerType = lp[PropertyListenerType] == null ? null : lp[PropertyListenerType]; if (listenerType == null) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "JobListener type not specified for listener '{0}'", jobListenerNames[i]), SchedulerException.ErrorBadConfiguration); throw initException; } IJobListener listener; try { listener = (IJobListener) ObjectUtils.InstantiateType(loadHelper.LoadType(listenerType)); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "JobListener of type '{0}' could not be instantiated.", listenerType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } try { MethodInfo nameSetter = listener.GetType().GetMethod("setName", (strArg == null) ? new Type[0] : strArg); if (nameSetter != null) { nameSetter.Invoke(listener, new object[] {jobListenerNames[i]}); } ObjectUtils.SetObjectProperties(listener, lp); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "JobListener '{0}' props could not be configured.", listenerType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } jobListeners[i] = listener; } // Set up any TriggerListeners // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string[] triggerListenerNames = cfg.GetPropertyGroups(PropertyTriggerListenerPrefix); ITriggerListener[] triggerListeners = new ITriggerListener[triggerListenerNames.Length]; for (int i = 0; i < triggerListenerNames.Length; i++) { NameValueCollection lp = cfg.GetPropertyGroup( string.Format(CultureInfo.InvariantCulture, "{0}.{1}", PropertyTriggerListenerPrefix, triggerListenerNames[i]), true); string listenerType = lp[PropertyListenerType] == null ? null : lp[PropertyListenerType]; if (listenerType == null) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "TriggerListener type not specified for listener '{0}'", triggerListenerNames[i]), SchedulerException.ErrorBadConfiguration); throw initException; } ITriggerListener listener; try { listener = (ITriggerListener) ObjectUtils.InstantiateType(loadHelper.LoadType(listenerType)); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "TriggerListener of type '{0}' could not be instantiated.", listenerType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } try { MethodInfo nameSetter = listener.GetType().GetMethod("setName", (strArg == null) ? new Type[0] : strArg); if (nameSetter != null) { nameSetter.Invoke(listener, (new object[] {triggerListenerNames[i]})); } ObjectUtils.SetObjectProperties(listener, lp); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "TriggerListener '{0}' props could not be configured.", listenerType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } triggerListeners[i] = listener; } // Get exporter // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ string exporterType = cfg.GetStringProperty(PropertySchedulerExporterType, null); if (exporterType != null) { try { exporter = (ISchedulerExporter)ObjectUtils.InstantiateType(loadHelper.LoadType(exporterType)); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "Scheduler exporter of type '{0}' could not be instantiated.", exporterType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } tProps = cfg.GetPropertyGroup(PropertySchedulerExporterPrefix, true); try { ObjectUtils.SetObjectProperties(exporter, tProps); } catch (Exception e) { initException = new SchedulerException( string.Format(CultureInfo.InvariantCulture, "Scheduler exporter type '{0}' props could not be configured.", exporterType), e); initException.ErrorCode = SchedulerException.ErrorBadConfiguration; throw initException; } } // Fire everything up // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ IJobRunShellFactory jrsf = new StdJobRunShellFactory(); if (autoId) { try { schedInstId = DefaultInstanceId; if (js is JobStoreSupport) { if (((JobStoreSupport) js).Clustered) { schedInstId = instanceIdGenerator.GenerateInstanceId(); } } } catch (Exception e) { Log.Error("Couldn't generate instance Id!", e); throw new SystemException("Cannot run without an instance id."); } } if (js is JobStoreSupport) { JobStoreSupport jjs = (JobStoreSupport) js; jjs.InstanceId = schedInstId; jjs.DbRetryInterval = dbFailureRetry; } QuartzSchedulerResources rsrcs = new QuartzSchedulerResources(); rsrcs.Name = schedName; rsrcs.ThreadName = threadName; rsrcs.InstanceId = schedInstId; rsrcs.JobRunShellFactory = jrsf; rsrcs.MakeSchedulerThreadDaemon = makeSchedulerThreadDaemon; rsrcs.ThreadPool = tp; rsrcs.SchedulerExporter = exporter; if (tp is SimpleThreadPool) { ((SimpleThreadPool) tp).ThreadNamePrefix = schedName + "_Worker"; } tp.Initialize(); rsrcs.JobStore = js; // add plugins for (int i = 0; i < plugins.Length; i++) { rsrcs.AddSchedulerPlugin(plugins[i]); } schedCtxt = new SchedulingContext(); schedCtxt.InstanceId = rsrcs.InstanceId; qs = new QuartzScheduler(rsrcs, schedCtxt, idleWaitTime, dbFailureRetry); // Create Scheduler ref... IScheduler sched = Instantiate(rsrcs, qs); // set job factory if specified if (jobFactory != null) { qs.JobFactory = jobFactory; } // Initialize plugins now that we have a Scheduler instance. for (int i = 0; i < plugins.Length; i++) { plugins[i].Initialize(pluginNames[i], sched); } // add listeners for (int i = 0; i < jobListeners.Length; i++) { qs.AddGlobalJobListener(jobListeners[i]); } for (int i = 0; i < triggerListeners.Length; i++) { qs.AddGlobalTriggerListener(triggerListeners[i]); } // set scheduler context data... IEnumerator itr = new HashSet(schedCtxtProps).GetEnumerator(); while (itr.MoveNext()) { string key = (String) itr.Current; string val = schedCtxtProps.Get(key); sched.Context.Put(key, val); } // fire up job store, and runshell factory js.Initialize(loadHelper, qs.SchedulerSignaler); jrsf.Initialize(sched, schedCtxt); Log.Info(string.Format(CultureInfo.InvariantCulture, "Quartz scheduler '{0}' initialized", sched.SchedulerName)); Log.Info(string.Format(CultureInfo.InvariantCulture, "Quartz scheduler version: {0}", qs.Version)); // prevents the repository from being garbage collected qs.AddNoGCObject(schedRep); // prevents the db manager from being garbage collected if (dbMgr != null) { qs.AddNoGCObject(dbMgr); } schedRep.Bind(sched); return sched; }
/// <summary> /// Schedules the with the provided <see cref="IJobDetail"/>. /// </summary> /// <param name="sched">The sched.</param> /// <param name="jobDetail">The job detail.</param> public void ScheduleWithJob(IScheduler sched, IJobDetail jobDetail) { RegisterCalendars(sched); if (RegisterGroups.Count > 1) // use the bulk add method if using more than 1 { List<ITrigger> triggers = new List<ITrigger>(); RegisterGroups.ForEach(x => triggers.Add(x.TriggerBuilder.Build())); var dic = new Dictionary<Quartz.IJobDetail, Quartz.Collection.ISet<ITrigger>>(); Quartz.Collection.ISet<ITrigger> set = new Collection.HashSet<ITrigger>(triggers); dic.Add(jobDetail, set); sched.ScheduleJobs(dic, true); } else if (RegisterGroups.Count == 1) { ITrigger trigger = RegisterGroups[0].TriggerBuilder.Build(); sched.ScheduleJob(jobDetail, trigger); } }
/// <summary> /// Selects the paused trigger groups. /// </summary> /// <param name="conn">The DB Connection.</param> /// <returns></returns> public virtual ISet SelectPausedTriggerGroups(ConnectionAndTransactionHolder conn) { HashSet retValue = new HashSet(); using (IDbCommand cmd = PrepareCommand(conn, ReplaceTablePrefix(SqlSelectPausedTriggerGroups))) { using (IDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { string groupName = (string)dr[ColumnTriggerGroup]; retValue.Add(groupName); } } return retValue; } }
/// <summary> /// Select the distinct instance names of all fired-trigger records. /// </summary> /// <param name="conn">The conn.</param> /// <returns></returns> /// <remarks> /// This is useful when trying to identify orphaned fired triggers (a /// fired trigger without a scheduler state record.) /// </remarks> public ISet SelectFiredTriggerInstanceNames(ConnectionAndTransactionHolder conn) { ISet instanceNames = new HashSet(); using (IDbCommand cmd = PrepareCommand(conn, ReplaceTablePrefix(SqlSelectFiredTriggerInstanceNames))) { using (IDataReader rs = cmd.ExecuteReader()) { while (rs.Read()) { instanceNames.Add(rs[ColumnInstanceName]); } return instanceNames; } } }
/// <summary> /// Get the names of all of the <see cref="Trigger" /> groups. /// </summary> public virtual string[] GetTriggerGroupNames(SchedulingContext ctxt) { string[] outList; lock (triggerLock) { outList = new string[triggersByGroup.Count]; int outListPos = 0; IEnumerator keys = new HashSet(triggersByGroup.Keys).GetEnumerator(); while (keys.MoveNext()) { outList[outListPos++] = ((string) keys.Current); } } return outList; }
/// <summary> /// Get the names of all of the <see cref="Trigger" /> s /// that have the given group name. /// </summary> public virtual string[] GetTriggerNames(SchedulingContext ctxt, string groupName) { string[] outList; IDictionary grpMap = (Hashtable) triggersByGroup[groupName]; if (grpMap != null) { lock (triggerLock) { outList = new string[grpMap.Count]; int outListPos = 0; IEnumerator keys = new HashSet(grpMap.Keys).GetEnumerator(); while (keys.MoveNext()) { string key = (string) keys.Current; TriggerWrapper tw = (TriggerWrapper) grpMap[key]; if (tw != null) { outList[outListPos++] = tw.trigger.Name; } } } } else { outList = new string[0]; } return outList; }
/// <seealso cref="IJobStore.GetPausedTriggerGroups(SchedulingContext)" /> public virtual ISet GetPausedTriggerGroups(SchedulingContext ctxt) { HashSet data = new HashSet(); data.AddAll(pausedTriggerGroups); return data; }
/// <summary> /// Peeks the triggers. /// </summary> /// <returns></returns> protected internal virtual string PeekTriggers() { StringBuilder str = new StringBuilder(); TriggerWrapper tw; lock (triggerLock) { IEnumerator itr = new HashSet(triggersByFQN.Keys).GetEnumerator(); while (itr.MoveNext()) { tw = (TriggerWrapper) triggersByFQN[itr.Current]; str.Append(tw.trigger.Name); str.Append("/"); } } str.Append(" | "); lock (triggerLock) { IEnumerator itr = timeTriggers.GetEnumerator(); while (itr.MoveNext()) { tw = (TriggerWrapper) itr.Current; str.Append(tw.trigger.Name); str.Append("->"); } } return str.ToString(); }