/// <summary> /// 添加服务项 /// </summary> public void AddServiceJob(ServiceJobDescriptor item) { // 传递数据的map var map = new JobDataMap(); var now = DateTime.Now; // 添加数据映射 map.Add(GlobalConst.__ServiceDataMapKey, item); // 绑定job的详细信息,以及执行容器PolicyWrapper var job = JobBuilder.Create <QuartzServiceWrapper>() .WithIdentity(item.Id) .SetJobData(map) .Build(); // 启动时间 DateTime?firstDateTime = null; // 存在有效开始日期 if (item.ValidStartTime.HasValue && DateTime.Compare(item.ValidStartTime.Value, now) > 0) { firstDateTime = item.ValidStartTime; } else { firstDateTime = DateTime.Now.AddMinutes(1); } // 绑定job的执行计划 var trigger = TriggerBuilder.Create() .WithIdentity("trigger" + item.Id) // 表达式启动 .WithCronSchedule(item.CronExpression); trigger.StartAt(firstDateTime.Value); if (item.ValidEndTime.HasValue) { trigger = trigger.EndAt(item.ValidEndTime.Value); } //重复次数 if (item.RepeatCount != null) { trigger.WithSimpleSchedule(x => x.WithRepeatCount(item.RepeatCount.Value)); } _scheduler.ScheduleJob(job, trigger.Build()); }
/// <summary> /// 当前执行的任务 /// </summary> /// <param name="descriptor">服务描述</param> public void Excute(ServiceJobDescriptor descriptor) { var workContext = Singleton <IEngine> .Instance.Resolve <IWorkContext>(); var unitRepository = Singleton <IEngine> .Instance.Resolve <IUnitRepository>(); var mqInfo = unitRepository.GetMessageRouteByBusinessKey(MessageQueueKey.__TestKey); var sendMessage = string.Format("CurrentEmplId = {0} , DBEmployeeCount = {1}", workContext.Current.EmplId, unitRepository.AllNoTracking <Sys_Org_Employee>().Count()); Singleton <IEngine> .Instance.Resolve <IMessagePublisher>().Send(mqInfo, new NGPSingleRequest { RequestData = sendMessage }); var msg = string.Format("ThreadId : {0}\n, SendMessage : {1}\n SendTime : {2}\n", System.Threading.Thread.CurrentThread.ManagedThreadId, sendMessage, DateTime.Now); Console.WriteLine(msg); }