/// <summary> /// 启用任务调度 /// 启动调度时会把任务表中状态为“执行中”的任务加入到任务调度队列中 /// </summary> public void StartScheduler() { try { if (_scheduler.IsStarted) { return; } //添加全局监听 _scheduler.ListenerManager.AddTriggerListener(new CustomTriggerListener(), GroupMatcher <TriggerKey> .AnyGroup()); _scheduler.Start(); //获取所有执行中的任务 foreach (var taskConfig in _taskConfigs) { try { ScheduleJob(taskConfig); } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } } ZilLionLoger.WriteTraceLog($"scheduler:{_schedulerKey}成功启动!"); } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } }
/// <summary> /// 立即运行一次任务 /// </summary> /// <param name="jobKey">任务key</param> public void RunOnceTask(string jobKey) { var jk = new JobKey(jobKey); if (!_scheduler.CheckExists(jk)) { return; } var jobDetail = _scheduler.GetJobDetail(jk); var type = jobDetail.JobType; var instance = type.FastNew(); var method = type.GetMethod("Execute"); method.Invoke(instance, new object[] { null }); ZilLionLoger.WriteTraceLog($"任务“{jobKey}”立即运行"); }