/// <summary> /// 创建作业实例 /// (写入数据库及添加至作业调度池中) /// </summary> /// <param name="model"></param> /// <returns></returns> public PMS.Model.Message.IBaseResponse AddJobInfo(J_JobInfo model, IJobData jobData=null) { //1 创建与UserInfo的关系 var user= this.CurrentDBSession.UserInfoDAL.GetListBy(u => u.ID == model.UID).FirstOrDefault(); //12月9日 //注意此处不要将user转成中间变量,否则会创建一个新的user对象该userInfo表中 //model.UserInfoes.Add(user.ToMiddleModel()); model.UserInfoes.Add(user); //2 创建J_JobInfo对象 // 1 添加作业至调度池中 if(jobData==null) jobData = new PMS.Model.JobDataModel.SendJobDataModel(); model.JobState = (int)(PMS.Model.Enum.JobState_Enum.WAITING); base.Create(model); //var response = ijobService.AddScheduleJob(model, jobData); var response = client_quartzProxy.AddScheduleJob(model, jobData as PMS.Model.JobDataModel.SendJobDataModel); //object response_wcf= jobServiceClient.AddScheduleJob(model.ToMiddleModel(), jobData); //var response= response_wcf as Model.Message.IBaseResponse; //client.AddScheduleJob(model.ToMiddleModel(), jobData); //2 写入jobInfo表作业的状态 if (response.Success) { model.JobState = (int)(PMS.Model.Enum.JobState_Enum.WAITING); base.Update(model); } //2 根据传入的JobInfo创建指定的作业 //if (response.Success == true) //{ // base.Create(model); //} //if (base.Create(model)) //{ // return true; //} return response; }
static void Main(string[] args) { //方式一:直接调用服务 #region 直接调用服务 //ServiceReference_QuartzService.IJobService jobSer = new ServiceReference_QuartzService.JobServiceClient(); //J_JobInfo job = new J_JobInfo() //{ // JobName = "1234", // JobGroup = "quartz", // JobClassName = "QueryJob", // StartRunTime = DateTime.Now.AddSeconds(2), // EndRunTime = DateTime.Now.AddSeconds(3), // UID = 3 //}; //QuartzJobFactory.IJobService jobClass = new QuartzJobFactory.JobService(); //ServiceReference_QuartzService.JobServiceClient client = new ServiceReference_QuartzService.JobServiceClient(); //client.AddScheduleJob(job, new PMS.Model.JobDataModel.SendJobDataModel()); //jobClass.AddScheduleJob(job, null); //jobSer.AddScheduleJob(job, new PMS.Model.JobDataModel.SendJobDataModel()); #endregion //方式二:使用代理类的方式 QuartzProxy.QuartzServiceFacade quartzService = new QuartzProxy.QuartzServiceFacade(new QuartzProxy.QuartzServiceClientProxy()); J_JobInfo jobInstance = new J_JobInfo() { JobName = "1234", JobGroup = "quartz", JobClassName = "QueryJob", StartRunTime = DateTime.Now.AddSeconds(2), EndRunTime = DateTime.Now.AddSeconds(3), UID = 3 }; PMS.Model.JobDataModel.SendJobDataModel jobData = new PMS.Model.JobDataModel.SendJobDataModel() { JobDataValue = new PMS.Model.CombineModel.SendAndMessage_Model() { Model_Send = new SMSModel_Send() { }, Model_Message = new PMS.Model.ViewModel.ViewModel_Message() { } } }; var response_base = quartzService.AddScheduleJob(jobInstance, jobData); }
/// <summary> /// 添加任务计划 /// </summary> /// <returns></returns> public bool AddScheduleJob(J_JobInfo m) { try { if (m != null) { if (m.StartRunTime == null) { m.StartRunTime = DateTime.Now; } DateTimeOffset starRunTime = DateBuilder.NextGivenSecondDate(m.StartRunTime, 1); if (m.EndRunTime == null) { m.EndRunTime = DateTime.MaxValue.AddDays(-1); } DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(m.EndRunTime, 1); scheduler = GetScheduler(); IJobDetail job = JobBuilder.Create<HttpJob>() .WithIdentity(m.JobName, m.JobGroup) .Build(); ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .StartAt(starRunTime) .EndAt(endRunTime) .WithIdentity(m.JobName, m.JobGroup) .WithCronSchedule(m.CronStr) .Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start(); //StopScheduleJob(m.JobValue, m.JobName); return true; } return false; } catch (Exception ex) { //DoApplication.WriteLogFile(ex.Message + "\r\n" + ex.StackTrace); return false; } }
/// <summary> /// 恢复指定的作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse ResumeTargetJob(J_JobInfo job) { IBaseResponse response = new BaseResponse() { Success = false }; try { if (!sche.IsStarted) { sche.Start(); } sche.ResumeJob(new JobKey(job.JID.ToString(), job.JobGroup)); response.Success = true; response.Message = string.Format("job:{0},group:{1}已恢复", job.JobName, job.JobGroup); } catch (Exception) { response.Success = false; response.Message = string.Format("job:{0},group:{1}恢复时出错", job.JobName, job.JobGroup); } //var scheduler_temp = new SchedulerFactory().GetScheduler(); return response; }
/// <summary> /// 终止指定作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse RemoveJob(J_JobInfo job) { IBaseResponse response = new BaseResponse() { Success = false }; try { sche.PauseJob(new JobKey(job.JID.ToString(), job.JobGroup)); //1 取出指定的触发器 var trigger = new TriggerKey(job.JID.ToString(), job.JobGroup); //2 先暂停触发器(试一下若不暂停触发器可否?——11月20日) sche.PauseTrigger(trigger); //3 调度中的该方法只能传入触发器 sche.UnscheduleJob(trigger); response.Success = true; response.Message = string.Format("job:{0},group{1}已终止工作", job.JobName, job.JobGroup); } catch (Exception) { response.Message = string.Format("job:{0},group{1}终止工作时出错", job.JobName, job.JobGroup); } return response; }
/// <summary> /// 删除某个作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse RemovceJob(J_JobInfo job) { IBaseResponse response = new BaseResponse() { Success = false }; try { var trigger = new TriggerKey(job.JID.ToString(), job.JobGroup); sche.PauseJob(new JobKey(job.JID.ToString(), job.JobGroup)); sche.UnscheduleJob(trigger); sche.DeleteJob(new JobKey(job.JID.ToString(), job.JobGroup)); response.Success = true; response.Message = string.Format("job:{0},group{1}已删除工作", job.JobName, job.JobGroup); } catch (Exception) { response.Message = string.Format("job:{0},group{1}删除工作时出错", job.JobName, job.JobGroup); } return response; }
/// <summary> /// 暂停某个作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse PauseJob(J_JobInfo job) { IBaseResponse response = new BaseResponse() { Success = false }; try { sche.PauseJob(new JobKey(job.JID.ToString(), job.JobGroup)); LogHelper.WriteLog(string.Format("{0}已恢复作业{1}-{2}(所属{3})", job.CreateUser, job.JID, job.JobName, job.JobGroup)); response.Success = true; response.Message = string.Format("job:{0},group{1}已暂停工作", job.JobName, job.JobGroup); } catch (Exception ex) { LogHelper.WriteError("作业恢复错误", ex); response.Message = string.Format("job:{0},group{1}暂停工作时出错", job.JobName, job.JobGroup); } return response; }
/// <summary> /// 根据工作对象 添加任务计划 /// 作业需要含UID /// </summary> /// <param name="jobInfo">作业(含UID)</param> /// <param name="data_temp">向作业调度中传的临时数据</param> /// <returns></returns> public IBaseResponse AddScheduleJob(J_JobInfo jobInfo,IJobData data_temp) { //1 根据Job的类名通过反射的方式创建IJobDetial var job = JobFactory.CreateJobInstance(jobInfo, data_temp); IBaseResponse response = new BaseResponse() { Success = false }; if (job == null) { response.Message = string.Format("创建作业实例时出错"); } //2 创建定时器 var trigger = JobFactory.CreateTrigger(jobInfo); //3 将定时器加入job中 //var sche = new SchedulerFactory().GetScheduler(); sche.ScheduleJob(job, trigger); try { //4 启动工作 sche.Start(); LogHelper.WriteLog(string.Format("{0}创建的作业{1}-{2}(所属{3})已添加至调度池中", jobInfo.CreateUser, jobInfo.JID, jobInfo.JobName, jobInfo.JobGroup)); } catch (Exception ex) { LogHelper.WriteError("作业添加错误",ex); throw; } response.Success = true; response.Message = string.Format("作业已添加至调度池中"); return response; }
/// <summary> /// 延时发送 /// </summary> /// <param name="msg"></param> /// <returns></returns> public bool SendMsgbyDelayed(PMS.Model.CombineModel.SendAndMessage_Model model,out SMSModel_Receive response) { //response = new PMS.Model.Message.BaseResponse(); //1 创建quartz父类客户端 // 不使用服务因为此处需要通过反射的方式创建作业实例 //Quartz_Service.JobServiceClient client = new Quartz_Service.JobServiceClient(); //ServiceReference_QuartzService.JobServiceClient client = new ServiceReference_QuartzService.JobServiceClient(); QuartzJobFactory.IJobService client = new QuartzJobFactory.JobService(); //2 创建发送作业实例(非模板) /*此处需要实现: 1)向数据库写入创建的新的作业实例 2)创建该作业实例的关联表 */ //var jobTemplateInstance= jobTemplateBLL.GetListBy(t => t.JTID == 3); //jobInfoBLL.Create(new J_JobInfo() { JobName = "发送作业", JobClassName = "SendJob", NextRunTime=model.Model_Message.NextRunTime }); //找到对应的作业模板(发送作业模板) //2.1 查找当前用户 var user_current= userInfoBLL.GetListBy(u =>u.ID== model.Model_Message.UID).FirstOrDefault(); //2.2 根据当前用户找到指定类型(jobType)的作业模板 var jobTemplate_target = (from t in user_current.J_JobTemplate where t.JobType == Convert.ToInt32(PMS.Model.Enum.JobType_Enum.sendJob) select t).FirstOrDefault(); //2.3 根据作业模板创建作业实例 // 调用J_JobInfoBLL中的AddJobInfo方法创建作业实例 J_JobInfo jobInstance = new J_JobInfo() { UID = model.Model_Message.UID, CreateTime = DateTime.Now, EndRunTime = model.Model_Message.EndRunTime == DateTime.MinValue ? model.Model_Message.StartRunTime : model.Model_Message.EndRunTime,//此处加入判断,若EndRunTime时间为1/1/1/1这种情况先将起始时间赋给他 NextRunTime = model.Model_Message.NextRunTime == DateTime.MinValue ? model.Model_Message.StartRunTime : model.Model_Message.NextRunTime, StartRunTime = model.Model_Message.StartRunTime, JobClassName = jobTemplate_target.JobClassName, JobName = jobTemplate_target.JTName, JobGroup = jobTemplate_target.JobGroup }; //执行以下操作 //var jobInstance = jobInfoBLL.GetListBy(j => j.JID == 27).FirstOrDefault(); //3 创建JobData var jobData = new PMS.Model.JobDataModel.SendJobDataModel() { //4 将前台传入的model值赋给JobData中的JobValue JobDataValue=new PMS.Model.CombineModel.SendAndMessage_Model() { Model_Send=new SMSModel_Send() { account = model.Model_Send.account, content = model.Model_Send.content, msgid = model.Model_Send.msgid, password = model.Model_Send.password, phones = model.Model_Send.phones, sendtime = model.Model_Send.sendtime, subcode = model.Model_Send.subcode }, Model_Message =new PMS.Model.ViewModel.ViewModel_Message() { UID = model.Model_Message.UID, Content=model.Model_Send.content, SMSMissionID=model.Model_Message.SMSMissionID } } //JobDataValue = new PMS.Model.SMSModel.SMSModel_Send() //{ // account = model.Model_Send.account, // content = model.Model_Send.content, // msgid = model.Model_Send.msgid, // password = model.Model_Send.password, // phones = model.Model_Send.phones, // sendtime = model.Model_Send.sendtime, // subcode = model.Model_Send.subcode //} }; //5 将发送作业实例添加至计划任务中 //注意此作业实例中需要含UID jobInfoBLL.AddJobInfo(jobInstance, jobData); //**** 11-16 创建jobInfo与其他的关联(以便在作业管理页面中显示) //在job的bll层中创建作业(同时写入数据库,并添加至调度池中) /*var response=*/ //12-6 //统一写在JobInfoBLL层中的AddJobInfo方法中 //QuartzProxy.QuartzServiceFacade quartzService = new QuartzProxy.QuartzServiceFacade(new QuartzProxy.QuartzServiceClientProxy()); //var response_base= quartzService.AddScheduleJob(jobInstance, jobData); //client.AddScheduleJob(jobInstance, jobData); response = new SMSModel_Receive() { result="0"}; return true; }
public void Execute(IJobExecutionContext context) { ThreadPool.QueueUserWorkItem(delegate (Object o) { IJ_JobInfoBLL jobBLL = new J_JobInfoBLL(); try { //DoApplication.WriteLogFile(context.JobDetail.Key.Group + "---" + context.JobDetail.Key.Name + "---" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---" + context.NextFireTimeUtc.Value.DateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss")); //jobBLL.GetListBy //var sm = new WJ_ScheduleManage().GetScheduleModel(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name }); // J_JobInfo jobModel = new J_JobInfo() { JobName = context.JobDetail.Key.Name, //JobValue=context.JobDetail.Key.Group, //State=1,//执行中 }; jobBLL.Create(jobModel); //new WJ_ScheduleManage().UpdateScheduleRunStatus(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, RunStatus = (int)ADJ.Job.Entity.EnumType.JobRunStatus.执行中 }); //ESBRequest req = new ESBRequest(sm.ServiceCode, sm.ApiCode); //DataResult result = req.Request(); //new WJ_ScheduleManage().UpdateScheduleRunStatus(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, RunStatus = (int)ADJ.Job.Entity.EnumType.JobRunStatus.待运行 }); //if (result.Code == 1) //{ // #region 加入执行明细 // WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity(); // dm.ActionDescribe = "执行完成:" + result.Message; // dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行完成; // dm.CreateTime = DateTime.Now; // dm.JobGroup = context.JobDetail.Key.Group; // dm.JobName = context.JobDetail.Key.Name; // dm.IsSuccess = 1; // new WJ_ScheduleManage().AddScheduleDetails(dm); // #endregion //} //else //{ // #region 加入执行明细 // WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity(); // dm.ActionDescribe = "执行任务计划中,执行计划过程出错." + result.Message; // dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行任务计划中; // dm.CreateTime = DateTime.Now; // dm.JobGroup = context.JobDetail.Key.Group; // dm.JobName = context.JobDetail.Key.Name; // dm.IsSuccess = 0; // new WJ_ScheduleManage().AddScheduleDetails(dm); // #endregion //} //new WJ_ScheduleManage().UpdateScheduleNextTime(new WJ_ScheduleEntity() { JobGroup = context.JobDetail.Key.Group, JobName = context.JobDetail.Key.Name, NextTime = context.NextFireTimeUtc.Value.DateTime.AddHours(8) }); } catch (Exception ex) { //#region 加入执行明细 //WJ_ScheduleDetailsEntity dm = new WJ_ScheduleDetailsEntity(); //dm.ActionDescribe = "执行任务计划中,执行计划过程出错:" + ex.Message + "/r/n" + ex.StackTrace; //dm.ActionStep = (int)ADJ.Job.Entity.EnumType.JobStep.执行任务计划中; //dm.CreateTime = DateTime.Now; //dm.JobGroup = context.JobDetail.Key.Group; //dm.JobName = context.JobDetail.Key.Name; //dm.IsSuccess = 0; //new WJ_ScheduleManage().AddScheduleDetails(dm); //#endregion //DoApplication.WriteLogFile(ex.Message + "\r\n" + ex.StackTrace); } }); }
/// <summary> /// 恢复指定作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse ResumeTargetJob(J_JobInfo job) { return jobService.ResumeTargetJob(job.ToMiddleModel()) as IBaseResponse; }
/// <summary> /// 暂停指定作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse PauseJob(J_JobInfo job) { return jobService.PauseJob(job.ToMiddleModel()) as IBaseResponse; }
/// <summary> /// 添加作业 /// </summary> /// <param name="jobInfo"></param> /// <param name="data_temp"></param> /// <returns></returns> public IBaseResponse AddScheduleJob(J_JobInfo jobInfo, PMS.Model.JobDataModel.SendJobDataModel data_temp) { return jobService.AddScheduleJob(jobInfo.ToMiddleModel(),data_temp) as IBaseResponse; }
/// <summary> /// 暂停某个作业 /// </summary> /// <param name="job"></param> /// <returns></returns> public IBaseResponse PauseJob(J_JobInfo job) { IBaseResponse response = new BaseResponse() { Success = false }; try { sche.PauseJob(new JobKey(job.JID.ToString(), job.JobGroup)); response.Success = true; response.Message = string.Format("job:{0},group{1}已暂停工作", job.JobName, job.JobGroup); } catch (Exception ex) { response.Message = string.Format("job:{0},group{1}暂停工作时出错", job.JobName, job.JobGroup); } return response; }
/// <summary> /// 根据工作对象 添加任务计划 /// 作业需要含UID /// </summary> /// <param name="jobInfo">作业(含UID)</param> /// <param name="data_temp">向作业调度中传的临时数据</param> /// <returns></returns> public IBaseResponse AddScheduleJob(J_JobInfo jobInfo, PMS.Model.JobDataModel.SendJobDataModel data_temp) { //1 根据Job的类名通过反射的方式创建IJobDetial var job = JobFactory.CreateJobInstance(jobInfo, data_temp); IBaseResponse response = new BaseResponse() { Success = false }; if (job == null) { response.Message = string.Format("创建作业实例时出错"); } //2 创建定时器 var trigger = JobFactory.CreateTrigger(jobInfo); //3 将定时器加入job中 //var sche = new SchedulerFactory().GetScheduler(); try { sche.ScheduleJob(job, trigger); //4 启动工作 sche.Start(); response.Success = true; response.Message = string.Format("作业已添加至调度池中"); } catch (Exception ex) { response.Success = false; response.Message = string.Format("作业添加至调度池时出错"); } return response; }
public IBaseResponse AddScheduleJob(J_JobInfo jobInfo, IJobData data_temp) { IBaseResponse response = new BaseResponse() { Success = false }; return response; }