You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.7 KiB
C#

using Newtonsoft.Json.Linq;
using Quartz;
using Quartz.Impl;
using System.Threading;
using ZKLT.Hadoop.Interface;
using ZKLT.Hadoop.Model;
using ZKLT.Quartz.Interface;
using ZKLT.Quartz.Job;
using ZKLT.Quartz.Model;
namespace ZKLT.Quartz
{
public class QuartzService : IQuartzService
{
private IScheduler _Scheduler;
private IHadoopService _HadoopService;
public QuartzService(IHadoopService hadoopService)
{
_HadoopService = hadoopService;
//实例化调度器
_Scheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
_Scheduler.Start();
}
public void CreateHttpJob(QZ_JobParams jobParams)
{
//生成jobDataMap
JobDataMap jobDataMap = MakeJobDataMap(jobParams);
//创建HTTP作业 Id作为名称
IJobDetail job = JobBuilder.Create<HttpJob>()
.WithIdentity("j" + jobParams.TaskId, HDP_Task.HTTPTASK)
.UsingJobData(jobDataMap)
.Build();
//创建触发器
ITrigger trigger = TriggerBuilder.Create().WithIdentity("t" + jobParams.TaskId, HDP_Task.HTTPTASK).StartNow()
.WithCronSchedule(jobParams.CronTime)
.Build();
// 把作业,触发器加入调度器
_Scheduler.ScheduleJob(job, trigger);
}
private JobDataMap MakeJobDataMap(QZ_JobParams jobParams)
{
JobDataMap jobDataMap = new JobDataMap();
Type jobType = jobParams.GetType();
foreach (var prop in jobType.GetProperties())
{
var propValue = prop.GetValue(jobParams);
// 过滤掉 null 值
if (propValue == null) continue;
jobDataMap.Add(prop.Name, propValue);
}
jobDataMap.Add("hadoop", _HadoopService);
return jobDataMap;
}
public void CreateSqlJob()
{
}
public void CloseJob(QZ_JobParams jobParams)
{
JobKey jobkey = new JobKey("j" + jobParams.TaskId, HDP_Task.HTTPTASK);
TriggerKey triggerKey = new TriggerKey("t" + jobParams.TaskId, HDP_Task.HTTPTASK);
_Scheduler.PauseTrigger(triggerKey);
_Scheduler.UnscheduleJob(triggerKey);
_Scheduler.PauseJob(jobkey);
_Scheduler.DeleteJob(jobkey);
}
/// <summary>
/// 暂停任务
/// </summary>
/// <param name="taskInfo"></param>
/// <returns></returns>
//public string Pause(TaskInfoModel taskInfo)
//{
// JobKey jobkey = new JobKey("j" + taskInfo.Id, "http");
// TriggerKey triggerKey = new TriggerKey("t" + taskInfo.Id, "http");
// scheduler.PauseJob(jobkey);
// scheduler.PauseTrigger(triggerKey);
// return taskInfo.Id;
//}
///// <summary>
///// 关闭任务
///// </summary>
///// <param name="taskInfo"></param>
///// <returns></returns>
//public string Close(TaskInfoModel taskInfo)
//{
// JobKey jobkey = new JobKey("j" + taskInfo.Id, "http");
// TriggerKey triggerKey = new TriggerKey("t" + taskInfo.Id, "http");
// scheduler.PauseTrigger(triggerKey);
// scheduler.UnscheduleJob(triggerKey);
// scheduler.PauseJob(jobkey);
// scheduler.DeleteJob(jobkey);
// return taskInfo.Id;
//}
private JobKey GetJobKey(string Id,string GroupName)
{
JobKey jobKey = new JobKey("j" + Id, GroupName);
return jobKey;
}
}
}